1

so I executed this query on a table:

EXPLAIN SELECT COUNT(*) FROM table;

and the 'rows' column from the output is displayed as NULL (whereas usually it will show how many rows the query went through)...

does this mean that the COUNT command is instantaneous and therefore does not require going through any row whatsoever?

3 Answers 3

4

If your table uses the MyISAM storage engine, then yes, that query resolves in constant time. The row count is part of the table metadata, the table itself does not have to be examined.

Sign up to request clarification or add additional context in comments.

1 Comment

Then it'll probably use the primary key index. The row count is not stored separately for InnoDB tables. The EXPLAIN would tell you this. For MyISAM it'll say Select tables optimized away, for InnoDB it'll tell you what index, if any, is used.
0

From: http://www.wikivs.com/wiki/MySQL_vs_PostgreSQL#COUNT.28.2A.29

Depending on what engine you are using, and I'm guessing it's MyISAM, a quick index count is executed rather than actually counting all the rows.

Comments

0

Many database engines use an index scan to get the count. If you're using MyISAM (fairly likely), it just reads a number in the engine index and returns it. Nearly instantaneous.

Edit:

InnoDB does a full table scan, so it will almost always be slower (than an engine that uses the table index), unless you're comparing queries with a WHERE clause.

2 Comments

The page you link contradicts what you said. InnoDB is not the same way, whether there is a WHERE clause or not. I think you've misread.
@Dan, you're right. It was very late when I answered this question :) I've fixed the error in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.