【YashanDB知識庫】崖山資料庫Outline功能驗證

YashanDB發表於2024-09-29

本文內容來自YashanDB官網,具體內容請見(https://www.yashandb.com/newsinfo/7488286.html?templateId=1718516)

測試驗證環境說明

測試用例說明

1、相同版本下,新增表資料量,使統計資訊失效。最佳化器優先使用outline的計劃。

2、相同版本下,繫結引數執行場景下,最佳化器優先使用outline的計劃。

3、單機主備環境,最佳化器優先使用outline的計劃。

4、升級資料庫版本後,最佳化器優先使用outline的計劃。

測試過程記錄

點選檢視程式碼
--建兩張普通表test_tab1和test_tab2 並且分佈插入200w資料。

create table test_tab1(col1 int, col2 int, col3 int);

create table test_tab2(col1 int, col2 int, col3 int);

SQL>

Succeed.

 

SQL>

Succeed.

 

SQL> begin

  for i in 1..2000000 loop

    insert into test_tab1 values(i+1,i+2,i+3);

    insert into test_tab2 values(i+2,i+3,i+4);

  end loop;

  commit;

end;

/

 

PL/SQL Succeed.

 

-- 收集統計資訊

SQL> exec DBMS_STATS.GATHER_DATABASE_STATS('GATHER AUTO', 1, 16, 'FOR ALL COLUMNS SIZE AUTO', 'AUTO', TRUE, FALSE);

 

 

PL/SQL Succeed.

--驗證版本

SQL> select * from v$version;

 

BANNER VERSION_NUMBER

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

Release 22.2.10.100 x86_64 22.2.10.100

 

1 row fetched.

SQL> explain select distinct t1.col1, t2.col1

from test_tab1 t1 inner join test_tab2 t2 on t1.col2=t2.col2

order by t1.col1 limit 500;

PLAN_DESCRIPTION

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

SQL hash value: 2118027925

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 500| 29542( 0)| |

| 2 | TOP SORT DISTINCT | | | 1999998| 29538( 0)| |

|* 3 | HASH JOIN INNER | | | 1999998| 24350( 0)| |

| 4 | TABLE ACCESS FULL | TEST_TAB1 | SALES | 2000000| 453( 0)| |

| 5 | TABLE ACCESS FULL | TEST_TAB2 | SALES | 2000000| 453( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   3 - Predicate : access("T1"."COL2" = "T2"."COL2")

 

18 rows fetched.

 

SQL> create unique index idx1 on test_tab2(col2, col1);

 

Succeed.

 

SQL> exec DBMS_STATS.GATHER_DATABASE_STATS('GATHER AUTO', 1, 16, 'FOR ALL COLUMNS SIZE AUTO', 'AUTO', TRUE, FALSE);

 

PL/SQL Succeed.

 

-- 建立索引後,從HASH JOIN 轉為NEXTED LOOPS INNER JOIN

SQL> explain select distinct t1.col1, t2.col1

   2 from test_tab1 t1 inner join test_tab2 t2 on t1.col2=t2.col2

order by t1.col1 limit 500;

   3

PLAN_DESCRIPTION

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

SQL hash value: 2118027925

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 500| 7785( 0)| |

| 2 | TOP SORT DISTINCT | | | 1999998| 7781( 0)| |

| 3 | NESTED LOOPS INNER | | | 1999998| 2594( 0)| |

| 4 | TABLE ACCESS FULL | TEST_TAB1 | SALES | 2000000| 453( 0)| |

|* 5 | INDEX RANGE SCAN | IDX1 | SALES | 1| 142( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   5 - Predicate : access("T2"."COL2" = "T1"."COL2")

 

18 rows fetched.

 

 

--刪除索引後,執行計劃從NEXTED LOOPS INNER 轉為 HASH JOIN INNER

SQL> drop index IDX1;

 

Succeed.

 

SQL>

explain select distinct t1.col1, t2.col1

from test_tab1 t1 inner join test_tab2 t2 on t1.col2=t2.col2

order by t1.col1 limit 500;

 

SQL>

PLAN_DESCRIPTION

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

SQL hash value: 2118027925

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 500| 29542( 0)| |

| 2 | TOP SORT DISTINCT | | | 1999998| 29538( 0)| |

|* 3 | HASH JOIN INNER | | | 1999998| 24350( 0)| |

| 4 | TABLE ACCESS FULL | TEST_TAB1 | SALES | 2000000| 453( 0)| |

| 5 | TABLE ACCESS FULL | TEST_TAB2 | SALES | 2000000| 453( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   3 - Predicate : access("T1"."COL2" = "T2"."COL2")

 

18 rows fetched.

 

-- 建立索引前,增加outline

-- outline 分別是ol_ab和ol_a

SQL> CREATE OUTLINE ol_ab FOR CATEGORY ctgy_ab ON

select distinct t1.col1, t2.col1

from test_tab1 t1 inner join test_tab2 t2 on t1.col2=t2.col2

order by t1.col1 limit 500;

Succeed.

 

SQL> CREATE OUTLINE ol_a FOR CATEGORY ctgy_ab ON

select /*+ full(t2) */ distinct t1.col1, t 2 3 4 2.col1

from test_tab1 t1 inner join test_tab2 t2 on t1.col2=t2.col2

order by t1.col1 limit 500;

Succeed.

 

--應用outline,使得配置生效

SQL> ALTER SESSION SET USE_STORED_OUTLINES=ctgy_ab;

 

Succeed.

 

-- 整庫收集統計資訊,讓執行計劃重新生成

SQL> exec DBMS_STATS.GATHER_DATABASE_STATS('GATHER AUTO', 1, 16, 'FOR ALL COLUMNS SIZE AUTO', 'AUTO', TRUE, FALSE);

 

PL/SQL Succeed.

--建立索引

-- 經前面測試得知,建立索引後,此版本資料庫會選擇NEXTED LOOP JOIN

SQL> create unique index idx1 on test_tab2(col2, col1);

 

Succeed.

 

-- 可以看到outline已生效,依然走HASH JOIN INNER

SQL> explain select distinct t1.col1, t2.col1

from test_tab1 t1 inner join test_tab2 t2 on t1.col2=t2.col2

order by t1.col1 limit 500;

 

SQL>

 

PLAN_DESCRIPTION

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

SQL hash value: 2118027925

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 500| 29542( 0)| |

| 2 | TOP SORT DISTINCT | | | 1999998| 29538( 0)| |

|* 3 | HASH JOIN INNER | | | 1999998| 24350( 0)| |

| 4 | TABLE ACCESS FULL | TEST_TAB1 | SALES | 2000000| 453( 0)| |

| 5 | TABLE ACCESS FULL | TEST_TAB2 | SALES | 2000000| 453( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   3 - Predicate : access("T1"."COL2" = "T2"."COL2")

                                                                

Outline Information :

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

                                                                

   - outline OL_AB used for this statement

 

23 rows fetched.

 

 

 SQL> SELECT join_pos, hint

FROM USER_OUTLINE_HINTS

WHERE name = 'OL_AB'; 2 3

 

JOIN_POS HINT

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

       0 LEADING(T1 T2)

       0 USE_HASH(T2)

       1 FULL(T1)

       2 FULL(T2)

 

4 rows fetched.
持續新增200w資料,使得兩張表統計資訊失效
點選檢視程式碼
begin

  for i in 2000003 ..4000003 loop

    insert into test_tab1 values(i+1,i+2,i+3);

    insert into test_tab2 values(i+2,i+3,i+4);

  end loop;

  commit;

end;

/
此時統計資訊已失效
點選檢視程式碼
SQL> select * from dba_tab_statistics where STALE_STATS!='N';

 

OWNER TABLE_NAME PARTITION_NAME PARTITION_POSITION SUBPARTITION_NAME SUBPARTITION_POSITION OBJECT_TYPE NUM_ROWS BLOCKS EMPTY_BLOCKS AVG_SPACE CHAIN_CNT AVG_ROW_LEN SAMPLE_SIZE LAST_ANALYZED GLOBAL_STATS USER_STATS LOCKED_STATS STALE_STATS SCOPE

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

SALES TEST_TAB1 TABLE 2000000 6007 36 735 0 20 2000000 2024-08-07 Y N N Y SHARED

SALES TEST_TAB2 TABLE 2000000 6039 68 774 0 20 2000000 2024-08-07 Y N N Y SHARED

 

2 rows fetched.

統計資訊失效後,最佳化器優先使用outline的計劃

點選檢視程式碼
-- outline 沒有生效時,業務查詢的計劃

SQL> explain select distinct t1.col1, t2.col1

from test_tab1 t1 inner join test_tab2 t2 on t1.col2=t2.col2

order by t1.col1 limit 500;

   2 3

PLAN_DESCRIPTION

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

SQL hash value: 2118027925

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 500| 943( 0)| |

| 2 | TOP SORT DISTINCT | | | 1000| 943( 0)| |

| 3 | NESTED LOOPS INNER | | | 100000| 683( 0)| |

| 4 | TABLE ACCESS FULL | TEST_TAB1 | SALES | 100000| 442( 0)| |

|* 5 | INDEX RANGE SCAN | IDX1 | SALES | 1| 142( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   5 - Predicate : access("T2"."COL2" = "T1"."COL2")

 

18 rows fetched.

 

--outline生效後,最佳化器優先使用outline的執行計劃

SQL> ALTER SESSION SET USE_STORED_OUTLINES=ctgy_ab;

 

Succeed.

 

SQL> explain select distinct t1.col1, t2.col1

from test_tab1 t1 inner join test_tab2 t2 on t1.col2=t2.col2

order by t1.col1 limit 500;

 

PLAN_DESCRIPTION

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

SQL hash value: 2118027925

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 500| 29542( 0)| |

| 2 | TOP SORT DISTINCT | | | 1999998| 29538( 0)| |

|* 3 | HASH JOIN INNER | | | 1999998| 24350( 0)| |

| 4 | TABLE ACCESS FULL | TEST_TAB1 | SALES | 2000000| 453( 0)| |

| 5 | TABLE ACCESS FULL | TEST_TAB2 | SALES | 2000000| 453( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   3 - Predicate : access("T1"."COL2" = "T2"."COL2")

                                                                

Outline Information :

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

                                                                

   - outline OL_AB used for this statement

 

23 rows fetched.

繫結引數執行,最佳化器優先使用outline計劃

點選檢視程式碼
--建立繫結引數的outline

CREATE OUTLINE ol_c FOR CATEGORY ctgy_ab ON

 select /*+ full(t2)*/ t2.col2,t2.col1 from test_tab2 t2 where t2.col2=? and t2.col1=?;

 

Succeed.

 

 

 ALTER system SET USE_STORED_OUTLINES=ctgy_ab;

 

-- outline生效

SQL> ALTER SESSION SET USE_STORED_OUTLINES=ctgy_ab;

 

Succeed.

 

SQL> explain select /*+ full(t2)*/ t2.col2,t2.col1 from test_tab2 t2 where t2.col2=? and t2.col1=?;

 

PLAN_DESCRIPTION

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

SQL hash value: 631693285

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

|* 1 | TABLE ACCESS FULL | TEST_TAB2 | SALES | 1| 448( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   1 - Predicate : filter("T2"."COL2" = Param(:0) AND "T2"."COL1" = Param(:1))

                                                                

Hint Information :

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

                                                                

full(t2) / rejected by IGNORE_OPTIM_EMBEDDED_HINTS

                                                                

Outline Information :

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

                                                                

   - outline OL_C used for this statement
<details>
<summary>點選檢視程式碼</summary>

</details>
 

24 rows fetched.
單機主備架構下,outline功能測試

主備資料庫都需要開啟outline功能。

點選檢視程式碼
[yashan@node01 install2210100]$ ./bin/yasboot cluster status -c yashandb -d

 hostid | node_type | nodeid | pid | instance_status | database_status | database_role | listen_address | data_path

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

 host0001 | db | 1-1:1 | 21925 | open | normal | primary | 192.168.33.103:1688 | /home/yashan/yasdb_data/db-1-1

          +-----------+--------+-------+-----------------+-----------------+---------------+---------------------+--------------------------------

          | db | 1-2:2 | 21959 | open | normal | standby | 192.168.33.103:1690 | /home/yashan/yasdb_data/db-1-2

----------+-----------+--------+-------+-----------------+-----------------+---------------+---------------------+--------------------------------

 

 

[yashan@node01 install2210100]$ yasql sys/yasdb_123@192.168.33.103:1688

YashanDB SQL Release 22.2.10.100 x86_64

 

Connected to:

YashanDB Server Release 22.2.10.100 x86_64 - X86 64bit Linux

 

SQL> ALTER SYSTEM SET USE_STORED_OUTLINES=ctgy_ab;

Succeed.

SQL> set autotrace on;

SQL> select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

 

COL2 COL1

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

3 3

 

Execution Plan

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

| Id | Operation type | Name | Owner | E - Rows | A - Rows | Cost(%CPU) | A - Time | Loops | Memory | Disk | Partition info |

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | | | | | | |

| 1 | WINDOW | | | 1| | 2212( 0)| | | | | |

| 2 | SORT DISTINCT | | | 1| | 2212( 0)| | | | | |

|* 3 | NESTED LOOPS INNER | | | 1| | 2212( 0)| | | | | |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| | 929( 0)| | | | | |

| 5 | INDEX FAST FULL SCAN | IDX_COL3 | SYS | 10000000| | 600( 0)| | | | | |

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   3 - Predicate : filter("T2"."COL2" = "T3"."COL1")

   4 - Predicate : filter(CAST("T2"."COL2" AS NUMBER) = 3)

                                                                

Outline Information :

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

                                                                

   - outline OL_AF used for this statement

 

Statistics

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

 

25 rows fetched.

 

SQL>

Disconnected from:

YashanDB Server Release 22.2.10.100 x86_64 - X86 64bit Linux

 

 

--備庫上測試outline功能

[yashan@node01 install2210100]$ yasql sys/yasdb_123@192.168.33.103:1690

YashanDB SQL Release 22.2.10.100 x86_64

 

Connected to:

YashanDB Server Release 22.2.10.100 x86_64 - X86 64bit Linux

 

SQL> set autotrace on;

SQL> select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

COL2 COL1

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

3 3

 

Execution Plan

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

| Id | Operation type | Name | Owner | E - Rows | A - Rows | Cost(%CPU) | A - Time | Loops | Memory | Disk | Partition info |

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | | | | | | |

| 1 | WINDOW | | | 1| | 1071( 0)| | | | | |

| 2 | SORT DISTINCT | | | 1| | 1071( 0)| | | | | |

| 3 | NESTED LOOPS INNER | | | 1| | 1071( 0)| | | | | |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| | 929( 0)| | | | | |

|* 5 | INDEX RANGE SCAN | IDX_COL3 | SYS | 1| | 142( 0)| | | | | |

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   4 - Predicate : filter(CAST("T2"."COL2" AS NUMBER) = 3)

   5 - Predicate : access("T3"."COL1" = "T2"."COL2")

 

Statistics

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

20 rows fetched.

 

--備庫需要單獨開啟outline

SQL> ALTER SYSTEM SET USE_STORED_OUTLINES=ctgy_ab;

 

 

Succeed.

 

SQL> select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

 

COL2 COL1

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

3 3

 

Execution Plan

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

| Id | Operation type | Name | Owner | E - Rows | A - Rows | Cost(%CPU) | A - Time | Loops | Memory | Disk | Partition info |

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | | | | | | |

| 1 | WINDOW | | | 1| | 2212( 0)| | | | | |

| 2 | SORT DISTINCT | | | 1| | 2212( 0)| | | | | |

|* 3 | NESTED LOOPS INNER | | | 1| | 2212( 0)| | | | | |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| | 929( 0)| | | | | |

| 5 | INDEX FAST FULL SCAN | IDX_COL3 | SYS | 10000000| | 600( 0)| | | | | |

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   3 - Predicate : filter("T2"."COL2" = "T3"."COL1")

   4 - Predicate : filter(CAST("T2"."COL2" AS NUMBER) = 3)

                                                                

Outline Information :

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

                                                                

   - outline OL_AF used for this statement

Statistics

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

 

25 rows fetched.

升級資料庫版本後,最佳化器優先使用outline為準

22.2.10.100 版本執行計劃和建立outline al_af

點選檢視程式碼
YashanDB Server Release 22.2.10.100 x86_64 - X86 64bit Linux

 

 

create table test_tab2(col1 varchar2(100),col2 varchar2(100),col3 varchar2(100));

 

create table test_tab3(col1 varchar2(100),col2 varchar2(100),col3 varchar2(100));

SQL>

Succeed.

 

 

Succeed.

 

SQL>

SQL>

begin

  for i in 1..10000000 loop

    insert into test_tab2 values(i+1,i+2,i+3);

    insert into test_tab3 values(i+2,i+3,i+4);

  end loop;

  commit;

end;

/

SQL>

 

PL/SQL Succeed.

 

 

SQL> exec DBMS_STATS.GATHER_DATABASE_STATS('GATHER AUTO', 1, 16, 'FOR ALL COLUMNS SIZE AUTO', 'AUTO', TRUE, FALSE);

 

  

 

PL/SQL Succeed.

 

create index idx_col2 on test_tab2(col2);

create index idx_col3 on test_tab3(col1);

SQL>

Succeed.

SQL>

 

Succeed.

 

 

SQL> explain select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

 

PLAN_DESCRIPTION

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 1| 1071( 0)| |

| 2 | SORT DISTINCT | | | 1| 1071( 0)| |

| 3 | NESTED LOOPS INNER | | | 1| 1071( 0)| |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| 929( 0)| |

|* 5 | INDEX RANGE SCAN | IDX_COL3 | SYS | 1| 142( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   4 - Predicate : filter(CAST("T2"."COL2" AS NUMBER) = 3)

   5 - Predicate : access("T3"."COL1" = "T2"."COL2")

 

19 rows fetched.

 

SQL> exec DBMS_STATS.GATHER_DATABASE_STATS('GATHER AUTO', 1, 16, 'FOR ALL COLUMNS SIZE AUTO', 'AUTO', TRUE, FALSE);

 

 

PL/SQL Succeed.

 

SQL> explain select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

 

PLAN_DESCRIPTION

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 1| 1071( 0)| |

| 2 | SORT DISTINCT | | | 1| 1071( 0)| |

| 3 | NESTED LOOPS INNER | | | 1| 1071( 0)| |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| 929( 0)| |

|* 5 | INDEX RANGE SCAN | IDX_COL3 | SYS | 1| 142( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   4 - Predicate : filter(CAST("T2"."COL2" AS NUMBER) = 3)

   5 - Predicate : access("T3"."COL1" = "T2"."COL2")

 

19 rows fetched.

 

SQL> CREATE OUTLINE ol_af FOR CATEGORY ctgy_ab ON

  select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

 

Succeed.

 

SQL>

ALTER SESSION SET USE_STORED_OUTLINES=ctgy_ab;

SQL>

Succeed.

 

SQL> explain select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

PLAN_DESCRIPTION

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 1| 2212( 0)| |

| 2 | SORT DISTINCT | | | 1| 2212( 0)| |

|* 3 | NESTED LOOPS INNER | | | 1| 2212( 0)| |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| 929( 0)| |

| 5 | INDEX FAST FULL SCAN | IDX_COL3 | SYS | 10000000| 600( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   3 - Predicate : filter("T2"."COL2" = "T3"."COL1")

   4 - Predicate : filter(CAST("T2"."COL2" AS NUMBER) = 3)

                                                                

Outline Information :

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

                                                                

   - outline OL_AF used for this statement

 

24 rows fetched.
從22.2.10.100升級到23.2.3.100,同時開啟outline,最佳化器優先使用outline儲存執行計劃
點選檢視程式碼
SQL> select * from v$version;

 

BANNER VERSION_NUMBER

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

Enterprise Edition Release 23.2.3.100 x86_64 23.2.3.100

 

YashanDB Server Enterprise Edition Release 23.2.3.100 x86_64 - X86 64bit Linux

 

SQL> explain

select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

 

PLAN_DESCRIPTION

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 1| 29( 0)| |

| 2 | SORT DISTINCT | | | 1| 29( 0)| |

| 3 | NESTED INDEX LOOPS INNER | | | 1| 29( 0)| |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| 28( 0)| |

|* 5 | INDEX RANGE SCAN | IDX_COL3 | SYS | 1| 1( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   1 - Limit Expression: (LIMIT: 10)

   2 - Distinct Expression: ("T2"."COL2")

   4 - Predicate : filter("T2"."COL2" = 3)

   5 - Predicate : access("T3"."COL1" = "T2"."COL2")

 

21 rows fetched.

 

SQL>

exec DBMS_STATS.GATHER_DATABASE_STATS('GATHER AUTO', 1, 16, 'FOR ALL COLUMNS SIZE AUTO', 'AUTO', TRUE, FALSE);

 

SQL>

PL/SQL Succeed.

 

SQL>

ALTER SESSION SET USE_STORED_OUTLINES=ctgy_ab;

 

Succeed.

 

SQL> explain select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

 

PLAN_DESCRIPTION

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| Id | Operation type | Name | Owner | Rows | Cost(%CPU) | Partition info |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | |

| 1 | WINDOW | | | 1| 767( 0)| |

| 2 | SORT DISTINCT | | | 1| 767( 0)| |

|* 3 | NESTED LOOPS INNER | | | 1| 767( 0)| |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| 28( 0)| |

| 5 | INDEX FAST FULL SCAN | IDX_COL3 | SYS | 10000000| 28( 0)| |

+----+--------------------------------+----------------------+------------+----------+-------------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   1 - Limit Expression: (LIMIT: 10)

   2 - Distinct Expression: ("T2"."COL2")

   3 - Predicate : filter("T2"."COL2" = "T3"."COL1")

   4 - Predicate : filter("T2"."COL2" = 3)

                                                                

Outline Information :

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

                                                                

   - outline OL_AF used for this statement

 

26 rows fetched.

 

 

SQL> set autotrace on;

SQL> select distinct t2.col2,t3.col1 from test_tab2 t2 inner join test_tab3 t3 on t2.col2=t3.col1

where t2.col2 =3 limit 10;

   2

 

COL2 COL1

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

3 3

 

 

 

 

Execution Plan

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

SQL hash value: 4249347504

Optimizer: ADOPT_C

                                                                

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

| Id | Operation type | Name | Owner | E - Rows | A - Rows | Cost(%CPU) | A - Time | Loops | Memory | Disk | Partition info |

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

| 0 | SELECT STATEMENT | | | | | | | | | | |

| 1 | WINDOW | | | 1| | 767( 0)| | | | | |

| 2 | SORT DISTINCT | | | 1| | 767( 0)| | | | | |

|* 3 | NESTED LOOPS INNER | | | 1| | 767( 0)| | | | | |

|* 4 | INDEX FAST FULL SCAN | IDX_COL2 | SYS | 1| | 28( 0)| | | | | |

| 5 | INDEX FAST FULL SCAN | IDX_COL3 | SYS | 10000000| | 28( 0)| | | | | |

+----+--------------------------------+----------------------+------------+----------+----------+-------------+----------+----------+----------+----------+--------------------------------+

                                                                

Operation Information (identified by operation id):

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

                                                                

   1 - Limit Expression: (LIMIT: 10)

   2 - Distinct Expression: ("T2"."COL2")

   3 - Predicate : filter("T2"."COL2" = "T3"."COL1")

   4 - Predicate : filter("T2"."COL2" = 3)

                                                                

Outline Information :

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

                                                                

   - outline OL_AF used for this statement

 

 

 

 

Statistics

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

 

27 rows fetched.

測試總結

相關文章