Oracle

為什麼 /+ NO_INDEX/ 不提示,影響“執行計劃”?

  • July 25, 2021

我有一個具有以下結構的範例表:

create table mi_dimcustomer
(customer_num    number(10),
<other columns> <data types>)

並且有一個unique indexon 列customer_num。我試圖hint the optimizer不像這樣使用這個索引(只是為了練習):

select /*+gather_plan_statistics*/ /*+ no_index(t idx1_dimcustomer) */
*
 from mi_dimcustomer t
where t.customer_num = 12;

但是在執行計劃中,我可以看到優化器還在使用索引!這是我擷取執行計劃的方式:

Step-1)

   select sql_id, child_number, sql_text
     from v$sql
    where sql_text like '%where t.CUSTOMER_NUM = 12%';

Step-2)    

select *
 from table(dbms_xplan.display_cursor('2qataxp9mahpj',
                                      '0',
                                      'ALLSTATS LAST +COST +OUTLINE'))

您可以在下面看到執行計劃:

SQL_ID  2qataxp9mahpj, child number 0
-------------------------------------
select /*+gather_plan_statistics*//*+ NO_INDEX(t idx1_dimcustomer) */ * 
from mi_dimcustomer t where t.CUSTOMER_NUM = 12

Plan hash value: 3784660444

-----------------------------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name             | Starts | E-Rows | Cost (%CPU)| A-Rows |   A-Time   | Buffers |
-----------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                  |      1 |        |     2 (100)|      1 |00:00:00.01 |       3 |
|   1 |  TABLE ACCESS BY INDEX ROWID| MI_DIMCUSTOMER   |      1 |      1 |     2   (0)|      1 |00:00:00.01 |       3 |
|*  2 |   INDEX UNIQUE SCAN         | IDX1_DIMCUSTOMER |      1 |      1 |     1   (0)|      1 |00:00:00.01 |       2 |
-----------------------------------------------------------------------------------------------------------------------

為什麼會這樣?

提前致謝

優化器提示概述

…一個語句塊只能有一個包含提示的註釋,…

select /*+gather_plan_statistics no_index(t idx1_dimcustomer) */
*
 from mi_dimcustomer t
where t.customer_num = 12;

引用自:https://dba.stackexchange.com/questions/296131