我有这个查询:
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
),大约有 80 行 in organizations
, 400 in fleets
, 2900 in vessels
, 3000 inlicences
和 10 inusers
这个查询甚至没有成功,它卡在copying to temp table
我应该如何重新处理这个查询以使其工作(快速)?
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