tags:

views:

61

answers:

3
  1. Which of the following 3 Queries will be faster? Why?
  2. I use such queries with a lot of other joins a lot within my app. so is there any way I can benchmark their speed? If yes, can you please mention what it is/they are?

Query 1:

$q = "SELECT COUNT(books.id) FROM books 
INNER JOIN books_type ON books.id = books_type.id
WHERE books_type.book_type = 'Comedy'";

Query 2:

$q = "SELECT COUNT(*) FROM books 
INNER JOIN books_type ON books.id = books_type.id
WHERE books_type.book_type = 'Comedy'";

Query 3:

$q = "SELECT books.id FROM books 
INNER JOIN books_type ON books.id = books_type.id
WHERE books_type.book_type = 'Comedy'";

$books_count = mysql_num_rows($q);

Thank you.

+2  A: 

You can try EXPLAIN query_here to find out.

For example:

EXPLAIN SELECT books.id FROM books 
INNER JOIN books_type ON books.id = books_type.id
WHERE books_type = 'Comedy'

This will give you some information on each query and how they perform. More information in the MySQL manual for the EXPLAIN statement:

"When you precede a SELECT statement with the keyword EXPLAIN, MySQL displays information from the optimizer about the query execution plan. That is, MySQL explains how it would process the SELECT, including information about how tables are joined and in which order"

http://dev.mysql.com/doc/refman/5.0/en/using-explain.html

I also recommend this tutorial for optimizing MySQL queries in Database Journal:

http://www.databasejournal.com/features/mysql/article.php/1382791/Optimizing-MySQL-Queries-and-Indexes.htm

mga
+1  A: 

Even though you can easily test it yourself, here's an article that goes into the why's. According to it, the second one should be fastest.

Raveren
+1 Thank you for the helpful reference.
Devner
The article doesn't explain the behavior of Count() for joins. And though `Count(*)` and `Count(books.id)` have a different meaning in this case they have the same result. Because of `INNER JOIN ... ON books.id = books_type.id` there can be no is_null(books.id) results.
VolkerK
In this case they do have the same result indeed. Because books.id is a primary key (I assume) mysql doesn't have to check all rows for NOT NULL.Although there behaviour is the same count(*) is still better. It is meant to counts rows and that's what you want to do.
douwe
A: 

@Raveren, wouldn't the first one be fastest? Having to only retrieve counts of one field rather than all.

henasraf
Unless you're posting an answer, write comments. I can't even wrap my head around your question though. Perhaps just read the linked article for starters.
Raveren
No, count(expr) only counts records where _expr_ is not NULL. If the MySQL optimizer cannot determine that this is true for all records additional tests must be performed. So count(expr) is potentially slower than count(*) (besides the possible different results)
VolkerK
Sorry - don't have the ability to post comments yet (except for apparently, this one). :/ @VolkerK - ah I see, thanks for clarifying that.
henasraf