–》对应的sql语句如下: select document.period_key, document.cycle_seq_no, document.ba_no, document.customer_no, bill_statement.pay_channel_no from document, –千万数据量 12671016 rows cycle_control, –数据字典表,2118 rows bill_statement, –千万数据量 12671016 rows cyc_payer_pop –百万数据量 5400326 rows where cycle_control.cycle_code = 2 and cycle_control.cycle_instance = 7 and cycle_control.cycle_year = 2014 and cyc_payer_pop.cycle_seq_no = cycle_control.cycle_seq_no and cyc_payer_pop.db_status = ‘BL’ and document.ba_no = cyc_payer_pop.ba_no and document.cycle_seq_no = cyc_payer_pop.cycle_seq_no and document.cycle_seq_run = cyc_payer_pop.cycle_seq_run and document.period_key = cyc_payer_pop.period_key and document.customer_key = cyc_payer_pop.customer_key and document.doc_produce_ind in (‘Y ‘, ‘ E ‘) and document.document_status != ‘ N’ and bill_statement.ba_no = cyc_payer_pop.ba_no and bill_statement.cycle_seq_no = document.cycle_seq_no and bill_statement.cycle_seq_run = document.cycle_seq_run and bill_statement.period_key = cyc_payer_pop.period_key and bill_statement.customer_key = cyc_payer_pop.customer_key and bill_statement.document_seq_no = document.doc_seq_no 可以通过执行计划看到,性能的瓶颈主要在两个地方,一个是做了全表扫描的部分 表CYC_PAYER_POP,另外一个就是CPU资源的过度消耗,表DOCUMENT SQL Plan Monitoring Details (Plan Hash Value=1606258714)
明确了这一点,我尝试把CYC_PAYER_POP的查询和数据字典表结合起来,过滤掉绝大部分数据。形成一个子查询。 在子查询中,启用了hint来强制查询按照计划的顺序和索引来执行。 (select /*+ leading (c p) index(p CYC_PAYER_POP_PK)*/ p.ba_no,p.cycle_seq_no,p.cycle_seq_run,p.period_key,p.customer_key from cyc_payer_pop p, cycle_control c where c.cycle_code = 2 and c.cycle_instance = 7 and c.cycle_year = 2014 and p.cycle_seq_no = c.cycle_seq_no and p.db_status = ‘BL’ ) cyc_payer_pop
然后在这个基础上,再和两个大表做关联,
优化后的sql语句如下: select /*+ leading( cyc_payer_pop bill_statement document)*/ document.period_key, document.cycle_seq_no, document.ba_no, document.customer_no, bill_statement.pay_channel_no from document, –千万数据量 12671016 rows bill_statement ,–千万数据量 12671016 rows (select /*+ leading (c p) index(p CYC_PAYER_POP_PK)*/ p.ba_no,p.cycle_seq_no,p.cycle_seq_run,p.period_key,p.customer_key from cyc_payer_pop p, cycle_control c where c.cycle_code = 2 and c.cycle_instance = 7 and c.cycle_year = 2014 and p.cycle_seq_no = c.cycle_seq_no and p.db_status = ‘BL’ ) cyc_payer_pop where and document.ba_no = cyc_payer_pop.ba_no and document.cycle_seq_no = cyc_payer_pop.cycle_seq_no and document.cycle_seq_run = cyc_payer_pop.cycle_seq_run and document.period_key = cyc_payer_pop.period_key and document.customer_key = cyc_payer_pop.customer_key and document.doc_produce_ind in (‘Y ‘, ‘ E ‘) and document.document_status != ‘ N’ and bill_statement.ba_no = cyc_payer_pop.ba_no and bill_statement.cycle_seq_no = document.cycle_seq_no and bill_statement.cycle_seq_run = document.cycle_seq_run and bill_statement.period_key = cyc_payer_pop.period_key and bill_statement.customer_key = cyc_payer_pop.customer_key and bill_statement.document_seq_no = document.doc_seq_no
优化后的执行计划如下。document表的io请求数从1700万次,降低到了将近8万次。解决了性能瓶颈。 SQL Plan Monitoring Details (Plan Hash Value=1573871804)