SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


count(*) vs count(1) or count(id)


count(*) vs count(1) or count(id)

Author
Message
ashkan sirous
ashkan sirous
Old Hand
Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)Old Hand (395 reputation)

Group: General Forum Members
Points: 395 Visits: 255
Dear Experts,
I want to know what is difference between count(*), count(id) and count(1) base on performance issues.
We have some quires with count(*) that sometimes throws timeout and we have many Transactions on these tables in our other queries.
we guess that if we change it to count(id) or count(1) it will be better.
Is this change effective?
Any information will be helpful.

Faithfully Yours,
Ashkan

Thanks and regards,
Ashkan
Perry Whittle
Perry Whittle
SSC Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53613 Visits: 17672
Count(*) includes rows with null values whereas count(id) would not include rows with a null id. Unlikely if the id is a primary key though, so it does depend on the column itself to a certain extent.

-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs" ;-)
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2739 Visits: 1189
http://msdn.microsoft.com/en-us/library/ms175997.aspx

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Steve Jones
Steve Jones
SSC Guru
SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)

Group: Administrators
Points: 145643 Visits: 19425
Here's an article on this site discussing the issue: http://www.sqlservercentral.com/articles/Performance+Tuning/adviceoncount/808/

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Loundy
Loundy
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2396 Visits: 1190
Steve Jones - SSC Editor (5/1/2013)
Here's an article on this site discussing the issue: http://www.sqlservercentral.com/articles/Performance+Tuning/adviceoncount/808/


This is a great article, it's amazing what you can forget over time.... Smile

There's no kill switch on awesome!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search