Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1952063
  • 博文数量: 176
  • 博客积分: 1857
  • 博客等级: 上尉
  • 技术积分: 2729
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-14 22:55
个人简介

吾生有涯,而知无涯,适当止学.循序渐进,步步提升 Talk is cheap, show me the code.

文章分类

全部博文(176)

文章存档

2019年(1)

2018年(14)

2017年(20)

2016年(31)

2015年(15)

2014年(5)

2013年(10)

2012年(80)

分类: Mysql/postgreSQL

2012-08-29 16:28:58

建表脚本:
 create table stu(sid int,msname varchar(20));

插入数据脚本:

mysql> insert into stu values(1,'a');
Query OK, 1 row affected (0.02 sec)

mysql> insert into stu values(2,'b');
Query OK, 1 row affected (0.03 sec)

mysql> insert into stu values(3,'c');
Query OK, 1 row affected (0.02 sec)

mysql> insert into stu values(4,'d');
Query OK, 1 row affected (0.03 sec)

mysql> insert into stu values(5,'e');
Query OK, 1 row affected (0.02 sec)

mysql> insert into stu values(6,'f');
Query OK, 1 row affected (0.03 sec)

mysql> insert into stu values(7,'g');
Query OK, 1 row affected (0.01 sec)

mysql> insert into stu values(8,'zsd');
Query OK, 1 row affected (0.02 sec)

mysql> insert into stu values(8,'liuzhonghao');
Query OK, 1 row affected (0.03 sec)


使用组合索引:
create index ind_stu_sid_sname on stu(sid,msname);

如果,查询用到了索引中最左边的列。是走索引的。
mysql> explain select * from stu where sid =1;
+----+-------------+-------+------+-------------------+-------------------+-----
----+-------+------+--------------------------+
| id | select_type | table | type | possible_keys     | key               | key_
len | ref   | rows | Extra                    |
+----+-------------+-------+------+-------------------+-------------------+-----
----+-------+------+--------------------------+
|  1 | SIMPLE      | stu   | ref  | ind_stu_sid_sname | ind_stu_sid_sname | 5
    | const |    1 | Using where; Using index |
+----+-------------+-------+------+-------------------+-------------------+-----
----+-------+------+--------------------------+

如果,查询用到的是按msname列走的话,不走索引。

mysql> explain select * from stu where msname ='a';
+----+-------------+-------+-------+---------------+-------------------+--------
-+------+------+--------------------------+
| id | select_type | table | type  | possible_keys | key               | key_len
 | ref  | rows | Extra                    |
+----+-------------+-------+-------+---------------+-------------------+--------
-+------+------+--------------------------+
|  1 | SIMPLE      | stu   | index | NULL          | ind_stu_sid_sname | 48
 | NULL |    9 | Using where; Using index |
+----+-------------+-------+-------+---------------+-------------------+--------
-+------+------+--------------------------+
1 row in set (0.00 sec)


关于like查询的讨论。

再建立一个索引,
mysql> create index ind_msname on stu(msname);

用‘%a’去做查询条件,不走索引

explain select * from stu where msname like '%a';

mysql> explain select * from stu where msname like '%a';

+----+-------------+-------+-------+---------------+-------------------+--------
-+------+------+--------------------------+
| id | select_type | table | type  | possible_keys | key               | key_len
 | ref  | rows | Extra                    |
+----+-------------+-------+-------+---------------+-------------------+--------
-+------+------+--------------------------+
|  1 | SIMPLE      | stu   | index | NULL          | ind_stu_sid_sname | 48
 | NULL |    9 | Using where; Using index |
+----+-------------+-------+-------+---------------+-------------------+--------
-+------+------+--------------------------+
1 row in set (0.00 sec)

用'a%'去做查询条件,走索引

mysql> explain select * from stu where msname like 'a%';

mysql> explain select * from stu where msname like 'a%';
+----+-------------+-------+-------+---------------+------------+---------+-----
-+------+-------------+
| id | select_type | table | type  | possible_keys | key        | key_len | ref
 | rows | Extra       |
+----+-------------+-------+-------+---------------+------------+---------+-----
-+------+-------------+
|  1 | SIMPLE      | stu   | range | ind_msname    | ind_msname | 43      | NULL
 |    1 | Using where |
+----+-------------+-------+-------+---------------+------------+---------+-----
-+------+-------------+

可知,如果针对like条件'%a%'号来说,就是不走索引的。

阅读(1575) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

zhangshengdong2012-08-29 16:38:56

MySQL估计索引比全表扫描的速度更慢,则不走索引。
如果where条件里面有两列,而索引只对应了一列,那么不走索引。
复合索引,如果某列不是第一列,不走索引。
like中的值‘%’为第一位,不走索引。

如果列类型是字符串类型,但是where条件中,查找的值没有用引号引起来。也不走索引。


查看索引的性能:
show status like 'handler_read%';

如果变量Hander_read_rnd_next的值高,意味着查询运行效率低下。
需要建立索引补救。