count(*)和count(1)的区别

您所在的位置:网站首页 wrong和mistake的区别 count(*)和count(1)的区别

count(*)和count(1)的区别

2023-04-01 06:57| 来源: 网络整理| 查看: 265

本文共 697 字,大约阅读时间需要 2 分钟。

Wrong 通常,count(*)和count(1)的计算结果一致。 若有主键,count(主键)的运行效率最快;若无主键,count(1)比count(*)运行效率要快。 若整个表只有一个行,count()的运行效率最快;若多于一个行时,count(1)要比count()运行效率快。因为count(*)会扫描整个表。

 

Right to MySQL8.x

InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way. There is no performance difference.

For MyISAM tables, COUNT(*) is optimized to return very quickly if the SELECT retrieves from one table, no other columns are retrieved, and there is no WHERE clause.

This optimization only applies to MyISAM tables, because an exact row count is stored for this storage engine and can be accessed very quickly. COUNT(1) is only subject to the same optimization if the first column is defined as NOT NULL.

 

 

 

转载地址:https://blog.csdn.net/lc11535/article/details/102631675 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3