在一个表上有2个联合索引y(a,b,c),x(a,c)
我查询的时候 select * from table where a=1201 order by c desc limit 20 的时候用explain查看,发现mysql用的是y索引
这样我在extra里看到有filesort,而不是我想要的x索引,这个怎么办?怎么才能用到我的x索引

解决方案 »

  1.   

    select * from table FORCE  INDEX (x)
    where a=1201 
    order by c desc limit 20 使用 Index Hint 
    详见MYSQL官方手册MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
      

  2.   

    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
      

  3.   

    ok,我发现有的时候是使用x索引,有的时候是y
    这是为什么呢?
      

  4.   

    因为这个表的数据现在大概有 200多w,估计以后还会多,用了filesort感觉很慢
      

  5.   


    提供你的 
    show create table xxxx;
    show index from xxxx;
    explain select * from table where a=1201 order by c desc limit 20
      

  6.   

    | tb_goods |          0 | PRIMARY   |            1 | iid         | A         |     2350642 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | cid_total |            1 | cid         | A         |        5366 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | cid_total |            2 | lmsn        | A         |       78354 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | key_total |            1 | cid         | A         |        5366 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | key_total |            2 | key_prop    | A         |        5921 |       10 | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | key_total |            3 | lmsn        | A         |       83951 |     NULL | NULL   |      | BTREE      | NULL    explain select iid,title,nick,category_name,pic_path,price,credit from tb_goods where cid=50010815   order by lmsn desc  limit 0,20;
    +----+-------------+----------+------+---------------------+-----------+---------+-------+-------+-----------------------------+
    | id | select_type | table    | type | possible_keys       | key       | key_len | ref   | rows  | Extra                       |
    +----+-------------+----------+------+---------------------+-----------+---------+-------+-------+-----------------------------+
    |  1 | SIMPLE      | tb_goods | ref  | cid_total,key_total | key_total | 4       | const | 30046 | Using where; Using filesort | 
    +----+-------------+----------+------+---------------------+-----------+---------+-------+-------+-----------------------------+看,这里应该使用的索引应该是cid_totlal
      

  7.   

    | tb_goods |      1 | cid_total |      1 | cid     | A     |    5366 |   NULL | NULL   |    | BTREE    | NULL  | 
    | tb_goods |      1 | cid_total |      2 | lmsn    | A     |     78354 |   NULL | NULL   |    | BTREE    | NULL  | | tb_goods |      1 | key_total |      1 | cid     | A     |    5366 |   NULL | NULL   |    | BTREE    | NULL  | 
    | tb_goods |      1 | key_total |      2 | key_prop  | A     |    5921 |     10 | NULL   |    | BTREE    | NULL  | 
    | tb_goods |      1 | key_total |      3 | lmsn    | A     |     83951 |   NULL | NULL   |    | BTREE    | NULL  索引 key_total 中的 Cardinality 比较大,所以MYSQL会选取大的那个。理论上你的这两个索引的 Cardinality 应该一样。 analyze table tb_goods, 让MYSQL重新分析生成一下索引的相关信息。
      

  8.   

    顶一下 楼主给点分哦 MySQL手册是个很好的帮助工具
      

  9.   

    昏,analyze table tb_goods 索引没有被修复
    tb_goods |          1 | cid_total |            1 | cid         | A         |        5356 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | cid_total |            2 | lmsn        | A         |       78738 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | nickgoods |            1 | nick        | A         |       42948 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | nickgoods |            2 | lmsn        | A         |      214740 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | key_total |            1 | cid         | A         |        5356 |     NULL | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | key_total |            2 | key_prop    | A         |        5920 |       10 | NULL   |      | BTREE      | NULL    | 
    | tb_goods |          1 | key_total |            3 | lmsn        | A         |       81453 |     NULL | NULL   |      | BTREE      | NULL    | 
    +----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+