首页 > 其他分享 >[20230308]12c以上版本模糊查询问题.txt

[20230308]12c以上版本模糊查询问题.txt

时间:2023-03-12 16:45:59浏览次数:39  
标签:00 12c name T1 20230308 SEL object t1 txt

[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');
--//关于模糊查询我个人建议还是尽量减少前面使用%的情况.

标签:00,12c,name,T1,20230308,SEL,object,t1,txt
From: https://www.cnblogs.com/lfree/p/17208433.html

相关文章

  • C++从txt中读取矩阵
    1.分析给定一个txt数据,中间由空格分割,目标是读取数据,以便后续使用。由于不清楚数据大小,为了方便管理,采用vector容器作为存贮对象。   2.程序下面是读取的方法......
  • python 批量提取.txt文件的前缀名称
    importtkinterastk#导入tkinter库设置别名tkimportosimporttimeimportglobroot=tk.Tk()#生成主窗口root.title('文件提取器')#设置窗体名字root......
  • 分布式事务-最大努力通知2 20230308
          ......
  • 20230308总结
    总之就是很寄,很寄。T1:想出来前缀和了,但是没想出来怎么优化,于是心态没了,于是就gg了,后面也没想暴力,我很菜。T1可以dfsmndp过nm二分过但是我一个没想出来我很废物......
  • 每日学习总结_20230308
    今天的JavaWeb学习主要集中在Servlet和JSP的使用上。我学习了如何创建一个Servlet并且在Web应用程序中进行部署和调试。同时,我还学习了如何在JSP中使用Java代码,并了解了JSP......
  • 02、CMakeLists.txt基本写法
    cmake_minimum_required(VERSION3.10)#设置c++标准#set(CMAKE_CXX_STANDARD11)#设置输出文件存放目录set(CMAKE_RUNTIME_OUTPUT_DIRECTORY${CMAKE_BINARY_DIR}/......
  • pip freeze >requirements.txt
    python项目中必须包含一个requirements.txt文件,用于记录所有依赖包及其精确的版本号。以便重新环境部署。使用pip生成:pipfreeze>requirements.txt会生成一个requirem......
  • python txt转MP3
    1.安装依赖pipinstallpyttsx3pipinstalllibrosapipinstallpydub 2.代码程序#encoding=utf8#-*-coding:utf-8-*-'''python将一段文字转为mp3音频文件......
  • CMakeLists.txt Import third-party source code
    Mode1cmake_minimum_required(VERSION3.19)project(test_sha_aes)set(CMAKE_CXX_STANDARD14)add_executable(mainmain.cppsha/sha2.caes/aes.caes/aes_crypto......
  • Kernel文档 DeviceTree——usage-model.txt
    此文介绍Linux的设备树使用模范。OpenFirmware设备树是用于描述硬件的数据结构和语言。他是一种对硬件的描述,此描述是可被操作系统读的,所以OS不需要硬编码机器的详细信......