in_list 11g優化器改進

yangzhangyue發表於2013-07-24
測試指令碼:
create table audience as
select
        trunc(dbms_random.value(1,13))  month_no
from
        all_objects
where
        rownum <= 1200
;

begin
        dbms_stats.gather_table_stats(
                user,
                'audience',
                cascade => true,
                estimate_percent => null,
                method_opt => 'for all columns size 1'
        );
end;
/
set autotrace traceonly explain

select count(*) from audience
where month_no = 25
;

select count(*) from audience
where month_no in (4, 4)
;

select count(*) from audience
where month_no in (3, 25)
;

select count(*) from audience
where month_no in (3, 25, 26)
;

select count(*) from audience
where month_no in (3, 25, 25, 26)
;

select count(*) from audience
where month_no in (3, 25, null)
;

select count(*) from audience
where month_no in (:b1, :b2, :b3)
;

set autotrace off

現象:
10g
Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |     1 |     3 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=25)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   100 |   300 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=4)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   100 |   300 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=3 OR "MONTH_NO"=25)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   101 |   303 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=3 OR "MONTH_NO"=25 OR "MONTH_NO"=26)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   101 |   303 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=3 OR "MONTH_NO"=25 OR "MONTH_NO"=26)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   200 |   600 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=3 OR "MONTH_NO"=25 OR
              "MONTH_NO"=TO_NUMBER(NULL))

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   300 |   900 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=TO_NUMBER(:B1) OR "MONTH_NO"=TO_NUMBER(:B2) OR
              "MONTH_NO"=TO_NUMBER(:B3))

11g:
Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |     1 |     3 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=25)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   100 |   300 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=4)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   100 |   300 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=3 OR "MONTH_NO"=25)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   101 |   303 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=3 OR "MONTH_NO"=25 OR "MONTH_NO"=26)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   101 |   303 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=3 OR "MONTH_NO"=25 OR "MONTH_NO"=26)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   100 |   300 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=3 OR "MONTH_NO"=25)

Note
-----
   - cpu costing is off (consider enabling it)


Execution Plan
----------------------------------------------------------
Plan hash value: 3337892515

---------------------------------------------------------------
| Id  | Operation          | Name     | Rows  | Bytes | Cost  |
---------------------------------------------------------------
|   0 | SELECT STATEMENT   |          |     1 |     3 |     2 |
|   1 |  SORT AGGREGATE    |          |     1 |     3 |       |
|*  2 |   TABLE ACCESS FULL| AUDIENCE |   300 |   900 |     2 |
---------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("MONTH_NO"=TO_NUMBER(:B1) OR "MONTH_NO"=TO_NUMBER(:B2) OR
              "MONTH_NO"=TO_NUMBER(:B3))

   觀測紅色字型部分的內容。我們會發現11g中能更精確的計算基數card的值。在劉易斯《基於成本的oracle優化法則中》,他所測試的9i/10g還不能正確計算超出最大最小值界限,只可以很好的處理重複值
我所測試的10g版本已經可以很好的處理最大最小值的問題。11.2.0.3.6中進一步完善了演算法,可以更好的處理異常值,但幫定變數還不行

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29033984/viewspace-767028/,如需轉載,請註明出處,否則將追究法律責任。

相關文章