当前位置:网站首页>A case of 94 SQL optimization (the writing method used is often rejected)

A case of 94 SQL optimization (the writing method used is often rejected)

2022-06-22 22:17:00 Tiger Liu

On the customer's production system SQL, The watch is getting bigger and bigger , The execution time is getting longer and longer , But as long as we can get results , As long as it's not too slow to accept , The user basically tolerated .

Many customers' systems are like this , Business SQL Consuming too many system resources , The execution efficiency is still very poor , Most of them have a lot of room for optimization , But many people first think of replacing advanced hardware , I don't know that optimization is the right way ( With a customer EBS Report after several hours of business execution ora-01555 error , The hardware is already very advanced , SQL Optimization is the only way out )

SQL Code ( Has been simplified desensitization treatment ):

select a.*, b.INPTBR as inst_no

from pa_agency a

left join

(

select tc.inptbr, tc.fragid

from book1 tc

union

select tcs.inptbr, tcs.fragid

from book2 tcs

) b

on a.agenid = b.fragid;

Implementation plan ( execution time 38 second ):

It is known that b Result set fragid There is no repetition value . 3 The record numbers of the tables are shown in the execution plan in the above figure . Please think about this SQL How to improve execution efficiency .

Thinking time ....................................................

I'll take this. SQL As an exercise, it was put in the wechat group of students to practice for everyone , Some students have given a better optimization method , The methods of the trainees and mine are listed below :

First of all, they should be created separately book1 and boo2 The two tables fragid Index on field .

The second is the need for sql To rewrite .

Students' rewriting methods :

select a.*,

nvl((select inptbr

from book1 tc

where a.agenid = tc.fragid

and rownum = 1

),

(select inptbr

from book2 tcs

where a.agenid = tcs.fragid

and rownum = 1

)) as inptbr

from pa_agency a;

My rewrite method :

select a.*,

(select inptbr from

(

select tc.inptbr,tc.fragid

from book1 tc

union all

select tcs.inptbr,tcs.fragid

from book2 tcs

) b where a.agenid=b.fragid

and rownum =1

) as inst_no

from pa_agency a ;

Both methods can greatly improve sql Execution efficiency ( It is expected that the execution will be completed in one second ), There are also some subtle differences in the efficiency of the two methods , I wonder if you can see ?

In my previous official account article , There are similar optimization ideas : <74- This kind of SQL Optimize ,oracle I lost it. mysql, How to remedy ?> , Some things are not necessarily bad , Sometimes we can use it for optimization .

( This is the end of this article. )

原网站

版权声明
本文为[Tiger Liu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221946305861.html