我有以下Oracle查询,可以快速执行(几秒钟内):
// generate range of values from 1 to 20:
int values[20];
std::iota(std::begin(values), std::end(values), 1);
// randomize the values:
std::random_shuffle(std::begin(values), std::end(values));
// select the first 8:
int tabela[8];
std::copy_n(std::begin(values), 8, std::begin(tabela));
旅行'表有大量行(超过2000万) 现在,我想使用以下命令在表中插入此查询的结果:
select contract_id_fk as ct,
max(trip_id) as tid,
max(consumed_mileage) as cum
from trip
where to_date > to_date('20-12-2016','DD-MM-YYYY')
and contract_id_fk is not null
and vehicle_id_fk is not null
and trip_stop_status is null
group by contract_id_fk
这非常慢。交易超时这么慢(在我的情况下超过30秒)。知道为什么这么慢,以及如何优化它?
答案 0 :(得分:1)
尝试用光标选择,并插入到循环中。就这样;
DECLARE
CURSOR SCURSOR IS
select contract_id_fk as ct,
max(trip_id) as tid,
max(consumed_mileage) as cum
from trip
where to_date > to_date('20-12-2016','DD-MM-YYYY')
and contract_id_fk is not null
and vehicle_id_fk is not null
and trip_stop_status is null
group by contract_id_fk ;
BEGIN
FOR RECS IN SCURSOR
LOOP
INSERT INTO lst
SELECT RECS.ct , RECS.tid , RECS.cum FROM DUAL;
COMMIT;
END LOOP;
END;
答案 1 :(得分:0)
很难说出原因是什么,因为可能存在多种因素,例如:
我建议的两件事:
INSERT /*+ APPEND */ INTO lst
select contract_id_fk as ct,
max(trip_id) as tid, max(consumed_mileage) as cum
from trip where to_date > to_date('20-12-2016','DD-MM-YYYY')
and contract_id_fk is not null and vehicle_id_fk is not null and
trip_stop_status is null group by contract_id_fk
答案 2 :(得分:0)
检查缓冲兑现,我认为问题可能出在缓冲兑现上 或尝试收集表格统计信息
答案 3 :(得分:0)
由于他们仍然是这个将近4年的问题的答案... IO(AWS上的RDS实例...)饱和缓慢的根本原因,再加上太多的按组/排序。最重要的是数据库建模问题。 感谢所有尝试提供帮助的人!