SQL获取行数优化
可能的重复:
在 SQL 中,count(column) 和 count( 之间有什么区别)?
计数() 与 Count(1)
我有一个大表,用于保存长文本,例如电子邮件内容或新闻。还有我的问题 计算表行数的性能有什么区别:
SELECT COUNT(*) FROM table_name
SELECT COUNT(t.id) FROM table_name as t
哪个更好?或者后一个将被查询优化器优化?有相关的文档吗?
Possible Duplicate:
In SQL, what's the difference between count(column) and count()?
Count() vs Count(1)
I have big tables which keep long texts for example email content or news. And my question
is there any difference for performance for counting table rows :
SELECT COUNT(*) FROM table_name
SELECT COUNT(t.id) FROM table_name as t
Which one is better? or the latter one will be optimized by query optimizer? Is there any documentation regarding this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
他们是不同的。
COUNT(*)
将检索所有值(甚至是NULL
值计数)。COUNT(t.id)
不计算NULL
值。在性能方面,它们是相同的(查询优化器很智能)。
They are different.
COUNT(*)
will retrieve all values (even theNULL
values count).COUNT(t.id)
doesn't count theNULL
values.In terms of performance, they are the same (the query optimizer is smart).
查询计划看起来相同,但在测试中,使用 * 更快(稍微)。
不过,指定列名时不会考虑空值。
Query plan looks the same, but in testing, using * is faster (slightly).
Nulls are not taking into account when specifying a column name though.