mysql查询:MySQL解释查询的奇怪逻辑



我用是MySQL 4.1.20直以为当表某列出现在查询语句order by中时如果这个列建了索引MySQL利用这个索引现在才发现这个想当然是

这几天对数据库跟踪我发现总是有些特简单SQL执行起来特别慢

如:select id,title from sources where status=1 and isrepeat=0 order by id desc limit 0,30;
这个查询执行大概需要3s表sources大概有26万数据,id是自增主键status和isrepeat都建了索引使用explain看下MySQL是如何解释他:

; mso-width-alt: 4352\" width=136> ; mso-width-alt: 3872\" width=121> ; mso-width-alt: 2208\" width=69> sources ref status_2 status_2 3 const,const 178760 Using where; Using filesort



居然没有用到主键索引(id)再看看MySQL是如何解释几乎同样SQL(select id,title from sources where status=1 and isrepeat=0 and id>0 order by id desc limit 0,30;):

; mso-width-alt: 4352\" width=136> ; mso-width-alt: 3872\" width=121> ; mso-width-alt: 2208\" width=69> sources range PRIMARY,status_2 PRIMARY 4 NULL 52704 Using where
Tags:  mysql查询

延伸阅读

最新评论

发表评论