3

Is there any difference in the performance, or the background execution behavior when counting * , any custom field ( n ), or the primary id in MySQL?

What exactly does * refer to in the query, and how does it differ from those two other ways?

SELECT COUNT( * ) FROM t;
SELECT COUNT( id ) FROM t;
SELECT COUNT( n ) FROM t;

UPDATE:

Assume, that neither id nor n is null at any record.

5
  • Count(*) counts all, count(id) will only count non nulls Commented Jul 30, 2015 at 7:15
  • You can't really compare performance of queries that return different results :-? Commented Jul 30, 2015 at 7:15
  • Let's assume, that the primary id is not null at any record. I guess, that's rather usual. Commented Jul 30, 2015 at 7:16
  • Correct. The question is not actually about performance because the options provided are asking two different questions. If a table has a PK then it's almost certain that count(*) would be implemented by counting the rows in the index. Commented Jul 30, 2015 at 7:16
  • 1
    For counting all rows, you can also use count(1) Commented Jul 30, 2015 at 7:17

3 Answers 3

3
COUNT(*) will include NULLS
COUNT(column_or_expression) won't.

This means COUNT(any_non_null_column) will give the same as COUNT(*) of course because there are no NULL values to cause differences.

Generally, COUNT(*) should be better because any index can be used because COUNT(column_or_expression) may not be indexed or SARGable

From ANSI-92 (look for "Scalar expressions 125")

Case:

a) If COUNT(*) is specified, then the result is the cardinality of T.

b) Otherwise, let TX be the single-column table that is the result of applying the to each row of T and eliminating null values. If one or more null values are eliminated, then a completion condition is raised: warning- null value eliminated in set function.

The same rules apply to SQL Server and Sybase too at least

Note: COUNT(1) is the same as COUNT(*) because 1 is a non-nullable expression.

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

Comments

1

count(*) has some optimizations in certain cases (querying a single MyISAM table with no where clause), which could to be the case in the OP, depending on the storage engine. If your query doesn't hit this special case, MySQL will have to construct an execution plan and run the query normally, which would be just as good (or bad) as count(my_primary_key) if you have a primary key.

Long story short - don't over think it. Just use count(*) and let the database worry about optimizing your query. After all, that's what it's build for.

4 Comments

count(*) and count(primary key) are not the same
@Mihai - Why are they different?
@RickJames One counts null the other doesn`t.
@Mihai primary keys don't have mulls - neither count nulls.
1

For InnoDB tables, you will probably find that the "smallest" index is used for COUNT(*), COUNT(1), or COUNT(id). To see this, do EXPLAIN SELECT COUNT(...) FROM tbl;.

If you have no secondary indexes, then the 'table' must be scanned.

Note that each secondary key includes the column(s) of the PRIMARY KEY. So, for

PRIMARY KEY(a, b)
INDEX c_d (c, d)

probably any count on any of those 4 columns, or (1) or (*) will use the c_d index.

COUNT(e) will need to scan the table.

Comments

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.