count(expr)
函数选择指南count(expr)
: MySQL聚合函数,用于统计列值或表行数。count(*)
行为where
条件时高效。count(*)
行为。count(expr)
函数详解count(主键)
: 遍历表,返回主键值,统计总数。count(1)
: 遍历表,Server层每行放置数字1,统计总数。count(非主键字段)
: 统计字段值不为NULL的总数,可能少于实际行数。item_sum.cc
文件。count(*)
: 内部视为count(0)
。count(1)
: 直接处理为longlong
类型1。count(字段名)
: 处理为字段类型Item_field
。count(非主键字段) < count(主键) < count(1) ≈ count(*)
。count(*)
,优化器选择最小索引树。count(*)
: 包含NULL值的行数。count(非主键字段)
: 不含NULL值的行数。show table status
的准确性count(*)
和count(1)
通过主键索引统计。count(*)
,利用MySQL优化器。count(expr)
变体。show table status
获取精确行数。