Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Count(*) vs Select Count(id) vs select count(1). Are these indeed equivalent?

As a follow up to my earlier question:

Some of the answers and comments suggest that

select count(*) is mostly equivalent to select count(id) where id is the primary key.`

I have always favored select count(1); I even always use if exists (select 1 from table_name) ...

Now my question is:

1) What is the optimal way of issuing a select count query over a table?

2) If we add a where clause: where msg_type = X; if msg_type has a non_clustered index, would select count(msg_type) from table_name where msg_type = X be the preferred option for counting?

Side-bar:

From a very early age, I was taught that select * from... is BAD BAD BAD, I guess this has made me skeptical of select count(*) as well

like image 575
Charles Okwuagwu Avatar asked Jun 05 '16 08:06

Charles Okwuagwu


People also ask

Is COUNT 1 and COUNT (*) Same?

The simple answer is no – there is no difference at all. The COUNT(*) function counts the total rows in the table, including the NULL values. The semantics for COUNT(1) differ slightly; we'll discuss them later. However, the results for COUNT(*) and COUNT(1) are identical.

Is COUNT ID faster than COUNT *?

... if you have a non-nullable column such as ID, then count(ID) will significantly improve performance over count(*).

What is the difference between select * and select 1?

Difference between Select * and Select 1: Select * means it selects all the columns in that table as well as total number of rows exist in that table. Where as Select 1 means "1" is treated as a new column with data 1 for that column and as many rows exist for that table.

What is the difference between select * and select COUNT (*)?

Select * Would return the entire table while Select Count(*) would return the number of rows.


2 Answers

count(*)  --counts all values including nulls

count(id)-- counts this column value by excluding nulls

count(1) is same as count(*)

If we add a where clause: where msg_type = X; if msg_type has a non_clustered index, would select count(msg_type) from table_name where msg_type = X be the preferred option for counting?

As i mentioned in my previous answer ,SQL server is a cost based optimizer and the plan choosen depends on many factors .sql tries to retrieve cheapest plan in minimum time possible..

now when you issue,count(msg_type),SQL may choose this index if this is cheaper or scan another one as long as it gives right results(no nulls in output)..

I always tend to use count(*) ,unless i want to exclude nulls,for the reasons quoted here

like image 99
TheGameiswar Avatar answered Sep 29 '22 11:09

TheGameiswar


Well, those count queries are not identical and will do different things.

select count(1)
select count(*)

Are identical, and will count every record !

select count(col_name)

Will count only NOT NULL values on col_name !

So, unless col_name is the PK as you said, those query will do different things.

As for you second question, it depends, we can't provide you a generic answer that will always be true. You will have to look at the explain plan or just check for your self, although I believe that adding this WHERE clause while having this index will be better.

like image 22
sagi Avatar answered Sep 29 '22 13:09

sagi