建了个表bbmsg,索引是主键索引id,多列索引(PhoneNum,RecvTime),
可是在order by id和order by recvtime的时候,一个使用索引,一个未使用有些不解,麻烦帮忙解释下,谢谢了1.先看表bbmsg的索引
mysql> show index from bbmsg;
+-------+------------+-------------------+--------------+--------------+
| Table | Non_unique | Key_name          | Seq_in_index | Column_name  |
+-------+------------+-------------------+--------------+--------------+
| bbmsg |          0 | PRIMARY           |            1 | ID           |
| bbmsg |          1 | PhoneNum_RecvTime |            1 | PhoneNum     |
| bbmsg |          1 | PhoneNum_RecvTime |            2 | RecvTime     |
+-------+------------+-------------------+--------------+--------------+
+-------------+---------+-------------+----------+--------+------+------------+
| Column_name |Collation| Cardinality | Sub_part | Packed | Null | Index_type |
+-------------+---------+-------------+----------+--------+------+------------+
| ID          | A       |      201658 |     NULL | NULL   |      | BTREE      |
| PhoneNum    | A       |          45 |     NULL | NULL   |      | BTREE      |
| RecvTime    | A       |      201658 |     NULL | NULL   |      | BTREE      |
+-------------+---------+-------------+----------+--------+------+------------+
3 rows in set (0.00 sec)
2.按照id降序排列时,未使用id这个索引
mysql> explain select recvtime from bbmsg where phonenum='13065670534' 
 order by id desc limit 2;
+----+-------------+-------+------+-------------------+-------------------+
| id | select_type | table | type | possible_keys     | key               |
+----+-------------+-------+------+-------------------+-------------------+
|  1 | SIMPLE      | bbmsg | ref  | PhoneNum_RecvTime | PhoneNum_RecvTime |
+----+-------------+-------+------+-------------------+-------------------+
+---------+-------+------+-----------------------------+
| key_len | ref   | rows | Extra                       |
+---------+-------+------+-----------------------------+
| 22      | const | 7072 | Using where; Using filesort |
+---------+-------+------+-----------------------------+
1 row in set (0.00 sec)2.按照recvtime降序排列时,使用了索引
mysql> explain select id from bbmsg where phonenum='13065670534' 
       order by recvtime desc limit 2;
+----+-------------+-------+------+-------------------+-------------------+
| id | select_type | table | type | possible_keys     | key               |
+----+-------------+-------+------+-------------------+-------------------+
|  1 | SIMPLE      | bbmsg | ref  | PhoneNum_RecvTime | PhoneNum_RecvTime |
+----+-------------+-------+------+-------------------+-------------------+
+---------+-------+------+-------------+
| key_len | ref   | rows | Extra       |
+---------+-------+------+-------------+
| 22      | const | 7072 | Using where |
+---------+-------+------+-------------+
1 row in set (0.00 sec)

解决方案 »

  1.   

    mysql> explain select recvtime from bbmsg where phonenum='13065670534'  
     order by id desc limit 2; 
    这里的分析,显示了查询的时候,它只识别到一个索引,就是PhoneNum_RecvTime,所以当
    phonenum='13065670534' 的时候,它就找到了索引并使用了索引,接着你又要它按照id来
    排序,但是上面并没有找到id这个主索引,所以就要用到filesort作一下临时的排序了。如果
    你的SQL语句是:
    select id,recvtime from bbmsg where phonenum='13065670534'  
     order by id desc limit 2;
    的话,可能就不会用到filesort了。mysql> explain select id from bbmsg where phonenum='13065670534'  
           order by recvtime desc limit 2; 
    这里也是一样,首先phonenum='13065670534'有索引PhoneNum_RecvTime可用,接着你又要它按照
    recvtime来排序,但是recvtime的排序已经在索引PhoneNum_RecvTime里面了,所以就不用filesort了。
    以上是个人理解,不当之处,还请指出。
      

  2.   

    看到水木上的回复,不知道说的全面不全面?发信人: diogin (design universe...), 信区: Database
    标  题: Re: mysql如何使用索引的呢?
    发信站: 水木社区 (Tue May  6 10:17:11 2008), 站内 mysql> explain select recvtime from bbmsg where phonenum='13065670534'
            order by id desc limit 2;对每个表的每次查询,mysql只会使用其中的一个索引。
    你定义了两个索引,一个主键,一个组合,那么针对该表的每条查询只会用到其中
    的一个索引,要么是主键,要么是组合,不可能两条都用得上。所以如果仅仅针对这条查询,最佳的索引是组合索引 (phonenum, id)
      

  3.   


    水木的回答可能不太让你明白。如果根据主键排序,是可以用到索引。
    如果要组合排序,只可以用到最左边的。
    索引针对这个问题。
    ID列的索引是用不到的。
    如果你要建立索引的话。
    不要组合。
    (phonenum)
    (id)