简体   繁体   English

php&mySQL:以下哪个查询更快? 为什么?

[英]php & mySQL: Which of the following query is faster? Why?

  1. Which of the following 3 Queries will be faster? 以下3个查询中的哪个查询会更快? 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: 查询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

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. 根据它,第二个应该最快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM