免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 10253 | 回复: 30
打印 上一主题 下一主题

mysql不能正确的使用 联合索引 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-16 22:19 |只看该作者 |倒序浏览
在一个表上有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索引

| 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

analyze table tb_goods 没有修复索引

论坛徽章:
0
2 [报告]
发表于 2009-11-17 10:14 |只看该作者
顶上来,那位能告知一二

论坛徽章:
9
每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00数据库技术版块每周发帖之星
日期:2016-03-07 16:30:25
3 [报告]
发表于 2009-11-17 11:55 |只看该作者
explain select iid,title,nick,category_name,pic_path,price,credit from tb_goods using index cid_total
where cid=50010815  order by lmsn desc  limit 0,20;

试试看?

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
4 [报告]
发表于 2009-11-17 11:56 |只看该作者
FORCE INDEX (FIELD1)看看

论坛徽章:
1
白银圣斗士
日期:2015-11-23 08:33:04
5 [报告]
发表于 2009-11-17 11:57 |只看该作者
或 USE INDEX (indexname)

论坛徽章:
0
6 [报告]
发表于 2009-11-17 12:10 |只看该作者
原帖由 cenalulu 于 2009-11-17 11:55 发表
explain select iid,title,nick,category_name,pic_path,price,credit from tb_goods using index cid_total
where cid=50010815  order by lmsn desc  limit 0,20;

试试看?


explain select iid,title,nick,category_name,pic_path,price,credit from tb_goods using index cid_total where cid=50011986 order by lmsn desc  limit 0,20;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'using index cid_total where cid=50011986 order by lmsn desc  limit 0,20' at line 1

论坛徽章:
9
每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00数据库技术版块每周发帖之星
日期:2016-03-07 16:30:25
7 [报告]
发表于 2009-11-17 12:12 |只看该作者
原帖由 yyxxzz 于 2009-11-17 12:10 发表


explain select iid,title,nick,category_name,pic_path,price,credit from tb_goods using index cid_total where cid=50011986 order by lmsn desc  limit 0,20;
ERROR 1064 (42000): You have an error ...


不好意思。。。是use index 不是 using index打错了

论坛徽章:
0
8 [报告]
发表于 2009-11-17 12:22 |只看该作者
原帖由 cenalulu 于 2009-11-17 12:12 发表


不好意思。。。是use index 不是 using index打错了


explain select iid,title,nick,category_name,pic_path,price,credit from tb_goods use index cid_total where cid=50011986 order by lmsn desc  limit 0,20;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cid_total where cid=50011986 order by lmsn desc  limit 0,20' at line 1

我的mysql 版本是5.0.22

论坛徽章:
9
每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00数据库技术版块每周发帖之星
日期:2016-03-07 16:30:25
9 [报告]
发表于 2009-11-17 12:27 |只看该作者
原帖由 yyxxzz 于 2009-11-17 12:22 发表


explain select iid,title,nick,category_name,pic_path,price,credit from tb_goods use index cid_total where cid=50011986 order by lmsn desc  limit 0,20;
ERROR 1064 (42000): You have an error i ...


不好意思。。。。罪孽深重。。。又打错了。。。。
use index后要有括号。。。use index (cid_total)
还是版主基本功扎实。。。

如果再错我去自尽拉。。。

论坛徽章:
0
10 [报告]
发表于 2009-11-17 12:55 |只看该作者
原帖由 cenalulu 于 2009-11-17 12:27 发表


不好意思。。。。罪孽深重。。。又打错了。。。。
use index后要有括号。。。use index (cid_total)
还是版主基本功扎实。。。

如果再错我去自尽拉。。。

恩,可以的,这样是用了cid_total,但是我想不用use index ,有什么办法呢,就是在
select iid,title,nick,category_name,pic_path,price,credit from tb_goods  where cid=50011986 order by lmsn desc  limit 0,20 这样的sql mysql自己认识到应该用cid_total,而不是key_total
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP