简体   繁体   English

如何提高此SELECT查询的性能

[英]How to improve performance of this SELECT query JOIN

I have a query which seems to be taking a long time to run on occasion. 我有一个查询,有时要花很长时间才能运行。 The slowness may be unrelated but I wanted to check what could be done to make this more efficient. 速度的变化可能无关紧要,但我想检查一下可以做些什么来提高效率。

The user table has about 40k rows. 用户表大约有4万行。 The code table has about 30k rows. 代码表大约有3万行。 user_id and code are unique values. user_id和代码是唯一值。

SELECT * 
FROM `user`, code 
WHERE `user`.user_id = code.user_id 
AND code.code = '50816ef96210415d1cad824bdb43';

I have an index setup on the code.user_id field. 我在code.user_id字段上设置了索引。 Anything else I can do? 我还能做什么? Should I have other indexes in place here? 我应该在这里设置其他索引吗?

Output from EXPLAIN on that query: 来自该查询的EXPLAIN的输出:

>> +----+-------------+-------+--------+---------------+---------+---------+----------------------+-------+-------------+
>> | id | select_type | table | type   | possible_keys | key     | key_len | ref                 | rows  | Extra       |
>> +----+-------------+-------+--------+---------------+---------+---------+----------------------+-------+-------------+
>> |  1 | SIMPLE      | code | ALL    | user_id       | NULL    | NULL    | NULL                 35696 | Using where | 
>> |  1 | SIMPLE      | user  | eq_ref | PRIMARY       | PRIMARY | 4       | mydb.code.user_id |     1 |             | 
>> +----+-------------+-------+--------+---------------+---------+---------+----------------------+-------+-------------+
>> 2 rows in set (10.11 sec)

您还需要在code.codeuser.user_id字段上添加索引,它应该开始运行

除了在代码中添加索引之外,您可以做的另一件事是仅选择所需的列(我不喜欢使用SELECT *)

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

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