1

I have a simple query like:

select count(*) from table

Can I speed up MySQL by doing:

select count(id) from table

Does this make any difference in terms of speed?

Best Regards,

André
  • 24,706
  • 43
  • 121
  • 178

4 Answers4

1

Not if id is your primary key. * will simply map to your primary key. count(id) = count(*)

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
1

You can try to get the count from information_schema

select TABLE_ROWS from information_schema.tables
where TABLE_SCHEMA = '$db' and TABLE_NAME = '$tbl'; 
ajreal
  • 46,720
  • 11
  • 89
  • 119
0

If id is your primary key, then no it will not, since MySql will use the primary key under the covers anyway. If id is not the primary key, then it may actually make the query slightly slower.

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
0

If id is "not null", no it won't make any difference.

If id is nullable, then it is a different query and will not (always) produce the same result.

MarkR
  • 62,604
  • 14
  • 116
  • 151