我有下表: fpt_action_handling(3GB大,包含10.000.000条记录
id | name | action_id | action_definition | sortorder |
------ | ------ | ----------- | ------ | ------ |
UUID() | name | id of table "actions" | UUID() | INT(11) |
id,action_id,sortorder有一个索引
在每个action_id中,我想要具有最高排序顺序(rank)的action_definition。
我写了以下查询:
select fa.id, fa.action_id
from fpt_action_handling fa
inner join (select action_id, max(sortorder) max_sortorder from ftp_action_handling group by action_id) ma
on fa.action_id = ma.action_id
and fa.sortorder = ma.max_sortorder
查询需要5分钟才能完成,它会在服务器上产生巨大的负载。
如何摆脱max()和GROUP BY,而不会失去获得action_id的要求,每个action_id具有最高排序顺序? (因为action_id列不是唯一的
答案 0 :(得分:0)
尝试外部联接:
SELECT a.id, a.action_id
FROM fpt_action_handling a
LEFT JOIN fpt_action_handling b
ON b.action_id = a.action_id
AND b.sortorder > a.sortorder
WHERE b.action_id IS NULL