优化插入选择查询Oracle

问题描述 投票:1回答:1

嗨,我需要优化下面的SQL查询。

insert into exa_table (column1, column2, column3, column4)
select value1, value2, value3, value4 from 
(select tb2.ID, tb2.PARCELNO, tb2.SHP_ID, tb4.CUST_ID
from exa_table2 tb2 
join table3 tb3 on tb2.ID = tb3.ID 
join table4 tb4 on tb3.ID = tb4.ID 
where tb2.STATUS='1' and tb2.ACTIVE='1' and tb2.DATE >= '20180924' AND tb2.SDATE < '20181024' and 
tb4.STATUS='1' and tb4.ACTIVE='1' and 
not exists (select 1 from exa_table Q where Q.ID = tb2.ID));

我已经尝试通过添加APPEND NOLOGGING和PARALLEL来优化查询,就像这样

insert /*+ APPEND NOLOGGING */ into exa_table (column1, column2, column3, column4)
select value1, value2, value3, value4 from 
(select /*+ PARALLEL(4) */ tb2.ID, tb2.PARCELNO, tb2.SHP_ID, tb4.CUST_ID
from exa_table2 tb2 
join table3 tb3 on tb2.ID = tb3.ID 
join table4 tb4 on tb3.ID = tb4.ID 
where tb2.STATUS='1' and tb2.ACTIVE='1' and tb2.DATE >= '20180924' AND tb2.SDATE < '20181024' and 
tb4.STATUS='1' and tb4.ACTIVE='1' and 
not exists (select 1 from exa_table Q where Q.ID = tb2.ID));

它现在好多了但仍然不够 - 花了13分钟插入~100k行

解释计划:enter image description here

您有任何想法如何改善查询?

sql oracle performance query-optimization insert-select
1个回答
0
投票

根据执行计划,您将在SELECT语句中显示瓶颈。你没有回答关于select的选择性,所以我假设它低于5%,因此你应该使用索引。

我将从创建以下索引开始:

create index ix1 on exa_table2 (STATUS, ACTIVE, DATE, SDATE, ID);

如果选择性低,则该指数将改善性能。首先尝试它,没有任何并行性,看它的表现如何。一旦你发现它表现良好,那么你就可以投入更多的硬件。

此外,了解以下每种过滤条件的个别(单独)选择性非常重要:

  • STATUS = '1'
  • ACTIVE = '1'
  • DATE >= '20180924'
  • SDATE < '20181024'

新创建的索引中的列的排序对于查询的性能而言可能非常重要,并且它在很大程度上取决于这些选择性。

© www.soinside.com 2019 - 2024. All rights reserved.