count(*) vs count(1)--social.msdn.microsoft.com

Clever response Dave, but insufficient. I'll admit I've suggested this myself for certain questions but I think more is needed here. The OP may run one query where count(*) and count(1) return the same result and have the same performance, but that doesn't mean they always will. Similarly OP could test count(*) and count (<mycolumn>) and get the same performance, so assume they are always the same.

Count(*) and count(1) both just count the number of rows. SQL Server processes them the same way.

I know this is not what the OP asked, but just in case you were wondering:

count(*) and count(<mycolumn>) may or not behave the same. Count(<mycolumn>) counts how many rows have VALUES, i.e. are not NULL for <mycolumn>, so SQL Server has to actually look at the column. For count(*) no values need to be examined, and any nonclustered index can be used to quickly determine the count of the rows. For count(<mycolumn>) the actual value in <mycolumn> must be examined, so it could be a lot slower, UNLESS <mycolumn> does not even allow NULLs and then count(<mycolumn>) is identical to count(*)

原文地址:https://www.cnblogs.com/jiangfei5945/p/3396540.html