I was curious. Everything is good to read documentation and theoretical answers, but I like to balance those who have empirical data.
I have a MySQL table (InnoDB) that contains 5,607,997 records. The table is in my own sandbox, so I know that the content is static and no one else uses the server. I think this effectively eliminates all external performance impacts. I have a table with the auto_increment (Id) primary key field, which, as I know, will never be null, which I will use for the where where test (ID WHERE NO NO).
The only possible glitch that I see when performing tests is the cache. The first time you run a query, it will always be slower than subsequent queries that use the same indexes. I will refer to this below as a request for caching Seeding. Just to mix it up a bit, I ran it with a where clause, which I know will always be evaluated as true regardless of any data (TRUE = TRUE).
Here are my results:
Querytype
| w/o WHERE | where id is not null | where true=true
COUNT ()
| 9 min 30.13 sec ++ | 6 min 16.68 sec ++ | 2 min 21.80 sec ++ | 6 min 13.34 sec | 1 min 36.02 sec | 2 min 0.11 sec | 6 min 10.06 se | 1 min 33.47 sec | 1 min 50.54 sec
COUNT (Id)
| 5 min 59.87 sec | 1 min 34.47 sec | 2 min 3.96 sec | 5 min 44.95 sec | 1 min 13.09 sec | 2 min 6.48 sec
COUNT (1)
| 6 min 49.64 sec | 2 min 0.80 sec | 2 min 11.64 sec | 6 min 31.64 sec | 1 min 41.19 sec | 1 min 43.51 sec
++ This is considered caching. It is expected that it will be slower than the rest.
I would say that the results speak for themselves. COUNT (Id) usually cuts others. Adding a Where clause significantly reduces access time, even if this condition, which you know, evaluates to true. Sweet spot seems COUNT (Id) ... WHERE Id NOT NULL.
I would like to see the results of other peoples, perhaps with smaller tables or with sentences in different fields, except for the field that you consider. I am sure there are other options that I have not taken into account.
Chris Mar 19 '09 at 21:36 2009-03-19 21:36
source share