我有一个按查询分组,只返回1行,有4列。当我运行这个选择查询时需要1-2分钟,因为此查询中涉及的表非常庞大(其中一个表有6000万条记录)但是当我在insert语句中使用此查询时,大约需要10分钟或更长时间。 / p>
有人可以说出背后的原因吗?我只插入一行,需要1分钟的时间,但为什么需要10分钟?还请提供一些建议来解决这类问题,因为搜索我主要是找到更快的批量插入方法。以下是审核的查询结构
select col1, count(loan), sum(A), sum(B)
from
(select 'dummy' as col1, X.loan, BT.A, BT.B,
row_number() over (partition by X.loan order by BT.C desc) rnum
from Big_Table BT, X
where X.loan = BT.loan
and X.date< "some date"
and X.date>"another date")
where rnum =1
and B> "some value"
group by col1
对于Big_Table索引在列loan和C.
答案 0 :(得分:0)
此查询可以更快地获得相同的结果:
SELECT "dummy", COUNT(BT.loan), SUM(BT.A), SUM(BT.B)
FROM Big_Table BT
JOIN (
SELECT X.loan, max(BT.C) as C
FROM X
JOIN Big_Table BT ON X.loan = BT.loan
WHERE X.date > "some date"
AND X.date < "another date"
GROUP BY X.loan ) partitioned
ON BT.loan = partitioned.loan AND BT.C = partitioned.C
WHERE BT.B > "some value"
并确保您在loan
和date
的X上以及loan
,C
和B
的Big_Table上有索引。