[20220811]奇怪的隱式轉換問題.txt

lfree發表於2022-08-11

[20220811]奇怪的隱式轉換問題.txt

--//生產系統遇到一個奇怪的隱式轉換問題,問題在於沒有發生隱式轉換,做一個分析調查。

--//後記:後面的分析存在一下偏差,不更正當時的分析,在這裡做一個補充,實際上從12.2版本開始,oracle就支援這樣的情況,當使
--//用繫結變數時,帶入的繫結變數引數是timestamp型別時,不再存在隱式轉換。即使秒後面的值非0!!
--//我另外寫一篇 [20220811]奇怪的隱式轉換問題(12c補充測試).txt 來說明問題。
--//感覺oracle 一個小小的改進,很奇怪我感覺至少我這之前沒見過別人寫過或者遇到這樣的情況。

1.環境:
SYS@192.168.100.235:1521/orcl> @ pr
==============================
PORT_STRING                   : x86_64/Linux 2.4.xx
VERSION                       : 19.0.0.0.0
BANNER                        : Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
BANNER_FULL                   : Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
BANNER_LEGACY                 : Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
CON_ID                        : 0

PL/SQL procedure successfully completed.

2.分析:
SYS@192.168.100.235:1521/orcl> @dashtop sql_id 1=1 &day
    Total
  Seconds     AAS %This   SQL_ID        FIRST_SEEN          LAST_SEEN
--------- ------- ------- ------------- ------------------- -------------------
    18420      .2   36%                 2022-08-10 08:54:16 2022-08-11 07:59:14
     4420      .1    9%   4qz6aykj6gq6v 2022-08-10 08:54:37 2022-08-11 07:46:32
     2640      .0    5%   gxak7guzxkwbh 2022-08-10 08:54:27 2022-08-11 07:23:29
     2000      .0    4%   3rhg88u6qnt7h 2022-08-10 10:01:05 2022-08-11 07:23:29
     1690      .0    3%   79ymhrhpk0x3p 2022-08-10 08:55:47 2022-08-11 07:54:03
     1600      .0    3%   9akuv534yadhw 2022-08-10 10:13:57 2022-08-11 07:55:33
     1100      .0    2%   0bzc2mv4s5f30 2022-08-10 08:55:37 2022-08-11 07:46:42
     1030      .0    2%   18xh7p6w137q5 2022-08-10 08:55:17 2022-08-11 07:55:43
...
30 rows selected.
--//分析sql_id=4qz6aykj6gq6v.

SYS@192.168.100.235:1521/orcl> @ sql_id 4qz6aykj6gq6v
--SQL_ID = 4qz6aykj6gq6v
select a.*,1 as TenantId from lis_test a  where   test_date=:end_date and inst_id=:inst_id   AND SENT_INST_FLAG = 0 AND SENT_COUNT<3 and state < :state;

SYS@192.168.100.235:1521/orcl> @ dpc 4qz6aykj6gq6v '' '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  4qz6aykj6gq6v, child number 5
-------------------------------------
select a.*,1 as TenantId from lis_test a  where   test_date=:end_date
and inst_id=:inst_id   AND SENT_INST_FLAG = 0 AND SENT_COUNT<3 and
state < :state
Plan hash value: 557083498
-----------------------------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name                           | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
-----------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |                                |        |       | 52419 (100)|          |
|*  1 |  TABLE ACCESS BY INDEX ROWID BATCHED| LIS_TEST                       |      4 |  2784 | 52419   (1)| 00:00:03 |
|*  2 |   INDEX SKIP SCAN                   | IX_LIS_TEST_TEST_NO__831848037 |    249 |       | 52170   (1)| 00:00:03 |
-----------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / A@SEL$1
   2 - SEL$1 / A@SEL$1
Peeked Binds (identified by position):
--------------------------------------
   1 - :1 (TIMESTAMP): [Not Printable]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   2 - :2 (NUMBER): 2490
   3 - (NUMBER): 90
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(("STATE"<:STATE AND "SENT_INST_FLAG"=0 AND "SENT_COUNT"<3))
   2 - access("TEST_DATE"=:END_DATE AND "INST_ID"=:INST_ID)
       filter(("TEST_DATE"=:END_DATE AND "INST_ID"=:INST_ID))
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level
42 rows selected.
--//索引的名字命名有點怪怪的IX_LIS_TEST_TEST_NO__831848037,好像sql profile提示建立的索引不應該是這樣的格式,猜測該產品
--//早期可能在ms sqlserver下開發的。執行計劃走的是INDEX SKIP SCAN,前面一個欄位是test_no,自然執行效率低下。
--//另外注意下劃線可以發現引數1型別timestamp型別,開始沒有注意這個細節。

SYS@192.168.100.235:1521/orcl> @ ind2 LIS.IX_LIS_TEST_TEST_NO__831848037
Display indexes where table or index name matches LIS.IX_LIS_TEST_TEST_NO__831848037...
TABLE_OWNER          TABLE_NAME                     INDEX_NAME                     POS# COLUMN_NAME                    DSC
-------------------- ------------------------------ ------------------------------ ---- ------------------------------ ----
LIS                  LIS_TEST                       IX_LIS_TEST_TEST_NO__831848037    1 TEST_NO
                                                                                      2 TEST_DATE
                                                                                      3 INST_ID
                                                                                      4 NO_PREFIX

INDEX_OWNER          TABLE_NAME                     INDEX_NAME                     IDXTYPE    UNIQ STATUS   PART TEMP  H     LFBLKS           NDK   NUM_ROWS       CLUF LAST_ANALYZED       DEGREE VISIBILIT
-------------------- ------------------------------ ------------------------------ ---------- ---- -------- ---- ---- -- ---------- ------------- ---------- ---------- ------------------- ------ ---------
LIS                  LIS_TEST                       IX_LIS_TEST_TEST_NO__831848037 NORMAL     YES  VALID    NO   N     3      52158       8238727    8238727    8217923 2022-08-09 22:19:23 1      VISIBLE



SYS@192.168.100.235:1521/orcl> @ descz LIS.LIS_TEST "column_name in ('TEST_NO','TEST_DATE','INST_ID','SENT_INST_FLAG','SENT_COUNT','STATE')"
eXtended describe of LIS.LIS_TEST

DISPLAY TABLE_NAME OF COLUMN_NAME INFORMATION.
INPUT   OWNER.TABLE_NAME  <filters>
SAMPLE  : @ TAB_LH TABLE_NAME "column_id between 3 and 5"
IF NOT INPUT <filters> ,USE "1=1" .

Owner      Table_Name           SAMPLE_SIZE LAST_ANALYZED       Col# Column Name          Null?      Type                 NUM_DISTINCT        Density  NUM_NULLS HISTOGRAM       NUM_BUCKETS Low_value           High_value
---------- -------------------- ----------- ------------------- ---- -------------------- ---------- -------------------- ------------ -------------- ---------- --------------- ----------- ------------------- -------------------
LIS        LIS_TEST                    8701 2022-08-09 22:07:12    3 TEST_NO              NOT NULL   NUMBER(19,0)               237680   .00000400000          0 HYBRID                  254 -9020               2103294101535845721
                                       8701 2022-08-09 22:07:12    4 TEST_DATE            NOT NULL   DATE(7)                       561   .00177900000          0 HYBRID                  254 0001-01-01 00:00:00 9773-06-08 00:00:00
                                    8238717 2022-08-09 22:07:12    5 INST_ID              NOT NULL   NUMBER(10,0)                  226   .00000006069          0 FREQUENCY               226 1530                4090
                                    8238717 2022-08-09 22:07:12   28 SENT_INST_FLAG       NOT NULL   NUMBER(10,0)                    2   .00000006069          0 FREQUENCY                 2 0                   1
                                    8238717 2022-08-09 22:07:12   29 SENT_COUNT           NOT NULL   NUMBER(10,0)                  224   .00000006069          0 FREQUENCY               224 0                   393
                                    8238717 2022-08-09 22:07:12  108 STATE                           NUMBER(10,0)                    5   .00000006069          0 FREQUENCY                 5 60                  100
6 rows selected.
--//呵呵還可以看出TEST_DATE存在垃圾資料。High_value=9773-06-08 00:00:00.

--//檢視繫結變數值如下:
SYS@192.168.100.235:1521/orcl> @ bind_cap  4qz6aykj6gq6v ''
SQL_ID        CHILD_NUMBER WAS NAME      POSITION MAX_LENGTH LAST_CAPTURED       DATATYPE_STRING VALUE_STRING C30
------------- ------------ --- --------- -------- ---------- ------------------- --------------- ------------ ------------------------------
4qz6aykj6gq6v            5 YES :END_DATE        1         11 2022-08-11 08:54:14 TIMESTAMP                    2022-08-11 00:00:00.000000000
                           YES :INST_ID         2         22 2022-08-11 08:54:14 NUMBER          2390
                           YES :STATE           3         22 2022-08-11 08:54:14 NUMBER          90
3 rows selected.
--//注意一個細節變數:END_DATE的資料型別帶入的是TIMESTAMP。而前面的顯示執行計劃顯示的是access,filter確是
--//"TEST_DATE"=:END_DATE,而欄位TEST_DATE的定義資料型別是date(7),也就是執行計劃沒有發生沒有執行隱式轉換。
--//開始懷疑是否存在打入引數即有date型別又有timestamp型別,開始沒有注意Peeked Binds (identified by position):部分顯示,
Peeked Binds (identified by position):
--------------------------------------
   1 - :1 (TIMESTAMP): [Not Printable]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   2 - :2 (NUMBER): 2490
   3 - (NUMBER): 90
--//不然就不會在開始最佳化開始時走彎路了。
--//查詢如下:

SYS@192.168.100.235:1521/orcl> @ bind_cap_awr  4qz6aykj6gq6v END_DATE
@bind_cap_awr sql_id [column]
1 row selected.

   SNAP_ID INSTANCE_NUMBER SQL_ID        WAS LAST_CAPTURED       NAME      POSITION MAX_LENGTH DATATYPE_STRING VALUE_STRING C30
---------- --------------- ------------- --- ------------------- --------- -------- ---------- --------------- ------------ ------------------------------
     14720               1 4qz6aykj6gq6v YES 2022-08-02 22:48:44 :END_DATE        1         11 TIMESTAMP                    2022-08-02 00:00:00.000000000
     14721               1 4qz6aykj6gq6v YES 2022-08-02 23:48:48 :END_DATE        1         11 TIMESTAMP                    2022-08-02 00:00:00.000000000
     14722               1 4qz6aykj6gq6v YES 2022-08-03 00:48:52 :END_DATE        1         11 TIMESTAMP                    2022-08-03 00:00:00.000000000
     14723               1 4qz6aykj6gq6v YES 2022-08-03 01:48:54 :END_DATE        1         11 TIMESTAMP                    2022-08-03 00:00:00.000000000
     14724               1 4qz6aykj6gq6v YES 2022-08-03 02:48:58 :END_DATE        1         11 TIMESTAMP                    2022-08-03 00:00:00.000000000
     14725               1 4qz6aykj6gq6v YES 2022-08-03 03:49:01 :END_DATE        1         11 TIMESTAMP                    2022-08-03 00:00:00.000000000
--//snip....
     14921               1 4qz6aykj6gq6v YES 2022-08-11 07:54:12 :END_DATE        1         11 TIMESTAMP                    2022-08-11 00:00:00.000000000
     14922               1 4qz6aykj6gq6v YES 2022-08-11 08:54:14 :END_DATE        1         11 TIMESTAMP                    2022-08-11 00:00:00.000000000
203 rows selected.
--//很明顯不存在引數即有date型別又有timestamp型別的情況。

SYS@192.168.100.235:1521/orcl> @ cntg  LIS.LIS_TEST SENT_INST_FLAG
select count(*) , SENT_INST_FLAG  from LIS.LIS_TEST group by SENT_INST_FLAG order by 1 desc;
  COUNT(*) SENT_INST_FLAG
---------- --------------
   5129342              0
   3135070              1
2 rows selected.

SYS@192.168.100.235:1521/orcl> @ cntg  LIS.LIS_TEST SENT_count
select count(*) , SENT_count  from LIS.LIS_TEST group by SENT_count order by 1 desc;
  COUNT(*) SENT_COUNT
---------- ----------
   6186388          0
   2059983          1
     13864          2
      2230          3
...
         1        182
224 rows selected.
--//建立索引在SENT_INST_FLAG以及SENT_COUNT也是不合理的,也就是根據以上分析建立複合索引:TEST_DATE,INST_ID.而且不存在隱式
--//轉換的問題。

CREATE INDEX LIS.i_LIS_TEST_TEST_DATE_INST_ID ON LIS.LIS_TEST
(TEST_DATE, INST_ID)
LOGGING
STORAGE    (
            BUFFER_POOL      DEFAULT
            FLASH_CACHE      DEFAULT
            CELL_FLASH_CACHE DEFAULT
           )
NOPARALLEL;

--//再看執行計劃,發現
SYS@192.168.100.235:1521/orcl> @ dpc 4qz6aykj6gq6v '' '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  4qz6aykj6gq6v, child number 1
-------------------------------------
select a.*,1 as TenantId from lis_test a  where   test_date=:end_date
and inst_id=:inst_id   AND SENT_INST_FLAG = 0 AND SENT_COUNT<3 and
state < :state
Plan hash value: 2953700840
---------------------------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name                         | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
---------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |                              |        |       |   160 (100)|          |
|*  1 |  TABLE ACCESS BY INDEX ROWID BATCHED| LIS_TEST                     |      6 |  4176 |   160   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN                  | I_LIS_TEST_TEST_DATE_INST_ID |    346 |       |     4   (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / A@SEL$1
   2 - SEL$1 / A@SEL$1
Peeked Binds (identified by position):
--------------------------------------
   1 - :1 (TIMESTAMP): [Not Printable]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   
   2 - :2 (NUMBER): 2890
   3 - (NUMBER): 90
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(("STATE"<:STATE AND "SENT_INST_FLAG"=0 AND "SENT_COUNT"<3))
   2 - access("TEST_DATE"=:END_DATE AND "INST_ID"=:INST_ID)
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

--//你可以發現oracle可以使用我建立的索引。並且執行計劃走INDEX RANGE SCAN使用I_LIS_TEST_TEST_DATE_INST_ID索引。
--//我手工生成指令碼,注在sqlplus使用variable無法定義date,timestamp型別,只能使用字串,注意帶入日期的格式要符合要求:
SYS@192.168.100.235:1521/orcl> @b9  4qz6aykj6gq6v 0 1
variable END_DATE VARCHAR2(32)
variable INST_ID NUMBER
variable STATE NUMBER
begin
:END_DATE := '2022/08/11 00:00:00';
:INST_ID := 2490;
:STATE := 90;
end;
/
set termout off
set sqlblanklines on
alter session set current_schema=LIS;
alter session set statistics_level=all;

select a.*,1 as TenantId from lis_test a  where   test_date=:end_date and inst_id=:inst_id   AND SENT_INST_FLAG = 0 AND SENT_COUNT<3 and state < :state;
set termout on
set sqlblanklines off
--@zws '' ''
--@dpc '' ''
@dpc '' outline
rollback;
alter session set current_schema=SYS ;
--//注我取消秒後面的000000000.不然報錯我開始就是透過這裡發現存在資料型別"轉換"問題的。

--//執行計劃如下:
SQL_ID  4qz6aykj6gq6v, child number 2
-------------------------------------
select a.*,1 as TenantId from lis_test a  where   test_date=:end_date
and inst_id=:inst_id   AND SENT_INST_FLAG = 0 AND SENT_COUNT<3 and
state < :state
Plan hash value: 2953700840
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name                         | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers | Reads  |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |                              |      1 |        |       |   157 (100)|          |      0 |00:00:00.01 |     335 |      4 |
|*  1 |  TABLE ACCESS BY INDEX ROWID BATCHED| LIS_TEST                     |      1 |      6 |  4176 |   157   (0)| 00:00:01 |      0 |00:00:00.01 |     335 |      4 |
|*  2 |   INDEX RANGE SCAN                  | I_LIS_TEST_TEST_DATE_INST_ID |      1 |    338 |       |     4   (0)| 00:00:01 |    522 |00:00:00.01 |       5 |      4 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / A@SEL$1
   2 - SEL$1 / A@SEL$1
Peeked Binds (identified by position):
--------------------------------------
   1 - :1 (VARCHAR2(30), CSID=852): '2022/08/11 00:00:00'
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   2 - :2 (NUMBER): 2490
   3 - (NUMBER): 90
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(("STATE"<:STATE AND "SENT_INST_FLAG"=0 AND "SENT_COUNT"<3))
   2 - access("TEST_DATE"=:END_DATE AND "INST_ID"=:INST_ID)

--//可以發現也可以使用索引,但是我的測試無法反映真實的情況,我帶入的:END_DATE是varchar2(32)。toad下執行也很麻煩,toad帶入
--//的引數型別沒有timestamp型別,我也不知道如何處理。我記憶裡在itpub上問過,toad下如何使用timestamp資料型別,沒人解答這
--//個問題.

--//我個人認為問題在於帶入的timestamp型別沒有秒後面的0值,就不會出現隱式轉換。【後記:我有點想當然了】
--//也許oracle 19c版本支援這樣的操作,當帶入日期型別引數是timestamp型別時,如果秒後面的值全部是0,可以當作date型別使用。
--//導致沒有存在隱式轉換,我給在11g測試看看.

--//加入秒後面的0的情況。
$ cat 4qz6aykj6gq6v.sql9_0
variable END_DATE VARCHAR2(32)
variable INST_ID NUMBER
variable STATE NUMBER
begin
:END_DATE := '2022-08-11 00:00:00.000000000';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:INST_ID := 2490;
:STATE := 90;
end;
/
set termout off
set sqlblanklines on
alter session set current_schema=LIS;
alter session set statistics_level=all;

SElect a.*,1 as TenantId from lis_test a  where   test_date=:end_date and inst_id=:inst_id   AND SENT_INST_FLAG = 0 AND SENT_COUNT<3 and state < :state;
set termout on
set sqlblanklines off
--@zws '' ''
--@dpc '' ''
@dpc '' outline
rollback;
alter session set current_schema=SYS ;

SYS@192.168.100.235:1521/orcl> @ 4qz6aykj6gq6v.sql9_0
PL/SQL procedure successfully completed.
PLAN_TABLE_OUTPUT
-----------------------------------------
Last statement has a PREV_SQL_ID of ZERO
2 rows selected.

argment : typical all advanced partition predicate remote note parallel projection alias peeked_binds outline bytes
argment : adaptive (12c) hint_report (19c)
Rollback complete.
Session altered.
--//執行報錯!!也就是這樣我才發現帶入型別是timestamp型別。

--//還可以透過生產系統的其它語句驗證我的測試有效,建立的I_LIS_TEST_TEST_DATE_INST_ID索引有用。

SYS@192.168.100.235:1521/orcl> @ sql_id 18xh7p6w137q5
--SQL_ID = 18xh7p6w137q5
select nvl(max(test_no),0) test_No from lis_test where test_date=:test_date and inst_id=:inst_id and TEST_NO between :StartNo and :MaxNo  and   is_initial<=1;

SYS@192.168.100.235:1521/orcl> @ bind_cap 18xh7p6w137q5  test_date
C200
-------------------------------------------------------------------------------------------------------------------------------------------------------------
select nvl(max(test_no),0) test_No from lis_test where test_date=:test_date and inst_id=:inst_id and TEST_NO between :StartNo and :MaxNo  and   is_initial<=1

SQL_ID        CHILD_NUMBER WAS NAME         POSITION MAX_LENGTH LAST_CAPTURED       DATATYPE_STRING VALUE_STRING C30
------------- ------------ --- ---------- ---------- ---------- ------------------- --------------- ------------ ------------------------------
18xh7p6w137q5            2 YES :TEST_DATE          1         11 2022-08-11 09:36:03 TIMESTAMP                    2022-08-11 00:00:00.000000000
--//一樣是timetamp型別。

SYS@192.168.100.235:1521/orcl> @ dpc  18xh7p6w137q5  ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  18xh7p6w137q5, child number 2
-------------------------------------
select nvl(max(test_no),0) test_No from lis_test where
test_date=:test_date and inst_id=:inst_id and TEST_NO between :StartNo
and :MaxNo  and   is_initial<=1
Plan hash value: 467228195
-----------------------------------------------------------------------------------------------------------------------
| Id  | Operation                             | Name                         | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
-----------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                      |                              |        |       |   316 (100)|          |
|   1 |  SORT AGGREGATE                       |                              |      1 |    20 |            |          |
|*  2 |   FILTER                              |                              |        |       |            |          |
|*  3 |    TABLE ACCESS BY INDEX ROWID BATCHED| LIS_TEST                     |     90 |  1800 |   316   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN                  | I_LIS_TEST_TEST_DATE_INST_ID |    689 |       |     5   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1
   3 - SEL$1 / LIS_TEST@SEL$1
   4 - SEL$1 / LIS_TEST@SEL$1
Peeked Binds (identified by position):
--------------------------------------
   1 - :1 (TIMESTAMP): [Not Printable]
   2 - :2 (NUMBER): 1671
   3 - (NUMBER): 1000
   4 - (NUMBER): 2000
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter(:MAXNO>=:STARTNO)
   3 - filter(("TEST_NO"<=:MAXNO AND "TEST_NO">=:STARTNO AND "IS_INITIAL"<=1))
   4 - access("TEST_DATE"=:TEST_DATE AND "INST_ID"=:INST_ID)

Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level
--//索引有效!!

SYS@192.168.100.235:1521/orcl> @ b9 18xh7p6w137q5 0 1
variable TEST_DATE VARCHAR2(32)
variable INST_ID NUMBER
variable STARTNO NUMBER
variable MAXNO NUMBER
begin
:TEST_DATE := '2022/08/11 00:00:00.000000000';
:INST_ID := 1725;
:STARTNO := 0;
:MAXNO := 300;
end;
/
set termout off
set sqlblanklines on
alter session set current_schema=LIS;
alter session set statistics_level=all;

select nvl(max(test_no),0) test_No from lis_test where test_date=:test_date and inst_id=:inst_id and TEST_NO between :StartNo and :MaxNo  and   is_initial<=1;
set termout on
set sqlblanklines off
--@zws '' ''
--@dpc '' ''
@dpc '' outline
rollback;
alter session set current_schema=SYS ;

SYS@192.168.100.235:1521/orcl> @18xh7p6w137q5.sql9_0
PL/SQL procedure successfully completed.
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------
Last statement has a PREV_SQL_ID of ZERO
2 rows selected.
argment : typical all advanced partition predicate remote note parallel projection alias peeked_binds outline bytes
argment : adaptive (12c) hint_report (19c)
Rollback complete.
Session altered.

--//順便改寫如下:
select a.*,1 as TenantId from lis_test a  where test_date=to_timestamp('2022-08-11 00:00:00.000000000','yyyy-mm-dd hh24:mi:ss.ff') and inst_id=1;

Plan hash value: 284515054
---------------------------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name                         | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |
---------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |                              |      1 |   696 |   564   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| LIS_TEST                     |      1 |   696 |   564   (0)| 00:00:01 |
|*  2 |   INDEX SKIP SCAN                   | I_LIS_TEST_TEST_DATE_INST_ID |      1 |       |   563   (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1 / A@SEL$1
   2 - SEL$1 / A@SEL$1
Outline Data
-------------
  /*+
      BEGIN_OUTLINE_DATA
      BATCH_TABLE_ACCESS_BY_ROWID(@"SEL$1" "A"@"SEL$1")
      INDEX_SS(@"SEL$1" "A"@"SEL$1" ("LIS_TEST"."TEST_DATE" "LIS_TEST"."INST_ID"))
      OUTLINE_LEAF(@"SEL$1")
      ALL_ROWS
      DB_VERSION('19.1.0')
      OPTIMIZER_FEATURES_ENABLE('19.1.0')
      IGNORE_OPTIM_EMBEDDED_HINTS
      END_OUTLINE_DATA
  */
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("INST_ID"=1)
       filter("INST_ID"=1 AND INTERNAL_FUNCTION("TEST_DATE")=TIMESTAMP' 2022-08-11 00:00:00.000000000')
--//可以驗證發生隱式轉換。
 
總結:
--//也就是在這種特殊的情況下隱式轉換並沒有發生,以前好像沒有遇到類似的問題,看來19c做了一些改進,看來給在11g下測試看看。


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

相關文章