我有一个查询:
SELECT
a.id,
a.name,
count(b.id),
count(c.id),
count(e.id),
count(f.id)
FROM
organizations a
LEFT JOIN vessels b ON a.id = b.organization_id
LEFT JOIN licences c ON a.id = c.organization_id
LEFT JOIN fleets e ON a.id = e.organization_id
LEFT JOIN users f ON a.id = f.organization_id
GROUP BY a.id;
在所有的表中都有一个适当的索引(在主键和“organization_id”上),在“organizations”表中有大约80行,在“fleets”表中有400行,在“vessels”表中有2900行,在“licenses”表中有3000行,在“users”表中有10行。
这个查询甚至不能成功,它被卡住在“拷贝到临时表”的过程中。
如何重新设计这个查询以使其工作(更快)?
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE a index PRIMARY 4 1
1 SIMPLE b ref organisation_id organisation_id 4 fuel.a.id 70 Using index
1 SIMPLE c ref organisation_id organisation_id 4 fuel.a.id 15 Using index
1 SIMPLE e ref organisation_id organisation_id 4 fuel.a.id 5
1 SIMPLE f ref organization_id organization_id 5 fuel.a.id 1 Using index
EXPLAIN
- h2oooooooa.name
添加到你的GROUP BY
子句中吗? - BellevueBobfleets
时没有使用索引? - h2ooooooo