Home  >  Article  >  Backend Development  >  关于复合索引和单独索引的一个问题

关于复合索引和单独索引的一个问题

WBOY
WBOYOriginal
2016-06-06 20:37:481144browse

复合索引
1,shopid ctime
EXPLAIN SELECT COUNT(*) FROM dc_order_menu_log WHERE shopid = 12 AND ctime > 1422720000
查询行数:4357

单独索引
分别建立索引shopid ctime
EXPLAIN SELECT COUNT(*) FROM dc_order_menu_log WHERE shopid = 12 AND ctime > 1422720000
key:shopid
查询行数:2362

问题1:为什么查询的影响行数有区别?

问题2:如果单独的索引查询的影响行数还少,在表中单独建立索立 这样的优缺点是什么??

回复内容:

复合索引
1,shopid ctime
EXPLAIN SELECT COUNT(*) FROM dc_order_menu_log WHERE shopid = 12 AND ctime > 1422720000
查询行数:4357

单独索引
分别建立索引shopid ctime
EXPLAIN SELECT COUNT(*) FROM dc_order_menu_log WHERE shopid = 12 AND ctime > 1422720000
key:shopid
查询行数:2362

问题1:为什么查询的影响行数有区别?

问题2:如果单独的索引查询的影响行数还少,在表中单独建立索立 这样的优缺点是什么??

其实综合来讲就是单索引与复合索引的区别不同问题

索引的目的是为了快速找到所需的记录

但复合索引在数据库操作期间所需的开销更小,可以代替多个单一索引。

尤其当表的行数远远大于索引键的数目时,使用这种方式可以明显加快表的查询速度。

再说说为啥行数有区别,因为多索引是组合而成的。在shopid基础上再查询ctime
而单独索引 shopid与ctime分别查询,这样一旦数据量多了,会有微小差异。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn