使用or展開進行sql優化(即sql語法union all代替or可以提高效率)


問題:

這樣一條sql應該怎么優化?

select * from sys_user

where user_code = 'zhangyong'

or user_code in

(select grp_code

from sys_grp

where sys_grp.user_code = 'zhangyong')

Execution Plan

----------------------------------------------------------

0  SELECT STATEMENT Optimizer=RULE

1  0   FILTER

2  1     TABLE ACCESS (FULL) OF 'SYS_USER'

3  1     INDEX (UNIQUE SCAN) OF 'PK_SYS_GRP' (UNIQUE)

Statistics

----------------------------------------------------------

14  recursive calls

4  db block gets

30590 consistent gets

0  physical reads

0  redo size

1723  bytes sent via SQL*Net to client

425  bytes received via SQL*Net from client

2  SQL*Net roundtrips to/from client

0  sorts (memory)

0  sorts (disk)

3  rows processed

里面的查詢返回的記錄數一般只有一兩條,但sys_user表的數據很多,怎么樣才能讓這條sql以sys_grp為驅動表?

表中記錄情況如下:

SQL> select count(*) from sys_grp;

COUNT(*)

----------

25130

SQL> select count(*) from sys_user;

COUNT(*)

----------

15190

優化:

降低邏輯讀是優化SQL的基本原則之一

我們嘗試通過降低邏輯讀來加快SQL的執行.

這里我們使用or展開來改寫SQL查詢:

select * from sys_user where user_code = 'zhangyong'

union all

select * from sys_user where user_code <> 'zhangyong'

and user_code in (select grp_code from sys_grp where sys_grp.user_code = 'zhangyong')

Statistics

----------------------------------------------------------

          0  recursive calls

          0  db block gets

130 consistent gets

          0  physical reads

          0  redo size

       1723  bytes sent via SQL*Net to client

        425  bytes received via SQL*Net from client

          2  SQL*Net roundtrips to/from client

          1  sorts (memory)

          0  sorts (disk)

          3  rows processed

Execution Plan

----------------------------------------------------------

   0      SELECT STATEMENT Optimizer=RULE

   1    0   UNION-ALL

   2    1     TABLE ACCESS (BY INDEX ROWID) OF 'SYS_USER'

   3    2       INDEX (UNIQUE SCAN) OF 'PK_SYS_USER' (UNIQUE)

   4    1     NESTED LOOPS

   5    4       VIEW OF 'VW_NSO_1'

   6    5         SORT (UNIQUE)

   7    6           TABLE ACCESS (BY INDEX ROWID) OF 'SYS_GRP'

   8    7             INDEX (RANGE SCAN) OF 'FK_SYS_USER_CODE' (NON-UNIQUE)

   9    4       TABLE ACCESS (BY INDEX ROWID) OF 'SYS_USER'

  10    9         INDEX (UNIQUE SCAN) OF 'PK_SYS_USER' (UNIQUE)  

我們注意到,通過改寫,邏輯讀減少到130,從30590到130這是一個巨大的提高,減少邏輯讀最終會減少資源消耗,提高SQL的執行效率.這個改寫把Filter改為了Nest LOOP,索引得以充分利用.從而大大提高了性能.

我們同時注意到,這里引入了一個排序

排序來自於這一步:

-----------------------------------------------------------------------------------------

6  5  SORT (UNIQUE)

7  6    TABLE ACCESS (BY INDEX ROWID) OF 'SYS_GRP'

8  7       INDEX (RANGE SCAN) OF 'FK_SYS_USER_CODE' (NON-UNIQUE)

------------------------------------------------------------------------------------------

在'SYS_GRP'表中,user_code 是非唯一鍵值

在in值判斷里,要做sort unique排序,去除重復值

這里的union all是不需要排序的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM