views:

985

answers:

8

I'm trying to find out if a row exists in a table. Using MySQL, is it better to do a query like this:

SELECT COUNT(*) AS total FROM table1 WHERE ...

and check to see if the total is non-zero or is it better to do a query like this:

SELECT * FROM table1 WHERE ... LIMIT 1

and check to see if any rows were returned?

In both queries, the WHERE clause uses an index.

A: 

A COUNT query is faster, although maybe not noticeably, but as far as getting the desired result, both should be sufficient.

jaywon
This is however DB specific. The COUNT(*) is known to be slow in PostgreSQL. Better would be to select the PK column and see if it returns any rows.
BalusC
+15  A: 

You could also try using

SELECT EXISTS(SELECT * FROM table1 WHERE ...)

per the documenation

Per a comment below:

SELECT EXISTS(SELECT 1 FROM table1 WHERE ...)
Chris Thompson
+1 - I never used "Exists" for this operation in MySQL.
James Black
Avoid using `SELECT *` wherever possible, as the `*` character will cause an extra lookup; remember, `SELECT 1 FROM table1 WHERE ...` will have the same net effect sans the overhead in this case.
Dereleased
@Dereleased very good point. Modified my post. Thanks!
Chris Thompson
OMG Ponies
A: 

COUNT(*) are optimized in MySQL, so the former query is likely to be faster, generally speaking.

Arthur Reutenauer
Are you referring to the optimization that the MyISAM has for selecting the count for a whole table? I didn't think that helped if there was a WHERE condition.
Bernard Chen
Yeah, sorry, typed too fast. That wouldn't help in that case.
Arthur Reutenauer
A: 

For non-InnoDB tables you could also use the information schema tables:

http://dev.mysql.com/doc/refman/5.1/en/tables-table.html

davek
+2  A: 

I'd go with COUNT(1). It is faster than COUNT(*) because COUNT(*) tests to see if at least one column in that row is != NULL. You don't need that, especially because you already have a condition in place (the WHERE clause). COUNT(1) instead tests the validity of 1, which is always valid and takes a lot less time to test.

Felix
-1 This is wrong. COUNT(*) doesn't look at the column values - it just counts the number of rows. See my answer here: http://stackoverflow.com/questions/2876909/count-and-countcolumn-name-whats-the-diff
Mark Byers
A: 

SELECT COUNT(*) is most likely faster. When you do SELECT COUNT(*), the database engine must send you only one number, possibly over network.

When you do SELECT *, the database engine must send you the whole row, which is significantly faster. Also in this case the database engine must also fetch more data from its memory and/or disk.

However, to get exact answer, you should measure how long it takes using each approach.

Juha Syrjälä
A: 

Ya know, we're sort of optimizing past a point where it's relevant here. If you're trying to learn, then by all means continue on...the more you know the better. If you are trying to write an application, though, you're much better off spending your time trying to grow your app to the point that it needs this sort of optimization than you are trying to optimize prematurely.

coreyward
Well, I was replacing code where someone was retrieving the full rows of every matching result into the application layer and then asking whether there were more than zero results. While replacing this, I figured I may as well pick an option that makes sense. I'm sure they all run pretty quickly, but when you have a choice, why not ask the question?
Bernard Chen
A: 

If you are storing BLOBs etc. in your table then getting count(*)'s performance can be really bad.

Aayush Puri