简体   繁体   English

哪个MySql行更快:

[英]Which MySql line is faster:

I have a classified_id variable which matches one document in a MySql table. 我有一个classified_id变量,它与MySql表中的一个文档匹配。

I am currently fetching the information about that one record like this: 我目前正在像这样获取有关该记录的信息:

   SELECT * FROM table WHERE table.classified_id = $classified_id

I wonder if there is a faster approach, for example like this: 我想知道是否有更快的方法,例如:

   SELECT 1 FROM table WHERE table.classified_id = $classified_id

Wont the last one only select 1 record, which is exactly what I need, so that it doesn't have to scan the entire table but instead stops searching for records after 1 is found? 不希望最后一个仅选择1条记录,这正是我所需要的,因此它不必扫描整个表,而是在找到1条记录后停止搜索记录?

Or am I dreaming this? 还是我在做梦?

Thanks 谢谢

您想使用LIMIT

SELECT * FROM table WHERE table.classified_id = $classified_id LIMIT 1

Yes, you're dreaming this. 是的,你在做梦。
There are 2 major faults in your reasoning: 您的推理有2个主要错误:

  1. The question itself. 问题本身。 Most newbies fail to that hole though. 不过,大多数新手都无法做到这一点。 They ask "what if I do something - will it be faster?". 他们问:“我该怎么做-会更快吗?”。 But the proper question is "My application Runs slow. How to find a bottleneck?" 但是正确的问题是“我的应用程序运行缓慢。如何找到瓶颈?” and "I have certain bottleneck. How to eliminate it?" 和“我有一定的瓶颈。如何消除它?”
  2. You suppose fieldlist part influencing database search. 您假设字段列表部分影响数据库搜索。 That's wrong. 错了 The field list is responsible only for the returned fields of the found rows. 字段列表仅负责找到的行的返回字段。 In both cases the number of rows would be the same. 在这两种情况下,行数都是相同的。

您应该将索引添加到classified_id列,以避免进行表扫描。

CREATE INDEX classified_idx ON table (classified_id);

Why don't you try it? 你为什么不尝试呢?

SELECT 1 FROM table;

returns 退货

+---+
| 1 |
+---+
| 1 | 
| 1 | 
| 1 | 
| 1 | 
| 1 | 
| 1 | 
+---+
6 rows in set (0.00 sec)

which is a computed column, a constant value of one in this case (for all 6 rows in my test case). 这是一个计算列,在这种情况下为1的常量值(对于我的测试用例中的所有6行)。

For your question 你的问题

SELECT * FROM table WHERE table.classified_id = $classified_id

this is the fastest way to retrieve data (assuming that you need all the columns from the table) 这是检索数据的最快方法(假设您需要表中的所有列)

There are following things that you can do: 您可以执行以下操作:

  • check if there is an index on classified_id, if there is and if using the index is faster the database will use the index and not scan the whole table, so you'll get what you want (using index can be slower if there are just a few records or if a high percentage of records satisfy a criteria, but having index will not bring any penalty to reading data, database will chose the best way to retrieve it) 检查是否在classified_id上有一个索引,如果有,并且使用索引的速度更快,数据库将使用该索引而不扫描整个表,因此您将获得所需的内容(如果存在索引,则使用索引的速度可能会变慢几条记录,或者大部分记录满足条件,但是具有索引不会对读取数据造成任何损失,数据库将选择检索它的最佳方法)
  • if you don't need all the columns from the table then specify exactly which ones you need 如果您不需要表中的所有列,请确切指定所需的列
  • if there is more then one record that satisfy the criteria you can use LIMIT keyword to get only one or only a few records 如果有多条记录满足条件,则可以使用LIMIT关键字仅获得一个或几个记录

other then this for such a simple query the only next step would be to partition the table to several hard drives (which might not be an option on your system). 否则,对于这样一个简单的查询,唯一的下一步就是将表分区到几个硬盘驱动器上(这可能不是系统上的选项)。

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

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