MySQL COUNT(*) 和COUNT(1)的区别

先说结论:推荐使用COUNT(*)

 

通过MySQL5.7官方文档https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html可知

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. For example:

mysql> SELECT COUNT(*) FROM student;
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.

InnoDB引擎下COUNT(*) 和COUNT(1)没有性能差异。

MyISAM引擎下使用COUNT(1)时如果第一列定义为NOT NULL就和COUNT(*) 一样,否则性能没有COUNT(*)好。

 

所以推荐使用COUNT(*),无论时InnoDB还是MyISAM都能保证最优性能,并且MySQL8.0又对COUNT(*)做了优化。

官方文档链接https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html

 

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值