Tuesday, May 8, 2007

Performance difference between Count(*) vs Count(1)

Both will give the same performance.

Lets consider the scenerio,

Case 1:- Sql server stores internally everything in pages of size 8kb.
So if we using count (1) or the count (*), actually we r dealing with these 8kb(8060 bytes to be precise)
So it doesnot matter if we use count(1) or Count(*), we r dealing with 8kb interal storage of pages.

Case2:-

Suppose we have a index in the table for which we r retreiving count(*) and count(1)

Sub case(A):- Suppose index is on the column which i am using in count.
In this case count(*) and count(1) will both use the same index and perform same.

Sub case(B): Suppose index is not on the column which we r using count(1)
In this case COunt (*) will use the default index but count(1) will not use it.
So count(1) will be slower if index is not on one.



If u like this post reply.

Wishes
whocares

No comments: