在一个表上有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索引
我查询的时候 select * from table where a=1201 order by c desc limit 20 的时候用explain查看,发现mysql用的是y索引
这样我在extra里看到有filesort,而不是我想要的x索引,这个怎么办?怎么才能用到我的x索引
解决方案 »
- 如何查询前1000条数据
- 运行MySQL Command Line Client时一闪而过,开始安装的是很正常
- 彻底崩溃了!求救 Mysql 存储过程语句
- mysql 事务失败后,连接不上数据库。
- 连接数据库的恐怖问题,高手请看看!
- Fatal error: Maximum execution time of 30 seconds exceeded in E:\export\home\wwwroot\enspc\spc\searchresults.php on line 125
- 新手请教MySQL字段时间减小问题
- 请关于mysql索引与limit的问题!
- 电脑重启,插入变得很慢
- mysql的存储过程按下列要求如何写(只是用存储过程方式),求大神解答
- 问下MYSQL Cluster和master slave的问题
- 复制数据库用什么SQL语句
where a=1201
order by c desc limit 20 使用 Index Hint
详见MYSQL官方手册MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
这是为什么呢?
提供你的
show create table xxxx;
show index from xxxx;
explain select * from table where a=1201 order by c desc limit 20
| 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
| 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重新分析生成一下索引的相关信息。
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 |
+----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+