免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3489 | 回复: 9
打印 上一主题 下一主题

How do you specify difference between count(*) and count(1) in。。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-13 16:29 |只看该作者 |倒序浏览
不知道大家有没有注意这个问题,平时都怎么写这个query
看完这里,一句话,使用count(*),,

By  Rudy Limeback, SQL Consultant, r937.com
SearchOracle.com
Digg This Stumble Delicious
How do you specify difference between count(*) and count(1) in a select statement? Which is best to use and when?

The difference is simple: COUNT(*) counts the number of rows produced by the query, whereas COUNT(1) counts the number of 1 values. Note that when you include a literal such as a number or a string in a query, this literal is "appended" or attached to every row that is produced by the FROM clause. This also applies to literals in aggregate functions, such as COUNT(1). It's hard to imagine a scenario where the COUNT(*) and COUNT(1) values would be different, so please do let me know if you find one.

In general, you should always use COUNT(*). This is because the database can often count rows by accessing an index, which is much faster than accessing a table. If you use COUNT(column), the database must actually inspect the individual values in the column, since it will not count NULLs. Aggregate functions like COUNT and SUM always ignore NULLs.

One would like to assume that the database optimizer is smart enough to realize that the literal value 1 will never be NULL, and not need to inspect it on every row, but it also would not surprise me to find out that the actions of appending the 1s into every row produced by the FROM clause and counting the 1s are handled by two separate logic modules, and maybe the module doing the counting doesn't know where the 1s came from. So just use COUNT(*).

17 Jan 2007

论坛徽章:
3
CU大牛徽章
日期:2013-09-18 15:16:55CU大牛徽章
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43
2 [报告]
发表于 2010-10-13 16:49 |只看该作者
恩   很难区分

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
3 [报告]
发表于 2010-10-13 17:02 |只看该作者
好像count(*)的效率比count(1)高些。

论坛徽章:
0
4 [报告]
发表于 2010-10-13 17:31 |只看该作者
好像count(*)的效率比count(1)高些。
renxiao2003 发表于 2010-10-13 18:02



文章的作者是这么分析的,不过我一只图省事写count(1),没想到啊,呵呵

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
5 [报告]
发表于 2010-10-13 19:19 |只看该作者
回复 4# kakasi


    其实你还可以使用count(column_name),其中的列名是你的索引列。

论坛徽章:
0
6 [报告]
发表于 2010-10-13 20:44 |只看该作者
回复  kakasi


    其实你还可以使用count(column_name),其中的列名是你的索引列。
renxiao2003 发表于 2010-10-13 20:19


嗯,这个就是查非null个数了。

一直有误区,以为放了*进去会多搞出很多开销,而想当然的使用了常量1的话,会快些。看了这位老兄的分析,觉得挺有道理,,
不过话说回来,一个查数,慢也慢不到哪里去哈

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
7 [报告]
发表于 2010-10-13 21:18 |只看该作者
呵呵。要是量大你就看出性能优劣来了。

论坛徽章:
0
8 [报告]
发表于 2010-10-14 02:42 |只看该作者
一般用count(*)就行,如果数据比较多,或者有一列是有索引的,则可以把*替换为列名,性能会好些

论坛徽章:
3
CU大牛徽章
日期:2013-09-18 15:16:55CU大牛徽章
日期:2013-09-18 15:18:22CU大牛徽章
日期:2013-09-18 15:18:43
9 [报告]
发表于 2010-10-14 08:23 |只看该作者
如果要计数  count(pk column)应该是效率最高、准确度最高的做法

论坛徽章:
0
10 [报告]
发表于 2010-10-15 13:22 |只看该作者
select /*index_ffs*/ count(index_column) from table
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP