83

MySQL 5.7下InnoDB对COUNT(*)的优化-小鱼的博客-51CTO博客

 6 years ago
source link: http://blog.51cto.com/395469372/2066973
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

MySQL 5.7下InnoDB对COUNT(*)的优化

在MySQL 5.7 COUNT()会选择聚集索引,进行一次内部handler函数调用,即可快速获得该表总数

执行计划显示:Extra:Select tables optimized away

5.7以前的版本中,COUNT()请求通常是:扫描普通索引来获得这个总数。
6.执行计划显示: Extra:Using index

如果聚集索引较大(或者说表数据量较大),没有完全加载到buffer pool中的话,MySQL 5.7的查询方式有可能反而会更慢,还不如用原先的方式(MySQL 5.6)
解决办法:强制走普通索引,不过还需要加上WHERE条件,否则还是不行
select count() from stock force index(idx_stock) where id >0;
extra:using where,using index

©著作权归作者所有:来自51CTO博客作者会说话的鱼的原创作品,如需转载,请注明出处,否则将追究法律责任

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK