简体   繁体   English

哪个mysql选择更好/更快?

[英]Which mysql select is better/faster?

Is there a preferred way? 有首选方式吗? There are currently 1640 rows in the game table to go through and will be adding about 1200 every year. 目前game表中有1640行要进行,每年将增加1200行。

SELECT `date` FROM `game` WHERE `date`<'2009-11-09' ORDER BY `date` DESC LIMIT 1;

0.0004 seconds 0.0004秒

SELECT MAX(`date`) AS `date` FROM `game` WHERE `date`<'2009-11-09' LIMIT 1;

0.0006 seconds 0.0006秒

The speeds were for the first time this ran. 速度是第一次这样。 Each time after was 0.0002 for each. 每次之后每次0.0002。

mySQL: MySQL的:
Server: Localhost via UNIX socket 服务器:通过UNIX套接字的Localhost
Server version: 5.1.37 服务器版本:5.1.37

PHP (shouldn't be relevant): PHP(不应该相关):
5.x 5.x的

Apply MySQL EXPLAIN and check the query plans. 应用MySQL EXPLAIN并检查查询计划。 The second example may well have a cleaner plan, which is nice as it looks cleaner to my eye. 第二个例子可能有一个更清洁的计划,这很好看,因为它看起来更干净。 (Once you remove the limit.) (一旦删除限制。)

A noddy test locally shows (no indexes). 本地显示一个点头测试(没有索引)。

Query 1: 查询1:

EXPLAIN
SELECT  datex
FROM    TABLE_X x
WHERE   datex < "2009-10-20"
ORDER BY datex DESC
LIMIT 1

Plan 计划

id select_type table type possible_keys key  key_len ref  rows Extra
1  SIMPLE      x     ALL  NULL          NULL NULL    NULL 2    Using where; Using filesort

Query 2: 查询2:

EXPLAIN
SELECT  MAX( datex )
FROM    TABLE_X x
WHERE   datex < "2009-10-20"

Plan 计划

id  select_type table type  possible_keys   key     key_len ref   rows Extra
1   SIMPLE      x     ALL   NULL            NULL    NULL    NULL  2    Using where

With those numbers of rows and the simplicity of the queries, it should not matter. 使用这些行数和查询的简单性,它应该无关紧要。 You also do not need the limit on the second query. 您也不需要第二个查询的限制。 Just choose whichever one is easier for you to understand. 只需选择一个更容易理解的内容。

In second query you don't have to add LIMIT 1 . 在第二个查询中,您不必添加LIMIT 1 It will always return 1 row. 它总是返回1行。 I would say that second query is more readable and you should use. 我会说第二个查询更具可读性 ,你应该使用。 But I agree with @jle. 但我同意@jle。 You have very small database and it really does not affect performance very much. 你有一个非常小的数据库,它确实不会影响性能。

You can look at what the plan is (ie how mysql does the work) using explain - http://dev.mysql.com/doc/refman/5.0/en/explain.html 你可以使用解释来查看计划是什么(即mysql如何工作) - http://dev.mysql.com/doc/refman/5.0/en/explain.html

I imagine they are pretty much identical, but that's the best way to check (it may depend on indices etc). 我想它们几乎完全相同,但这是检查的最佳方式(可能取决于索引等)。

But I have to ask - do you really need to worry about this? 但我不得不问 - 你真的需要担心吗? It's not exactly slow, and the table isn't growing at a huge rate. 它并不是很慢,桌子也没有以极快的速度增长。

Finally, you don't need the second "limit". 最后,您不需要第二个“限制”。

I would also want to add to all the other responses: 我还想添加所有其他响应:

try to add a million records. 尝试添加一百万条记录。 Best way to know for sure. 最好的方式来确定。

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

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