公眾號:MCNU雲原生,文章首發地,歡迎微信搜索關註,更多乾貨,第一時間掌握! @ 一、PostgreSQL是什麼? PostgreSQL是一種開源的關係型資料庫管理系統,也被稱為Postgres。它最初由加拿大電腦科學家Michael Stonebraker在1986年創建,其目標是創建一個具有 ...
[20230308]12c以上版本模糊查詢問題.txt
--//前幾天看了鏈接http://www.itpub.net/thread-2148700-1-1.html,對方提到模糊查詢慢的問題,實際上這個問題使用常規模式基本
--//無解,僅僅使用全文本檢索,當然對方解析也很慢我就不知道為什麼了。
--//不過我突然想起我去年看https://jonathanlewis.wordpress.com/2022/07/15/index-wildcard/的鏈接提到的情況,當時因為其他事
--//情,僅僅看了帖子,但是自己忘了自己測試一下。
--//簡單說明實際應用設置cursor_sharing = force,如果查詢使用column_name like '%XYZ%'之類查詢時,12c以上版本邏輯讀很很高
--//的情況。
1.環境:
[email protected]:1521/orcl> @ pr
==============================
PORT_STRING : x86_64/Linux 2.4.xx
VERSION : 18.0.0.0.0
BANNER : Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
BANNER_FULL : Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0
BANNER_LEGACY : Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
CON_ID : 0
PL/SQL procedure successfully completed.
2.建立測試例子:
create table t1 as select * from all_objects ;
--//註:原始鏈接將結果插入5次,執行1次一樣可以測試出來作者遇到的問題。
create index t1_id on t1(object_name);
--//分析略。
alter session set cursor_sharing = force;
alter session set statistics_level = all;
alter session set events '10053 trace name context forever';
[email protected]:1521/orcl> select count(*) from t1;
COUNT(*)
----------
69688
3.測試:
[email protected]:1521/orcl> select /*+ index(t1(object_name)) cursor_sharing_exact */ t1.* from t1 t1 where object_name like '%XYZ%';
no rows selected
--//執行計劃如下:
[email protected]:1521/orcl> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID dwduhqcm4r08c, child number 0
-------------------------------------
select /*+ index(t1(object_name)) cursor_sharing_exact */ t1.* from t1
t1 where object_name like '%XYZ%'
Plan hash value: 2798063786
---------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 2204 (100)| | 0 |00:00:00.02 | 454 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 | 3484 | 452K| 2204 (1)| 00:00:01 | 0 |00:00:00.02 | 454 |
|* 2 | INDEX FULL SCAN | T1_ID | 1 | 3484 | | 455 (1)| 00:00:01 | 0 |00:00:00.02 | 454 |
---------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T1@SEL$1
2 - SEL$1 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("OBJECT_NAME" LIKE '%XYZ%')
--//3484/69688 = .04999,基本可以推斷按照5%估算.
--//設置cursor_sharing_exact,也就是cursor_sharing = force無效,裡面的常量'%XYZ%'不會轉義為:SYS_B_0變數。
[email protected]:1521/orcl> select /*+ index(t1(object_name)) */ t1.* from t1 t1 where object_name like '%XYZ%';
no rows selected
[email protected]:1521/orcl> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID azfnvmrr42y1k, child number 0
-------------------------------------
select /*+ index(t1(object_name)) */ t1.* from t1 t1 where
object_name like :"SYS_B_0"
Plan hash value: 539998951
---------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 1774 (100)| | 0 |00:00:00.11 | 35419 |
|* 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 | 3484 | 452K| 1774 (0)| 00:00:01 | 0 |00:00:00.11 | 35419 |
|* 2 | INDEX RANGE SCAN | T1_ID | 1 | 3484 | | 25 (0)| 00:00:01 | 69688 |00:00:00.02 | 454 |
---------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T1@SEL$1
2 - SEL$1 / T1@SEL$1
Peeked Binds (identified by position):
--------------------------------------
1 - :1 (VARCHAR2(30), CSID=1): '%XYZ%'
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("OBJECT_NAME" LIKE :SYS_B_0)
2 - access("OBJECT_NAME" LIKE :SYS_B_0)
32 rows selected.
--//執行計劃選擇INDEX RANGE SCAN,理論我的測試要掃描全部索引,buffers=454可以很前面的INDEX FULL SCAN對上,但是ID=1的
--//Buffers=35419,比前面的測試高許多.我開始也犯渾,所以留下很深的印象,看作者講解才發現filer發生在id=1上,也就是表上.
--//這樣邏輯讀很高就很正常了,如果filter發生在id=2就沒有這個高的邏輯讀了.
[email protected]:1521/orcl> select * from v$system_fix_control where SQL_FEATURE like '%ACCESS_PATH%' and DESCRIPTION like '%LIKE%';
BUGNO VALUE SQL_FEATURE DESCRIPTION OPTIMIZER_FEATURE_ENABLE EVENT IS_DEFAULT CON_ID
-------- ----- ------------------------------ ---------------------------------------------------------------- ------------------------ ----- ---------- ------
3628118 1 QKSFM_ACCESS_PATH_3628118 Do not consider LIKE with leading wildcard as index key 10.2.0.1 0 1 3
9011016 1 QKSFM_ACCESS_PATH_9011016 use 1/NDV+1/NROWS for col1 LIKE col2 selectivities (index driver 11.2.0.2 0 1 3
9303766 1 QKSFM_ACCESS_PATH_9303766 use 1/NDV+1/NROWS for col1 LIKE col2 selectivities (table access 11.2.0.2 0 1 3
20289688 1 QKSFM_ACCESS_PATH_20289688 check for leading wildcard in LIKE with bind 12.2.0.1 0 1 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--//DESCRIPTION的信息竟然顯示不全VARCHAR2(64)!!
--//利用參數提示opt_param('_fix_control' '20289688:0')
[email protected]:1521/orcl> select /*+ index(t1(object_name)) opt_param('_fix_control' '20289688:0') */ t1.* from t1 t1 where object_name like '%XYZ%';
no rows selected
[email protected]:1521/orcl> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID dss7wrqs7zuv9, child number 0
-------------------------------------
select /*+ index(t1(object_name)) opt_param('_fix_control'
'20289688:0') */ t1.* from t1 t1 where object_name like :"SYS_B_0"
Plan hash value: 539998951
---------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 1774 (100)| | 0 |00:00:00.03 | 454 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 | 3484 | 452K| 1774 (0)| 00:00:01 | 0 |00:00:00.03 | 454 |
|* 2 | INDEX RANGE SCAN | T1_ID | 1 | 3484 | | 25 (0)| 00:00:01 | 0 |00:00:00.03 | 454 |
---------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T1@SEL$1
2 - SEL$1 / T1@SEL$1
Peeked Binds (identified by position):
--------------------------------------
1 - :1 (VARCHAR2(30), CSID=1): '%XYZ%'
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_NAME" LIKE :SYS_B_0)
filter("OBJECT_NAME" LIKE :SYS_B_0)
32 rows selected.
--//OK!!
--//採用opt_param('_optim_peek_user_binds' 'false')也可以解決問題.
[email protected]:1521/orcl> select /*+ index(t1(object_name)) opt_param('_optim_peek_user_binds' 'false') */ t1.* from t1 t1 where object_name like '%XYZ%';
no rows selected
[email protected]:1521/orcl> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID dh5u4yug04fzp, child number 0
-------------------------------------
select /*+ index(t1(object_name)) opt_param('_optim_peek_user_binds'
'false') */ t1.* from t1 t1 where object_name like :"SYS_B_0"
Plan hash value: 539998951
---------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 322 (100)| | 0 |00:00:00.03 | 454 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 | 3484 | 452K| 322 (0)| 00:00:01 | 0 |00:00:00.03 | 454 |
|* 2 | INDEX RANGE SCAN | T1_ID | 1 | 627 | | 7 (0)| 00:00:01 | 0 |00:00:00.03 | 454 |
---------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T1@SEL$1
2 - SEL$1 / T1@SEL$1
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_NAME" LIKE :SYS_B_0)
filter("OBJECT_NAME" LIKE :SYS_B_0)
27 rows selected.
3.如果在生產系統遇到設置cursor_sharing = force的情況,目前的oracle版本遇到這類問題該如何解決呢?
--//還可以執行如下:
SCOTT@test01p> ALTER SYSTEM SET "_fix_control" = '20289688:0';
System altered.
[email protected]:1521/orcl> ALTER SYSTEM SET "_fix_control" = '20289688:0';
System altered.
[email protected]:1521/orcl> select * from v$system_fix_control where bugno=20289688;
BUGNO VALUE SQL_FEATURE DESCRIPTION OPTIMIZER_FEATURE_ENABLE EVENT IS_DEFAULT CON_ID
-------- ----- -------------------------- -------------------------------------------- ------------------------ ----- ---------- ------
20289688 0 QKSFM_ACCESS_PATH_20289688 check for leading wildcard in LIKE with bind 12.2.0.1 0 0 3
[email protected]:1521/orcl> Select /*+ index(t1(object_name)) */ t1.* from t1 t1 where object_name like '%XYZ%';
no rows selected
[email protected]:1521/orcl> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID dytp4h89b4p3x, child number 0
-------------------------------------
Select /*+ index(t1(object_name)) */ t1.* from t1 t1 where
object_name like :"SYS_B_0"
Plan hash value: 539998951
---------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | A-Rows | A-Time | Buffers |
---------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 1774 (100)| | 0 |00:00:00.02 | 454 |
| 1 | TABLE ACCESS BY INDEX ROWID BATCHED| T1 | 1 | 3484 | 452K| 1774 (0)| 00:00:01 | 0 |00:00:00.02 | 454 |
|* 2 | INDEX RANGE SCAN | T1_ID | 1 | 3484 | | 25 (0)| 00:00:01 | 0 |00:00:00.02 | 454 |
---------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 / T1@SEL$1
2 - SEL$1 / T1@SEL$1
Peeked Binds (identified by position):
--------------------------------------
1 - :1 (VARCHAR2(30), CSID=1): '%XYZ%'
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_NAME" LIKE :SYS_B_0)
filter("OBJECT_NAME" LIKE :SYS_B_0)
32 rows selected.
--//還原:
[email protected]:1521/orcl> ALTER SYSTEM SET "_fix_control" = '20289688:1';
System altered.
[email protected]:1521/orcl> ALTER SYSTEM RESET "_fix_control";
System altered.
--//如果19c版本還支持使用dbms_optim_bundle包.當前版本不支持.
--//https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/dbms_optim_bundle.html#GUID-D9DBDC73-38A2-428A-BC27-7CA8EDC67E8A
118.2.4 SET_FIX_CONTROLS Procedure
The DBMS_OPTIM_BUNDLE subprogram, SET_FIX_CONTROLS procedure enables or disables a list of fixes with _fix_controls. The
fixes can be present in a base version, in a release update, or in a one-off release. This procedure appends the new fix
control settings to the existing ones.
Syntax
DBMS_OPTIM_BUNDLE.SET_FIX_CONTROLS (
fix_control_string IN VARCHAR2,
sid IN VARCHAR2 DEFAULT '*',
scope IN VARCHAR2 DEFAULT 'MEMORY',
current_setting_precedence IN VARCHAR2 DEFAULT 'YES');
--//exec dbms_optim_bundle.set_fix_controls('20289688:0','*', 'BOTH', 'NO');
--//關於模糊查詢我個人建議還是儘量減少前面使用%的情況.