【发布时间】:2023-04-01 09:23:02
【问题描述】:
我有以下问题
SELECT a.id, b.id from table1 AS a, table2 AS b
WHERE a.table2_id IS NULL
AND a.plane = SUBSTRING(b.imb, 1, 20)
AND (a.stat LIKE "f%" OR a.stat LIKE "F%")
这是 EXPLAIN 的输出
+----+-------------+-------+------+------------ -------------------------------------------------- -----------------------------------------+-------- ----------+---------+------+----------+----------- --+ |编号 |选择类型 |表|类型 |可能的键 |关键 | key_len |参考 |行 |额外 | +----+-------------+--------+------+--------------- -------------------------------------------------- --------------------------+------------------------ -------+---------+------+----------+-------------+ | 1 |简单 |乙 |全部 |空 |空 |空 |空 | 28578039 | | | 1 |简单 |一个 |参考 | index_on_plane,index_on_table2_id_id,mysql_confirmstat_on_stat | index_on_plane | 258 |功能| 2 |使用位置 | +----+-------------+--------+------+--------------- -------------------------------------------------- --------------------------+------------------------ -------+---------+------+----------+-------------+
执行查询大约需要 80 分钟。
table1上的索引如下
+--------------+------------+---------- ------------+-------------+--------------+--------- --+-------------+----------+--------+------+------ ------+---------+----------------+ |表 |非唯一 |键名 | Seq_in_index |列名 |整理 |基数|子部分 |包装 |空 |索引类型 |评论 |索引评论 | +--------------+------------+---------- ------------+-------------+--------------+--------- --+-------------+----------+--------+------+------ ------+---------+----------------+ |表1 | 0 |初级 | 1 |编号 |一个 | 50319117 |空 |空 | | BTREE | | | |表1 | 1 | index_on_post | 1 |发布 |一个 | 7188445 |空 |空 |是 | BTREE | | | |表1 | 1 | index_on_plane | 1 |飞机|一个 | 25159558 |空 |空 |是 | BTREE | | | |表1 | 1 | index_on_table2_id | 1 | table2_id |一个 | 25159558 |空 |空 |是 | BTREE | | | |表1 | 1 | index_on_stat | 1 |统计 |一个 | 187 |空 |空 |是 | BTREE | | | +--------------+------------+---------- ------------+-------------+--------------+--------- --+-------------+----------+--------+------+------ ------+---------+----------------+和 table2 索引是。
+-------+------------+----------------------------+ --------------+-------------+------------+--------- ----+----------+--------+------+------------+-- ----+---------------+ |表 |非唯一 |键名 | Seq_in_index |列名 |整理 |基数|子部分 |包装 |空 |索引类型 |评论 |索引评论 | +-------+------------+----------------------------+ --------------+-------------+------------+--------- ----+----------+--------+------+------------+-- ----+---------------+ |表2 | 0 |初级 | 1 |编号 |一个 | 28578039 |空 |空 | | BTREE | | | |表2 | 1 | index_on_post | 1 |发布 |一个 | 28578039 |空 |空 |是 | BTREE | | | |表2 | 1 | index_on_ver | 1 |版本 |一个 |第1371章空 |空 |是 | BTREE | | | |表2 | 1 | index_on_imb | 1 | IMB |一个 | 28578039 |空 |空 |是 | BTREE | | | +-------+------------+----------------------------+ --------------+-------------+------------+--------- ----+----------+--------+------+------------+-- ----+---------------+如何提高这个查询的执行时间?
这里是更新的解释
EXPLAIN SELECT STRAIGHT_JOIN a.id, b.id from table1 AS a JOIN b AS b
ON a.plane=substring(b.imb,1,20)
WHERE a.table2_id IS NULL
AND (a.stat LIKE "f%" OR a.stat LIKE "F%");
+----+-------------+--------+------+--------------- -------------------------------------------------- --------------------------+------------------------ --------+---------+--------+----------+------------ ------------------+
|编号 |选择类型 |表|类型 |可能的键 |关键 | key_len |参考 |行 |额外 |
+----+-------------+--------+------+--------------- -------------------------------------------------- --------------------------+------------------------ --------+---------+--------+----------+------------ ------------------+
| 1 |简单 |一个 |参考 | index_on_plane,index_on_table2_id,index_on_stat | index_on_table2_id | 5 |常量 | 500543 |使用位置 |
| 1 |简单 |乙 |全部 |空 |空 |空 |空 | 28578039 |使用哪里;使用连接缓冲区 |
+----+-------------+--------+------+--------------- -------------------------------------------------- --------------------------+------------------------ --------+---------+--------+----------+------------ ------------------+
SQL 小提琴链接http://www.sqlfiddle.com/#!2/362a6/4
【问题讨论】:
-
尝试加入类似 ` ... from table1 a left join table2 b on a.plane=substring(b.imb,...) 的表 - 你会收到什么?
-
在解释结果中,S和CS是什么意思。分别是table1和table2吗?
-
您可以尝试在 table2 上为 table2.imb 的前 20 个字符建立索引。 stackoverflow.com/questions/10595037/…
-
@ursitesion 更新了问题
-
@BrianHoover 在 table2 中的 imb 上已有索引
标签: mysql performance query-optimization