1. 程式人生 > >Oracle優化之單表分頁優化

Oracle優化之單表分頁優化

單表分頁優化思路:

--建立測試表:

SQL> create table t_test as select * from dba_objects;

Table created.

如,下面的sql (沒有過濾條件,只有排序),要將查詢結果分頁顯示,每頁顯示10條,如:

select * from t_test order by object_id;

例子:

1、分頁查詢sql語句,如下(通常會採用下面的方法,但是這是錯誤的分頁框架)

語法:select * from (select t.*,rownum rn from (需要分頁的sql)t ) where rn >=1 and rn <=10;

SQL> select * from (select t.*,rownum rn from (select * from t_test order by object_id) t) where rn >=1 and rn <=10;
Execution Plan
----------------------------------------------------------
Plan hash value: 3603170480
----------------------------------------------------------------------------------------
| Id  | Operation            | Name  | Rows  | Bytes |TempSpc| Cost (%CPU)| Time    |
----------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT      |        | 80700 |    16M|      |  3918  (1)| 00:00:48 |
|*  1 |  VIEW                |        | 80700 |    16M|      |  3918  (1)| 00:00:48 |
|  2 |  COUNT              |        |      |      |      |            |          |
|  3 |    VIEW              |        | 80700 |    15M|      |  3918  (1)| 00:00:48 |
|  4 |    SORT ORDER BY    |        | 80700 |    15M|    19M|  3918  (1)| 00:00:48 |
|  5 |      TABLE ACCESS FULL| t_test | 80700 |    15M|      |  287  (1)| 00:00:04 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN"<=10 AND "RN">=1)
Note
-----
  - dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
        331  recursive calls
          0  db block gets
      1211  consistent gets
      1024  physical reads
          0  redo size
      2075  bytes sent via SQL*Net to client
        523  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
        10  rows processed

  從上面的執行計劃可以看出,該sql走了全表掃描,如果該表很大,則先要將該表排序,再取出其中10行資料,這樣會出現嚴重的效能問題,所以該sql不能走全表掃描,必須走索引掃描。(因為索引已經排序了,可使用索引來消除排序)一般分頁語句中都有排序。

如下,在object_id列建立索引,並在索引中新增一個常量0,如下:(在索引中新增一個常量0,是因為object_id列允許位null,如果不新增常量(不一定是0,可以是1,2,3,也可以是英文字母),索引中就不能儲存null值,;因為sql中並沒有剔除null值,所以我們必須要新增一個常量,讓索引儲存null,這樣才能使sql走索引。)

SQL> create index idx_test on t_test(object_id,0);

SQL> select * from (select t.*,rownum rn from (select /*+ index)t_test idx_test) */ * from t_test order by object_id) t) where rn >=1 and rn <=10;

SQL> alter session set statistics_level=all;

SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
-----------------------------------------
SQL_ID  gvq2rvmutr85w, child number 0
-------------------------------------
 select * from (select t.*,rownum rn from (select /*+ index)t_test
idx_test) */ * from t_test order by object_id) t) where rn >=1 and rn
<=10
Plan hash value: 3119682446
-----------------------------------------------------------------------------------------------------
| Id  | Operation                      | Name    | Starts | E-Rows | A-Rows |  A-Time  | Buffers |
-----------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT              |          |      1 |        |    10 |00:00:00.08 |    1245 |
|*  1 |  VIEW                          |          |      1 |  80700 |    10 |00:00:00.08 |    1245 |
|  2 |  COUNT                        |          |      1 |        |  71903 |00:00:00.07 |    1245 |
|  3 |    VIEW                        |          |      1 |  80700 |  71903 |00:00:00.06 |    1245 |
|  4 |    TABLE ACCESS BY INDEX ROWID| t_test  |      1 |  80700 |  71903 |00:00:00.04 |    1245 |
|  5 |      INDEX FULL SCAN          | IDX_test |      1 |  80700 |  71903 |00:00:00.01 |    181 |
-----------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter(("RN"<=10 AND "RN">=1))
Note
-----
  - dynamic sampling used for this statement (level=2)
28 rows selected.

(因為sql語句沒有where過濾條件,強制走索引只能走index full scan,無法走索引範圍掃描(index range scan)。E-Rows的顯示 說明了掃描了索引中所有葉子塊,一共消耗了1245個邏輯讀;理想的執行計劃是:index full scan只掃描一個最多幾個索引葉子塊,最根本的原因還在於這個分頁框架錯了!)

2、正確的分頁框架:

語法:select * from (select * from (select a.*,rownum rn from (需要分頁的sql) a ) where rownum <=10) where rn >=1;

SQL> select * from (select * from (select a.*,rownum rn from (select /*+ index(t_test idx_test) */ * from t_test order by object_id) a ) where rownum <=10) where rn >=1;
SQL> alter session set statistics_level=all;
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
-----------------------------------------
SQL_ID  cgjp65zfj1yqa, child number 0
-------------------------------------
select * from (select * from (select a.*,rownum rn from (select /*+
index(t_test idx_test) */ * from t_test order by object_id) a ) where
rownum <=10) where rn >=1
Plan hash value: 1201925926
-------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name    | Starts | E-Rows | A-Rows |  A-Time  | Buffers |
-------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                |          |      1 |        |    10 |00:00:00.01 |      5 |
|*  1 |  VIEW                            |          |      1 |    10 |    10 |00:00:00.01 |      5 |
|*  2 |  COUNT STOPKEY                  |          |      1 |        |    10 |00:00:00.01 |      5 |
|  3 |    VIEW                          |          |      1 |  80700 |    10 |00:00:00.01 |      5 |
|  4 |    COUNT                        |          |      1 |        |    10 |00:00:00.01 |      5 |
|  5 |      VIEW                        |          |      1 |  80700 |    10 |00:00:00.01 |      5 |
|  6 |      TABLE ACCESS BY INDEX ROWID| t_test  |      1 |  80700 |    10 |00:00:00.01 |      5 |
|  7 |        INDEX FULL SCAN          | IDX_test |      1 |  80700 |    10 |00:00:00.01 |      3 |
-------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
Note
-----
  - dynamic sampling used for this statement (level=2)
31 rows selected.

(從上面的執行計劃可以看到,sql走 index full scan,只掃描了10條資料(id=7 A-Rows=10)就停止了(id=2 COUNT STOPKEY)一共消耗了5個邏輯讀;該執行計劃利用索引已經排序的特性只掃描索引獲取了10條資料,然後再利用count stopkey特性,獲取到分頁需要的資料,sql立即停止執行,這才是最佳的執行計劃。)

注意:

  如果分頁語句中有排序(order by),要利用索引已經排序特性,將order by的列包含在索引中,同時也要利用rownum的count stopkey特性來優化分頁sql。如果分頁中沒有排序,可以直接利用rownum的count stopkey特性來優化分頁sql。

例子:

如下兩條sq(注意,過濾條件是等值過濾,當然也有order by)l,改成分頁語句,並檢視執行計劃,如下:

select * from t_test where owner='SCOTT' order by object_id;

select * from t_test where owner='SYS' order by object_id;

(其中第一條sql語句的過濾條件是where owner='SCOTT';該過濾條件能過濾掉表中大部分資料。第二條sql語句的過濾條件where owner='SYS',能過濾表中一半資料)

---執行第一條sql語句:

SQL> select * from (select * from (select a.*,rownum rn from (select /*+ index(t_test idx_test) */ * from t_test where owner='SCOTT' order by object_id) a ) where rownum <=10) where rn >=1;
SQL> alter session set statistics_level=all;
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
--------------------------------------------
SQL_ID  0w9tbrwkn9tn6, child number 0
-------------------------------------
 select * from (select * from (select a.*,rownum rn from (select /*+
index(t_test idx_test) */ * from t_test where owner='SCOTT' order by
object_id) a ) where rownum <=10) where rn >=1
Plan hash value: 1201925926
-------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name    | Starts | E-Rows | A-Rows |  A-Time  | Buffers |
-------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                |          |      1 |        |    10 |00:00:00.04 |    1245 |
|*  1 |  VIEW                            |          |      1 |    10 |    10 |00:00:00.04 |    1245 |
|*  2 |  COUNT STOPKEY                  |          |      1 |        |    10 |00:00:00.04 |    1245 |
|  3 |    VIEW                          |          |      1 |    12 |    10 |00:00:00.04 |    1245 |
|  4 |    COUNT                        |          |      1 |        |    10 |00:00:00.04 |    1245 |
|  5 |      VIEW                        |          |      1 |    12 |    10 |00:00:00.04 |    1245 |
|*  6 |      TABLE ACCESS BY INDEX ROWID| t_test  |      1 |    12 |    10 |00:00:00.04 |    1245 |
|  7 |        INDEX FULL SCAN          | IDX_test |      1 |  80700 |  71901 |00:00:00.01 |    181 |
-------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
  6 - filter("OWNER"='SCOTT')
Note
-----
  - dynamic sampling used for this statement (level=2)
32 rows selected.

---執行第2條語句,如下:

SQL> select * from (select * from (select a.*,rownum rn from (select /*+ index(t_test idx_test) */ * from t_test where owner='SYS' order by object_id) a ) where rownum <=10) where rn >=1;
SQL> alter session set statistics_level=all;
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
-------------------------------------------------------
SQL_ID  dfwkpppbtc8h7, child number 0
-------------------------------------
select * from (select * from (select a.*,rownum rn from (select /*+
index(t_test idx_test) */ * from t_test where owner='SYS' order by
object_id) a ) where rownum <=10) where rn >=1
Plan hash value: 1201925926
-------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name    | Starts | E-Rows | A-Rows |  A-Time  | Buffers |
-------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                |          |      1 |        |    10 |00:00:00.01 |      5 |
|*  1 |  VIEW                            |          |      1 |    10 |    10 |00:00:00.01 |      5 |
|*  2 |  COUNT STOPKEY                  |          |      1 |        |    10 |00:00:00.01 |      5 |
|  3 |    VIEW                          |          |      1 |  28483 |    10 |00:00:00.01 |      5 |
|  4 |    COUNT                        |          |      1 |        |    10 |00:00:00.01 |      5 |
|  5 |      VIEW                        |          |      1 |  28483 |    10 |00:00:00.01 |      5 |
|*  6 |      TABLE ACCESS BY INDEX ROWID| t_test  |      1 |  28483 |    10 |00:00:00.01 |      5 |
|  7 |        INDEX FULL SCAN          | IDX_test |      1 |  80700 |    10 |00:00:00.01 |      3 |
-------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
  6 - filter("OWNER"='SYS')
Note
-----
  - dynamic sampling used for this statement (level=2)
32 rows selected.

  從上面的執行計劃可以看出,兩條sql都走了 index full scan,第1條sql從索引中掃描了71901條資料(id=7 A-Rows=71901),在回表的時候對資料進行了大量過濾(id=6),最後得到10條資料,耗費了1245個邏輯讀。

  第2條sql從索引中掃描了10條資料,耗費了5個邏輯讀。可以看出,第二條sql的執行計劃是正確的,而第一條sql的執行計劃是錯誤的,應該儘量在索引掃描的時候就取得10行資料。

(為什麼上面的兩條sql只有過濾條件不一樣,而第一條sql的執行計劃就錯了呢?這是因為第一條sql的過濾條件where owner='SCOTT',在表中只有很少資料,通過掃描object_id列的索引,然後在回表去匹配owner='SCOTT',因為owner='SCOTT'資料量很少,要搜尋大量資料才能匹配上。而第二條sql的過濾條件owner='SYS',因為資料量多,只需要搜尋少量的資料就能匹配上。)

---優化第一條sql:(就必須讓過濾條件的列出現在索引中,如下:)

SQL> create index idx_test_all on t_test(owner,object_id);  ---建立一個組合索引,包含owner欄位
SQL>  select * from (select * from (select a.*,rownum rn from (select /*+ index(t_test idx_test_all) */ * from t_test where owner='SCOTT' order by object_id) a ) where rownum <=10) where rn >=1;
SQL>  select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
---------------------------------------------、
SQL_ID  9mm61b7j943sf, child number 0
-------------------------------------
 select * from (select * from (select a.*,rownum rn from (select /*+
index(t_test idx_test_all) */ * from t_test where owner='SCOTT' order
by object_id) a ) where rownum <=10) where rn >=1
Plan hash value: 3696904346
-----------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name        | Starts | E-Rows | A-Rows |  A-Time  | Buffers |
-----------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                |              |      1 |        |    10 |00:00:00.01 |      6 |
|*  1 |  VIEW                            |              |      1 |    10 |    10 |00:00:00.01 |      6 |
|*  2 |  COUNT STOPKEY                  |              |      1 |        |    10 |00:00:00.01 |      6 |
|  3 |    VIEW                          |              |      1 |    12 |    10 |00:00:00.01 |      6 |
|  4 |    COUNT                        |              |      1 |        |    10 |00:00:00.01 |      6 |
|  5 |      VIEW                        |              |      1 |    12 |    10 |00:00:00.01 |      6 |
|  6 |      TABLE ACCESS BY INDEX ROWID| t_test      |      1 |    12 |    10 |00:00:00.01 |      6 |
|*  7 |        INDEX RANGE SCAN          | IDX_test_ALL |      1 |    12 |    10 |00:00:00.01 |      3 |
-----------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
  7 - access("OWNER"='SCOTT')
Note
-----
  - dynamic sampling used for this statement (level=2)
32 rows selected.

(這時候會發現,sql走了索引範圍掃描,從索引中掃描了10條資料,一共耗費了6個邏輯讀。這說明現在的執行計劃是正確的。)

注意:

  在實際的生成環境中,過濾條件一般都是繫結變數,我們無法控制傳參究竟傳入哪個值,這就不能確定返回資料究竟是多還是少了,所以,建議最好將排序的列包含在索引中。

但是要注意:如果排序列有多個列,建立索引的時候,我們要將所有的排序列包含在索引中,並且要注意排序列先後順序,而且還要注意列時升序還是降序。如果分頁語句中排序列只有一個列,但是是降序顯示的,建立索引的時候就沒必要降序建立索引了,我們可以使用hint:index_desc 讓索引降序掃描就可以了。

例子:

(建立索引,只能是object_id在前,object_name在後,另外object_name是降序顯示的,那麼在建立索引的時候,還要指定object_name列降序排序。下面的sql也沒有過濾條件,在建立索引的時候還要加個常量,如下所示:)

SQL> create index idx_test_2 on t_test(object_id,object_name desc,0);  ---建立一個組合索引,包含owner欄位
SQL>  select * from (select * from (select a.*,rownum rn from (select /*+ index(t_test idx_test_2) */ * from t_test  order by object_id,object_name desc) a ) where rownum <=10) where rn >=1;
SQL>  select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
----------------------------------------
SQL_ID  g8zgbvt1u1qjq, child number 0
-------------------------------------
select * from (select * from (select a.*,rownum rn from (select /*+
index(t_test idx_test_2) */ * from t_test  order by
object_id,object_name desc) a ) where rownum <=10) where rn >=1
Plan hash value: 2251915778
------------------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name      | Starts | E-Rows | A-Rows |  A-Time  | Buffers | Reads  |
------------------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                |            |      1 |        |    10 |00:00:00.01 |      5 |      1 |
|*  1 |  VIEW                            |            |      1 |    10 |    10 |00:00:00.01 |      5 |      1 |
|*  2 |  COUNT STOPKEY                  |            |      1 |        |    10 |00:00:00.01 |      5 |      1 |
|  3 |    VIEW                          |            |      1 |  80700 |    10 |00:00:00.01 |      5 |      1 |
|  4 |    COUNT                        |            |      1 |        |    10 |00:00:00.01 |      5 |      1 |
|  5 |      VIEW                        |            |      1 |  80700 |    10 |00:00:00.01 |      5 |      1 |
|  6 |      TABLE ACCESS BY INDEX ROWID| t_test    |      1 |  80700 |    10 |00:00:00.01 |      5 |      1 |
|  7 |        INDEX FULL SCAN          | IDX_test_2 |      1 |  80700 |    10 |00:00:00.01 |      3 |      1 |
------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
Note
-----
  - dynamic sampling used for this statement (level=2)
31 rows selected.

如果在建立索引的時候沒有指定 object_name列降序排序,那麼執行計劃中會出現sort order by。因為索引中排序和分頁語句中排序不一致,如:

SQL> create index idx_test_3 on t_test(object_id,object_name,0);
SQL>  select * from (select * from (select a.*,rownum rn from (select /*+ index(t_test idx_test_3) */ * from t_test  order by object_id,object_name desc) a ) where rownum <=10) where rn >=1;
SQL>  select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
---------------------------------------------
SQL_ID  bapu5jn5vn22r, child number 0
-------------------------------------
select * from (select * from (select a.*,rownum rn from (select /*+
index(t_test idx_test_3) */ * from t_test  order by
object_id,object_name desc) a ) where rownum <=10) where rn >=1
Plan hash value: 3547841113
-----------------------------------------
| Id  | Operation                        | Name      | Starts | E-Rows | A-Rows |  A-Time  | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                  |            |      1 |        |    10 |00:00:00.10 |    1490 |    426 |      |      |          |
|*  1 |  VIEW                            |            |      1 |    10 |    10 |00:00:00.10 |    1490 |    426 |      |      |          |
|*  2 |  COUNT STOPKEY                  |            |      1 |        |    10 |00:00:00.10 |    1490 |    426 |      |      |          |
|  3 |    VIEW                          |            |      1 |  80700 |    10 |00:00:00.10 |    1490 |    426 |      |      |          |
|  4 |    COUNT                        |            |      1 |        |    10 |00:00:00.10 |    1490 |    426 |      |      |          |
|  5 |      VIEW                        |            |      1 |  80700 |    10 |00:00:00.10 |    1490 |    426 |      |      |          |
|  6 |      SORT ORDER BY              |            |      1 |  80700 |    10 |00:00:00.10 |    1490 |    426 |    10M|  1272K| 9811K (0)|
|  7 |        TABLE ACCESS BY INDEX ROWID| t_test    |      1 |  80700 |  71903 |00:00:00.06 |    1490 |    426 |      |      |          |
|  8 |        INDEX FULL SCAN          | IDX_test_3 |      1 |  80700 |  71903 |00:00:00.03 |    427 |    426 |      |      |          |
----------------------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
Note
-----
  - dynamic sampling used for this statement (level=2)
32 rows selected.

(從上面可以看出,執行計劃中出現了 sort order by,這就意味著分頁語句沒有利用到索引已經排序的特性,執行計劃是錯誤的,這時候就需要建立正確的索引。)

例子3:

一條sql(,過濾條件有等值條件,也有非等值條件,當然也有order by),如下,將下面的sql分頁查詢:

select * from t_test where owner='SYS' and object_id > 1000 order by object_name;

--:建立索引(因為owner是等值過濾,object_Id是非等值過濾,建立索引的時候要優先將等值過濾列和排序列組合在一起,然後再將非等值過濾列放到後面)如下:

create index idx_test_4 on t_test(owner,object_name,object_id);
select * from (select * from (select a.*,rownum rn from (select /*+ index(t_test idx_test_4) */ * from t_test where owner='SYS' and object_id > 1000 order by object_name) a ) where rownum <=10) where rn >=1;
SQL>  select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
PLAN_TABLE_OUTPUT
------------------------------------------:
SQL_ID  4z6tjgrdjm5a1, child number 0
-------------------------------------
select * from (select * from (select a.*,rownum rn from (select /*+
index(t_test idx_test_4) */ * from t_test where owner='SYS' and
object_id > 1000 order by object_name) a ) where rownum <=10) where rn
>=1
Plan hash value: 1432357471
------------------------------------------------------------------------------------------------------------------
| Id  | Operation                        | Name      | Starts | E-Rows | A-Rows |  A-Time  | Buffers | Reads  |
------------------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                |            |      1 |        |    10 |00:00:00.01 |      14 |      2 |
|*  1 |  VIEW                            |            |      1 |    10 |    10 |00:00:00.01 |      14 |      2 |
|*  2 |  COUNT STOPKEY                  |            |      1 |        |    10 |00:00:00.01 |      14 |      2 |
|  3 |    VIEW                          |            |      1 |  25683 |    10 |00:00:00.01 |      14 |      2 |
|  4 |    COUNT                        |            |      1 |        |    10 |00:00:00.01 |      14 |      2 |
|  5 |      VIEW                        |            |      1 |  25683 |    10 |00:00:00.01 |      14 |      2 |
|  6 |      TABLE ACCESS BY INDEX ROWID| t_test    |      1 |  25683 |    10 |00:00:00.01 |      14 |      2 |
|*  7 |        INDEX RANGE SCAN          | IDX_test_4 |      1 |    256 |    10 |00:00:00.01 |      4 |      2 |
------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
  7 - access("OWNER"='SYS' AND "OBJECT_ID">1000)
      filter("OBJECT_ID">1000)
Note
-----
  - dynamic sampling used for this statement (level=2)
34 rows selected.

(可以看出,執行計劃中沒有出現 sort order by,邏輯讀也只有14個,說明執行計劃是正確的。)

注意:

  ①:如果分頁語句中有排序(order by),要利用索引已經排序的特性,將order by的列按照排序的先後順序包含在索引中,同時要注意排序是升序還是降序。如果分頁語句中有過濾條件,我們要注意過濾條件是否有等值過濾條件,如果有等值過濾條件,要將等值過濾條件優先組合在一起,然後將排序列放在等值過濾條件後面,最後將非等值過濾列放排序列後面。如果分頁語句中沒有等值過濾條件,我們應該先將排序列放在索引前面,將非等值過濾列放後面,最後利用rownum的count stopkey特性來優化分頁sql。如果分頁中沒有排序,可以直接利用rownum的count stopkey特性來優化分頁sql。

 ②:要想一眼看出執行計劃是否正確,就要先看分頁語句有沒有order by,再看執行計劃有沒有sort order by,如果執行計劃中有sort order by,說明執行計劃一般是錯誤的。

 ③:如果分頁語句中排序的表是分割槽表,這時我們要看分頁語句中是否有跨區分割槽掃描,如果有跨分割槽掃描,建立的索引一般都是global索引,如果不建立global索引,就無法保證分頁的順序與所有的順序是一致的。如果就只掃描一個分割槽,這時可以建立local索引:

例子:建立分割槽表,並匯入資料

 CREATE TABLE p_test

  (    "OWNER" VARCHAR2(30),

        "OBJECT_NAME" VARCHAR2(128),

        "SUBOBJECT_NAME" VARCHAR2(30),

        "OBJECT_ID" NUMBER,

        "DATA_OBJECT_ID" NUMBER,

        "OBJECT_TYPE" VARCHAR2(19),

        "CREATED" DATE,

        "LAST_DDL_TIME" DATE,

        "TIMESTAMP" VARCHAR2(19),

        "STATUS" VARCHAR2(7),

        "TEMPORARY" VARCHAR2(1),

        "GENERATED" VARCHAR2(1),

        "SECONDARY" VARCHAR2(1),

        "NAMESPACE" NUMBER,

        "EDITION_NAME" VARCHAR2(30)

  ) partition by range (object_id)

  (

  partition p1 values less than (10000),

  partition p2 values less than (20000),

  partition p3 values less than (30000),

  partition p4 values less than (40000),

  partition p5 values less than (50000),

  partition p6 values less than (60000),

  partition p7 values less than (70000),

  partition px values less than (maxvalue));

 SQL> insert into p_test select * from dba_objects;

71917 rows created. 

---例子1:分頁語句:

select * from (selct * from (select a.*,rownum rn from (select * from p_test order by object_id) a ) where rownum <=10) where rn >=1;

  (這個分頁語句沒有過濾條件,因此會掃描表中的所有分割槽。因為排序列恰好是範圍分割槽列,範圍分割槽每個分割槽的資料也是遞增的,這時我們建立索引可以建立為local索引。但是如果將範圍分割槽改為list分割槽或者hash分割槽,這時我們就必須建立global索引,因為list分割槽和hash分割槽是無序的。)

SQL> create index idx_test_id on p_test(object_id,0) local;  ---建立local索引

select * from (select * from (select a.*,rownum rn from (select /*+ index(p_test idx_test_id ) */ * from p_test order by object_id) a ) where rownum <=10) where rn >=1;
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
SQL_ID  bxw1059jmgxvx, child number 0
-------------------------------------
select * from (select * from (select a.*,rownum rn from (select /*+
index(p_test idx_test_id ) */ * from p_test order by object_id) a )
where rownum <=10) where rn >=1
Plan hash value: 1291390031
--------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                              | Name        | Starts | E-Rows | A-Rows |  A-Time  | Buffers | Reads  |
--------------------------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                        |            |      1 |        |    10 |00:00:00.01 |      5 |      1 |
|*  1 |  VIEW                                  |            |      1 |    10 |    10 |00:00:00.01 |      5 |      1 |
|*  2 |  COUNT STOPKEY                        |            |      1 |        |    10 |00:00:00.01 |      5 |      1 |
|  3 |    VIEW                                |            |      1 |  63696 |    10 |00:00:00.01 |      5 |      1 |
|  4 |    COUNT                              |            |      1 |        |    10 |00:00:00.01 |      5 |      1 |
|  5 |      VIEW                              |            |      1 |  63696 |    10 |00:00:00.01 |      5 |      1 |
|  6 |      PARTITION RANGE ALL              |            |      1 |  63696 |    10 |00:00:00.01 |      5 |      1 |
|  7 |        TABLE ACCESS BY LOCAL INDEX ROWID| P_TEST      |      1 |  63696 |    10 |00:00:00.01 |      5 |      1 |
|  8 |        INDEX FULL SCAN                | IDX_TEST_ID |      1 |  63696 |    10 |00:00:00.01 |      3 |      1 |
--------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
Note
-----
  - dynamic sampling used for this statement (level=2)
32 rows selected.

例子2:分頁語句(根據object_name排序)

select * from (selct * from (select a.*,rownum rn from (select * from p_test order by object_name) a ) where rownum <=10) where rn >=1;

這時候我們需要建立global索引,因為如果是本地索引就會產生 sort order by

SQL> drop index idx_test_id ;
SQL> create index idx_test_id on p_test(object_name,0);
select * from (select * from (select a.*,rownum rn from (select /*+ index(p_test idx_test_id ) */ * from p_test order by object_name) a ) where rownum <=10) where rn >=1;
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));
SQL_ID  25pm0f6b6m49x, child number 0
-------------------------------------
select * from (select * from (select a.*,rownum rn from (select /*+
index(p_test idx_test_id ) */ * from p_test order by object_name) a )
where rownum <=10) where rn >=1
Plan hash value: 246970912
--------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                              | Name        | Starts | E-Rows | A-Rows |  A-Time  | Buffers | Reads  |
--------------------------------------------------------------------------------------------------------------------------
|  0 | SELECT STATEMENT                        |            |      1 |        |    10 |00:00:00.01 |      10 |      2 |
|*  1 |  VIEW                                  |            |      1 |    10 |    10 |00:00:00.01 |      10 |      2 |
|*  2 |  COUNT STOPKEY                        |            |      1 |        |    10 |00:00:00.01 |      10 |      2 |
|  3 |    VIEW                                |            |      1 |  63696 |    10 |00:00:00.01 |      10 |      2 |
|  4 |    COUNT                              |            |      1 |        |    10 |00:00:00.01 |      10 |      2 |
|  5 |      VIEW                              |            |      1 |  63696 |    10 |00:00:00.01 |      10 |      2 |
|  6 |      TABLE ACCESS BY GLOBAL INDEX ROWID| P_TEST      |      1 |  63696 |    10 |00:00:00.01 |      10 |      2 |
|  7 |        INDEX FULL SCAN                  | IDX_TEST_ID |      1 |  63696 |    10 |00:00:00.01 |      4 |      2 |
--------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
  1 - filter("RN">=1)
  2 - filter(ROWNUM<=10)
Note
-----
  - dynamic sampling used for this statement (level=2)
31 rows selected.