《Mysql必读MySQL 5.7 InnoDB对COUNT(*)的优化》要点:
本文介绍了Mysql必读MySQL 5.7 InnoDB对COUNT(*)的优化,希望对您有用。如果有疑问,可以联系我们。
- InnoDB: SELECT COUNT(*) FROM t statements now invoke a single handler call to the storage engine to scan the clustered index and return the row count to the Optimizer. Previously, a row count was typically performed by traversing a smaller secondary index and invoking a handler call for each record. A single handler call to the storage engine to count rows in the clustered index generally improves SELECT COUNT(*) FROM t performance. However, in the case of a large clustered index and a significantly smaller secondary index, performance degradation is possible compared to performance using the previous, non-optimized implementation. For more information, see Limits on InnoDB Tables.
简单地说就是:COUNT(*)会选择聚集索引,进行一次内部handler函数调用,即可快速获得该表总数.我们可以通过执行计划看到这个变化,例如:
很明显,在查询优化器阶段就已经得到优化了,相比效率应该杠杠的吧,我们稍后再来对比看看.
补充说下,5.7以前的版本中,COUNT(*)请求通常是:扫描普通索引来获得这个总数.也来看看5.6下的执行计划是怎样的:
可以看到,可以利用覆盖索引来完成COUNT(*)请求.MYSQL学习
- InnoDB: SELECT COUNT(*) FROM t statements now invoke a single handler call to the storage engine to scan the clustered index and return the row count to the Optimizer. Previously, a row count was typically performed by traversing a smaller secondary index and invoking a handler call for each record. A single handler call to the storage engine to count rows in the clustered index generally improves SELECT COUNT(*) FROM t performance. However, in the case of a large clustered index and a significantly smaller secondary index, performance degradation is possible compared to performance using the previous, non-optimized implementation. For more information, see Limits on InnoDB Tables.
count(*)对比测试MYSQL学习 |
MySQL 5.6.33MYSQL学习 |
MySQL 5.7.15MYSQL学习 |
相差MYSQL学习 |
表数据量MYSQL学习 |
1亿MYSQL学习 |
1亿MYSQL学习 |
0.00%MYSQL学习 |
耗时(秒)MYSQL学习 |
693.66MYSQL学习 |
5331.69MYSQL学习 |
768.63%MYSQL学习 |
转载请注明本页网址:
http://www.vephp.com/jiaocheng/5723.html