KILL 後業務恢復,需根治SELECT tradedto0_.*
FROM a1 tradedto0_
WHERE tradedto0_.tradestatus='1'
AND tradedto0_.tradeoid IN (
SELECT orderdto1_.tradeoid
FROM a2 orderdto1_
WHERE orderdto1_.proname LIKE '%??%'
OR orderdto1_.procode LIKE '%??%'
)
AND tradedto0_.undefine4='1'
AND tradedto0_.invoicetype='1'
AND tradedto0_.tradestep='0'
AND tradedto0_.orderCompany LIKE '0002%'
ORDER BY tradedto0_.tradesign ASC, tradedto0_.makertime DESC LIMIT 15;
+----+--------------------+------------+------+---------------+------+---------+------+-------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------+------+---------------+------+---------+------+-------+--------------------------+ | 1 | PRIMARY | tradedto0_ | ALL | NULL | NULL | NULL | NULL | 27454 | Using where; Using filesort | | 2 | DEPENDENT SUBQUERY | orderdto1_ | ALL | NULL | NULL | NULL | NULL | 40998 | Using where | +----+--------------------+------------+------+---------------+------+---------+------+-------+--------------------------+
雙表全掃描,預估關聯 27,454 × 40,998 ≈ 11 億次,filesort 加劇災難。
(proname, procode, tradeoid) 失敗:proname VARCHAR(1000) 超過 1000 位元組限制proname VARCHAR(156) 後建立索引,子查詢走 Using index,但雙表掃描行數未降(8962 × 41005)SELECT tradedto0_.*
FROM a1 tradedto0_,
(SELECT orderdto1_.tradeoid
FROM a2 orderdto1_
WHERE orderdto1_.proname LIKE '%??%'
OR orderdto1_.procode LIKE '%??%') t2
WHERE tradedto0_.tradestatus='1'
AND tradedto0_.tradeoid = t2.tradeoid
AND tradedto0_.undefine4='1'
AND tradedto0_.invoicetype='1'
AND tradedto0_.tradestep='0'
AND tradedto0_.orderCompany LIKE '0002%'
ORDER BY tradedto0_.tradesign ASC, tradedto0_.makertime DESC LIMIT 15;
+----+-------------+------------+-------+---------------+----------+---------+------+------+--------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+-------+---------------+----------+---------+------+------+--------------------------------+ | 1 | PRIMARY || ALL | NULL | NULL | NULL | NULL | 0 | Impossible WHERE noticed... | | 2 | DERIVED | orderdto1_ | index | NULL | ind_a2 | 777 | NULL | 41005| Using where; Using index | +----+-------------+------------+-------+---------------+----------+---------+------+------+--------------------------------+
Impossible WHERE),直接返回空集DEPENDENT SUBQUERY 是效能殺手,優先改寫為 DERIVED(衍生表)或 JOINLIKE '%...%' 仍需全索引掃描,無法根治