我将Oracle用作DBMS,并且具有大表(400,000,000条记录)。现在,我想检索每个类别的最后一条记录。当我如下所示使用“分组依据”时,需要花费很长时间。有没有更快的方法?
注意:我需要检索表的所有数据:
select *
from myTable tb1
inner join
(select MAX(id) max_id, categoryColumn
from myTable
group by categoryColumn) tb2 on tb1.id = tb2.max_id
答案 0 :(得分:4)
您可以尝试窗口功能:
select t.*
from (select t.*,
row_number() over (partition by categoryColumn order by id desc) as seqnum
from mytable t
) t
where seqnum = 1;
我建议在(categoryColumn, id desc)
上建立索引。
使用此索引,您可能会发现相关子查询更快:
select t.*
from mytable t
where t.id = (select max(t2.id)
from mytable t2
where t2.categoryColumn = t.categoryColumn
);
尽管我认为上述操作速度更快,但是使用keep
的表现还是不错的。语法更麻烦:
select categoryColumn, max(id) as id,
max(col1) keep (dense_rank first order by id desc) as col1,
max(col2) keep (dense_rank first order by id desc) as col2,
. . . -- and so on for each column
from mytable t
group by categoryColumn;