mysql count count id_为什么mysql count(*)具有比count(id)更好的性能

起初我想要比较count(*)和count(id),它有更好的性能?

5.6.21-1~dotdeb.1-log

表信息

PRIMARY KEY (`id`),

KEY `is_availability` (`is_availability`,`is_del`)

ENGINE=InnoDB AUTO_INCREMENT=48993819 DEFAULT CHARSET=utf8

>比较没有条件

select count(*) from op_log;

+----------+

| count(*) |

+----------+

| 48989975 |

+----------+

1 row in set (10.02 sec)

select count(id) from op_log ;

+-----------+

| count(id) |

+-----------+

| 48989990 |

+-----------+

1 row in set (12.05 sec)

count(*)优于count(id)

>与条件比较

select count(*) from op_log where is_availability=1;

+----------+

| count(*) |

+----------+

| 48990038 |

+----------+

1 row in set (15.86 sec)

select count(id) from op_log where is_availability=1;

+-----------+

| count(id) |

+-----------+

| 48990096 |

+-----------+

1 row in set (17.13 sec)

count(*)仍然比count(*)更好

所以,如果我能得出结论count(*)具有比count(id)更好的性能,为什么会这样?

从高性能MySQL,我得到了

if mysql knows some col cannot be NULL, it will optimize count(col) to count(*) internally

所以我怀疑花费更多的时间用于做这项优化工作.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值