Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count rows in an SQL table in O(1)

I understand the best way to count the number of rows in an SQL table is count(*) (or equivalently count(PrimaryKey)).

  1. Is this O(1)?
  2. If not, why not?

Why not just implement a counter and return it for this specific query? Is it because this query is not a common use case?

If the answers vary according to SQL engine, I'd like to hear the differences - but in any case, I'm interested in the actual implementation in production SQL engines.

like image 886
ripper234 Avatar asked Dec 17 '08 14:12

ripper234


People also ask

How do I count rows in a table in SQL?

Use the COUNT aggregate function to count the number of rows in a table. This function takes the name of the column as its argument (e.g., id ) and returns the number of rows for this particular column in the table (e.g., 5).

How do I count rows in SQL with conditions?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values. COUNT() returns 0 if there were no matching rows.

Which SQL function is used to 1 point count the number of rows in a SQL query *?

The SQL COUNT( ) function is used to return the number of rows in a table. It is used with the Select( ) statement.

What is difference between count (*) and Count 1?

The difference is simple: COUNT(*) counts the number of rows produced by the query, whereas COUNT(1) counts the number of 1 values.


2 Answers

In some RDBM's this is O(1) (most notably MySQL), put AFAIK it is generally frowned upon and considered an "ugly performance hack". The reasons is that if you have transactions (which every real RDBM should have), the total number of rows in the table might or might not be equal to the total number you can see from the current transaction. This is why the server needs to check which rows are actually visible to your transaction, making it more O(n) than O(1).

If you want to optimize the process of getting the number of rows and are satisfied with an approximate result, most RDBM's have special "info" tables which hold information about the tables, including the approximate number of rows (again, it is not the exact number of rows because of the transactions).

like image 96
Grey Panther Avatar answered Oct 01 '22 17:10

Grey Panther


No this is not a common use case. Most row counts I have seen have some where clause involved.

The main reason this is not implemented though is the row counter would be a cause of contention in a multi user environment. Every time a row was inserted or deleted the counter would need updating effectively locking the whole table for each insert/delete.

like image 36
James Anderson Avatar answered Oct 01 '22 17:10

James Anderson