sql - Count(*) vs Count(1) -
just wondering if of guys use count(1)
on count(*)
, if there noticeable difference in performance or if legacy habit has been brought forward days gone past?
(the specific database sql server 2005.)
there no difference.
reason:
books on-line says "
count ( { [ [ | distinct ] expression ] | * } )
"
"1" non-null expression: it's same count(*)
. optimizer recognizes is: trivial.
the same exists (select * ...
or exists (select 1 ...
example:
select count(1) dbo.tab800krows select count(1),fkid dbo.tab800krows group fkid select count(*) dbo.tab800krows select count(*),fkid dbo.tab800krows group fkid
same io, same plan, works
edit, aug 2011
edit, dec 2011
count(*)
mentioned in ansi-92 (look "scalar expressions 125
")
case:
a) if count(*) specified, result cardinality of t.
that is, ansi standard recognizes bleeding obvious mean. count(1)
has been optimized out rdbms vendors because of superstition. otherwise evaluated per ansi
b) otherwise, let tx single-column table result of applying <value expression> each row of t , eliminating null values. if 1 or more null values eliminated, completion condition raised: warning-
Comments
Post a Comment