Cesar
Cesar

Reputation: 3519

Sql COUNT Performance Question

Structure of Example table:

Id, Integer (PK)
Name, Varchar(100)
Description, Text

I need to know if exists difference in performance between:

SELECT COUNT(*) FROM Example;

and

SELECT COUNT(Id) FROM Example;

Or does not exists differences?

Upvotes: 3

Views: 4736

Answers (4)

Mike Chaliy
Mike Chaliy

Reputation: 26658

Worth reading, but unfortunately in russian.

COUNT(*), could it be faster?

Short story is that COUNT(*) is not so efficient as direct access to the dm_db_partition_stats.

Upvotes: 0

Jim B
Jim B

Reputation: 8574

I would believe that there is a difference; depending on the size of your table, it could be negligible, or it could be significant.

This is probably the slowest: SELECT COUNT(*) FROM Example
This is in the middle: SELECT COUNT(Id) FROM Example
This would be fastest: SELECT COUNT(1) FROM Example

Upvotes: 0

David Hedlund
David Hedlund

Reputation: 129792

Differences exist in both performance and the actual resultset

Performance of COUNT SQL function

Upvotes: 2

davek
davek

Reputation: 22915

Check the explain plan: does the result come from the table or from meta data (e.g. INFORMATION_SCHEMA)?: The differences are likely to be very minimal.

Upvotes: 0

Related Questions