[英]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个主要错误:
您应该将索引添加到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: 您可以执行以下操作:
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.