我有以下查询
SELECT STRAIGHT_JOIN a.*
FROM b
INNER JOIN a ON a.b_id = b.b_id
INNER JOIN c ON dd.a_id = a.id
WHERE a.a_type_id = 164
AND a.status = 1
AND NOT EXISTS (
SELECT a2.*
FROM a a2
INNER JOIN c c2 ON c2.a_id = a2.id
INNER JOIN d d.a_id = a2.id
WHERE a.w_id = 567
AND c.ds_id = c2.ds_id
)
order by b.id
LIMIT 1;
解释给出以下结果
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: b
type: index
possible_keys: index_b_on_b_id
key: PRIMARY
key_len: 4
ref: NULL
rows: 1
Extra:
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: a
type: ref
possible_keys: PRIMARY,index_a_on_a_type_id,index_a_on_status,index_a_on_b_id,index_a_on_a_type_id_and_status
key: index_a_on_b_id
key_len: 4
ref: b.b_id
rows: 1
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: PRIMARY
table: c
type: ref
possible_keys: index_c_on_a_id
key: index_c_on_a_id
key_len: 4
ref: a.id
rows: 1
Extra: Using where
*************************** 4. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: c2
type: ref
possible_keys: index_c_on_ds_id,index_c_on_a_id
key: index_c_on_ds_id
key_len: 4
ref: c.ds_id
rows: 1
Extra:
*************************** 5. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: d
type: ref
possible_keys: index_d_on_a_id,index_d_on_w_id
key: index_d_on_a_id
key_len: 5
ref: c2.a_id
rows: 1
Extra: Using where
*************************** 6. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: a2
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: d.a_id
rows: 1
Extra: Using where; Using index
从解释结果来看,要扫描的行只有一个,但实际执行时间超过一秒。我知道解释只是一个估计,实际执行可能会有所不同,但这里有什么可以优化的(添加新索引、重写查询等)?
表中的行数如下:
一个 => 2585530
b => 10000
c => 2585525
d => 3350381