SQL获取行数优化

发布于 2024-12-27 08:37:02 字数 606 浏览 1 评论 0原文

可能的重复:
在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反话 2025-01-03 08:37:02

他们是不同的。

COUNT(*) 将检索所有值(甚至是 NULL 值计数)。
COUNT(t.id) 不计算 NULL 值。


在性能方面,它们是相同的(查询优化器很智能)。

They are different.

COUNT(*) will retrieve all values (even the NULL values count).
COUNT(t.id) doesn't count the NULL values.


In terms of performance, they are the same (the query optimizer is smart).

烛影斜 2025-01-03 08:37:02

查询计划看起来相同,但在测试中,使用 * 更快(稍微)。

不过,指定列名时不会考虑空值。

Query plan looks the same, but in testing, using * is faster (slightly).

Nulls are not taking into account when specifying a column name though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文