Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of COUNT SQL function

I have two choices when writing an SQL statement with the COUNT function.

  1. SELECT COUNT(*) FROM <table_name>
  2. SELECT COUNT(some_column_name) FROM <table_name>

In terms of performance, what is the best SQL statement? Can I obtain some performance gain by using option 1?

like image 752
Upul Bandara Avatar asked Nov 17 '09 10:11

Upul Bandara


1 Answers

Performance should not matter because they do 2 different aggregates

  • COUNT(*) is all rows, including NULLs
  • COUNT(some_column_name), excludes NULL in "some_column_name"

See the "Count(*) vs Count(1)" question for more

like image 122
gbn Avatar answered Nov 07 '22 17:11

gbn