Sql 性能调优查询
我必须调整查询的性能。我的查询是动态的,如下所示Sql 性能调优查询,sql,oracle,plsql,Sql,Oracle,Plsql,我必须调整查询的性能。我的查询是动态的,如下所示 store_1:='select item_number from Tab_1 where subclass_id= ' || part_class_id || ' and category_id= '|| part_type_id || ' and delete_flag=0 and text11 != ''' || i.eccn_old ||
store_1:='select item_number from Tab_1
where subclass_id= ' || part_class_id || '
and category_id= '|| part_type_id || '
and delete_flag=0
and text11 != ''' || i.eccn_old ||'''' ;
open cursor for store_1;
loop
fetch cursor into record;
exit when record%notfound;
select count(1) into variable from Tab_1 a, Tab_2 b
where a.id=b.id
and a.item_number=record
and some condition;
if (variable>0) then
insert into table tab_3 values(record);
commit;
end if;
end loop;
The time taken by above code in approx 1 min for 150 Cursor records.
表Tab_1(1495093)行和Tab_2(6580252)行的大小较大
此外,store_1的输出将有100多行,但同样取决于条件
但在极少数情况下会超过5000
我试图通过创建全局临时表“tab_4”并将其与“tab_2”表连接,并直接插入到我的“tab_3”表中,来调整整个过程
我的尝试如下
store_1:='insert into tab_4 select item_number from tab_1
where subclass_id= ' || part_class_id || '
and category_id= ' || part_type_id || '
and delete_flag=0 and text11 != ''' || i.eccn_old ||'''' ;
execute immediate store_1;
commit;
insert into tab_3 select b.item_number from tab_1 a, tab_4 b
where a.item_number=b.item_number
and b.release_date> somedate
and b.delete_flag=0
and (b.release_type =974 or b.release_type =975);
我原以为join table可以解决这个问题,但不幸的是,我的过程甚至需要更多的时间
The time taken was approx 3 min for 150 Cursor Records
请指导我修改代码,这样我的大概时间至少减少50-60%
注意:创建索引可能需要一段时间,所以请耐心等待。首先为什么要使用动态SQL?您发布的示例没有做任何需要使用动态SQL的事情,甚至没有从中受益。如果要使用动态SQL,是否有不使用绑定变量的原因?您首先使用PL/SQL而不是编写一条
INSERT
语句,这有什么原因吗。。。您是否尝试在所有插入完成后放入提交。我不确定Oracle,但其他引擎在一次最终提交时的性能要高得多。至少在您最初的查询中,这可能会有所不同。由于数据集很大,整个过程花费了7个多小时,因此我将commit放在跟踪插入的最新行号上,以便在会话突然结束时可以从该特定行号开始。但是ya point认为我的过程在代码进入生产时不会在每次插入后都进行提交。Justin:Store_1查询在不同的场景中不断变化,用户可能不知道的表和列可能很少,但名称在某些条件下是从dictionary表派生的,Store_1可能包括这样的内容因此,联接和where条件查询的表和列必须使用动态语句。我没有看到绑定变量的任何机会,因此没有使用任何。好的,如果你看到任何,请分享。谢谢
The time taken was approx 3 min for 150 Cursor Records