【发布时间】:2018-09-27 21:19:22
【问题描述】:
根据以下查询的数据量,我的返回时间很慢。
mysql> explain select *
from worker_location
where gate_id not in (
SELECT gate_id from worker_address
);
+----+--------------------+---------------------+-------+---------------------------+---------------------------+---------+------+---------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+---------------------+-------+---------------------------+---------------------------+---------+------+---------+--------------------------+
| 1 | PRIMARY | worker_location | ALL | NULL | NULL | NULL | NULL | 527347 | Using where |
| 2 | DEPENDENT SUBQUERY | worker_address | index | gate_id_idx | gate_id_ix | 48 | NULL | 3041342 | Using where; Using index |
+----+--------------------+---------------------+-------+---------------------------+---------------------------+---------+------+---------+--------------------------+
2 rows in set (0.00 sec)
我尝试使用左连接,但得到了相同的计划,但速度没有任何好处。
mysql> explain select *
from worker_location wl
left join worker_address wa ON wl.gate_id=wa.gate_id
where wa.gate_id is null;
+----+-------------+-------+------+---------------+------+---------+------+---------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+----------------------------------------------------+
| 1 | SIMPLE | wl | ALL | NULL | NULL | NULL | NULL | 527347 | NULL |
| 1 | SIMPLE | wa | ALL | NULL | NULL | NULL | NULL | 3041342 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+------+---------+------+---------+----------------------------------------------------+
2 rows in set (0.00 sec)
有没有办法进一步优化这个查询?
【问题讨论】:
-
确保只选择您需要的列。不要使用 * 并获取您不需要的不必要的列。
-
所有列都需要。
-
你有
worker_location.gate_id列的索引吗? -
是的,所有列都已编入索引。
-
无能为力。可以尝试增加会话join_buffer_size,BNL和BKA
标签: mysql optimization sql-tuning