1

A question on a simple SQL statement, but one which I sometimes wonder over. Thought I'd see if anyone knew the answer to.

When counting the records in a table using a simple SQL statement, which has the least overheard:

1) SELECT COUNT(single_primary_field) FROM table, i.e. SELECT COUNT(user_ID) FROM users;

2) SELECT COUNT(*) FROM table

I initially thought the first may be quickest. But perhaps not having a specific field to associate with makes the second quicker?

Probably makes very little difference speed wise either way.

Thanks

forkdbloke
  • 1,505
  • 2
  • 12
  • 29
excession
  • 11
  • 2
  • Does this answer your question? [Which is faster/best? SELECT \* or SELECT column1, colum2, column3, etc](https://stackoverflow.com/questions/65512/which-is-faster-best-select-or-select-column1-colum2-column3-etc) – Simas Joneliunas Nov 22 '19 at 06:43

3 Answers3

2

COUNT(column) counts only selected column and ignore the null values.

COUNT(*) count rows and don't care values in the columns.

Using COUNT(*) is a better way for counting rows.

Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
0

Count(*) is most efficent way to count according to mysql: Count

Nouman Janjua
  • 410
  • 2
  • 9
0

Have a read through https://mariadb.com/kb/en/library/explain/ to look at what type of indexing your query is using, it usually hints at its performance.

I think Count(*) is going to be the fastest because maria does store a running count.