Skip to content


mysql笔记(4)-复合索引的性能问题

昨天有个朋友谈起复合索引,之前对复合索引只是大概了解没有专门的总结也不敢多言复合索引使用方法的优劣.查阅相关资料后把例子贴出与大家分享,希望可以抛砖引玉.

对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分。例如索引是key index (a,b,c). 可以支持a | a,b| a,b,c 3种组合进行查找,但不支持 b,c进行查找 .当最左侧字段是常量引用时,索引就十分有效。下面用几个例子对比查询条件的不同对性能影响.

create table test(
a int,
b int,
c int,
KEY a(a,b,c)
);

优: select * from test where a=10 and b>50
差: select * from test where a50

优: select * from test where order by a
差: select * from test where order by b
差: select * from test where order by c

优: select * from test where a=10 order by a
优: select * from test where a=10 order by b
差: select * from test where a=10 order by c

优: select * from test where a>10 order by a
差: select * from test where a>10 order by b
差: select * from test where a>10 order by c

优: select * from test where a=10 and b=10 order by a
优: select * from test where a=10 and b=10 order by b
优: select * from test where a=10 and b=10 order by c

优: select * from test where a=10 and b=10 order by a
优: select * from test where a=10 and b>10 order by b
差: select * from test where a=10 and b>10 order by c

索引原则

1.索引越少越好
原因:主要在修改数据时,第个索引都要进行更新,降低写速度。
2.最窄的字段放在键的左边
3.避免file sort排序,临时表和表扫描.

Posted in mysql, 技术.

Tagged with .


13 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. says

    坐臭皮匠沙发,走健康之路

  2. 果沟 says

    晕,沙发被你搞破了快。把你加到朋友链里边,看我抢你沙发

  3. says

    呵呵,老时喜欢坐地板。。

  4. zylew says

    学习mysql中……
    哈哈

  5. 老时 says

    嘿嘿

  6. kongcuo says

    很专业啊,以后电脑问题多向你请教。

  7. 果沟 says

    乐意帮忙。

  8. kongcuo says

    不好意思,本来我也应该在首页链接你的博客。但因为贵站的主题和我的主题实在有所不同,因此放在了link页面,望见谅。

  9. zonghua says

    链接已经给你加上了啊

  10. zonghua says

    博主是搞PHP开发的?

  11. 果沟 says

    当前做web开发

  12. db says

    如果数据量大的话
    19.优: select * from test where a>10 order by a 这个性能也很差的

    如果数据量不太大的话
    差: select * from test where a>10 order by b
    这个性能不会很差的哦

  13. 果沟 says

    言之有理,不过以上每组比较都是在同等条件下进行优劣比较的.



Some HTML is OK

or, reply to this post via trackback.