[英]php & mySQL: Which of the following query is faster? Why?
Query 1: 查询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: 查询2:
$q = "SELECT COUNT(*) FROM books
INNER JOIN books_type ON books.id = books_type.id
WHERE books_type.book_type = 'Comedy'";
Query 3: 查询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. 谢谢。
You can try EXPLAIN query_here
to find out. 您可以尝试在
EXPLAIN query_here
进行查找。
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: MySQL手册中有关
EXPLAIN
语句的更多信息:
"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" “当在SELECT语句前加上关键字EXPLAIN时,MySQL将显示来自优化程序的有关查询执行计划的信息。也就是说,MySQL解释了它将如何处理SELECT,包括有关如何联接表以及以何种顺序进行联接的信息”
http://dev.mysql.com/doc/refman/5.0/en/using-explain.html http://dev.mysql.com/doc/refman/5.0/en/using-explain.html
I also recommend this tutorial for optimizing MySQL queries in Database Journal: 我还建议使用本教程来优化Database Journal中的MySQL查询:
http://www.databasejournal.com/features/mysql/article.php/1382791/Optimizing-MySQL-Queries-and-Indexes.htm http://www.databasejournal.com/features/mysql/article.php/1382791/Optimizing-MySQL-Queries-and-Indexes.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.