从 oracle 12c 升级到 oracle 19c 导致查询性能下降

Posted

技术标签:

【中文标题】从 oracle 12c 升级到 oracle 19c 导致查询性能下降【英文标题】:Upgradation from oracle 12c to oracle 19c resulting in degradation in performance of Query 【发布时间】:2021-08-03 06:06:50 【问题描述】:

我已从 Oracle 12c 迁移到 Oracle 19c。在选择查询中观察到了主要的退化。我正在执行 50k - 1L 记录的选择查询,但它几乎是 Oracle 12c 的两倍。即使在获取 500 条记录时也会出现同样的问题。

select * from TABLE_NAME where ((col1='1' and col2='1' and col3='1' and col4='SAT, JUL 31 2021 00:00:00 GMT') or (col1='1' and col2='1' and col3='1' and col4='SAT, JUL 31 2021 00:00:00 GMT').........);

查询中传入的上述4列是表的唯一键。

我尝试设置 optimizer_features_enable('12.1.0.2'),但没有改善。

谁能帮帮我?

Oracle 12 的查询执行计划如下:


      "version": "1",
      "signature": "Oracle JDBC driver",
      "date": "2021-08-03T11:54:18.642048800",
      "sql": "\r\nselect\r\n       \r\n        this_.col1 as RDPD0_0_,\r\n        this_.col2 as col2_,\r\n        this_.col5 as Transfer5_0_0_,\r\n        this_.col6 as ProductN6_0_0_,\r\n        this_.RDPD1 as RDPD7_0_0_,\r\n        this_.RMPD as RMPD0_0_,\r\n        this_.RDPD3 as RDPD10_0_0_,\r\n        this_.RDPD2 as RDPD11_0_0_,\r\n       ,\r\n        this_.RMP as RMP0_0_,\r\n ,\r\n    ,\r\n       ,\r\n  this_.RDP as RDP0_0_,\r\n        this_.col1 as Product20_0_0_,\r\n        this_.col3 as RetailZ21_0_0_,\r\n        this_.col4 as Effecti22_0_0_,\r\n        this_.col2 as col20_0_ \r\n    from\r\n        TABLE_NAME this_ \r\n    where\r\n        (\r\n            (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n\t\t\t\t) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n\t\t\t or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n\t\t\t or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ))\r\n",
      "root": [
        
          "name": "",
          "kind": "Node",
          "type": "SELECT STATEMENT",
          "cond": "",
          "desc": "",
          "attributes": [
            
              "statement_id": "krvoebs9lcntv4"
            ,
            
              "plan_id": 33
            ,
            
              "timestamp": 1627971863000
            ,
            
              "remarks": ""
            ,
            
              "operation": "SELECT STATEMENT"
            ,
            
              "options": ""
            ,
            
              "object_node": ""
            ,
            
              "object_owner": ""
            ,
            
              "object_name": ""
            ,
            
              "object_alias": ""
            ,
            
              "object_instance": 0
            ,
            
              "object_type": ""
            ,
            
              "optimizer": "ALL_ROWS"
            ,
            
              "search_columns": 0
            ,
            
              "id": 0
            ,
            
              "depth": 0
            ,
            
              "position": 550
            ,
            
              "cost": 550
            ,
            
              "cardinality": 1
            ,
            
              "bytes": 143
            ,
            
              "other_tag": ""
            ,
            
              "partition_start": ""
            ,
            
              "partition_stop": ""
            ,
            
              "partition_id": 0
            ,
            
              "other": ""
            ,
            
              "distribution": ""
            ,
            
              "cpu_cost": 86299946
            ,
            
              "io_cost": 545
            ,
            
              "temp_space": 0
            ,
            
              "access_predicates": ""
            ,
            
              "filter_predicates": ""
            ,
            
              "projection": ""
            ,
            
              "time": 1
            ,
            
              "qblock_name": ""
            ,
            
              "other_xml": ""
            ,
            
              "parent_id": 0
            
          ],
          "child": [
            
              "name": "TABLE_NAME",
              "kind": "Node",
              "type": "TABLE ACCESS",
              "cond": "",
              "desc": "",
              "attributes": [
                
                  "statement_id": "krvoebs9lcntv4"
                ,
                
                  "plan_id": 33
                ,
                
                  "timestamp": 1627971863000
                ,
                
                  "remarks": ""
                ,
                
                  "operation": "TABLE ACCESS (FULL)"
                ,
                
                  "options": "FULL"
                ,
                
                  "object_node": ""
                ,
                
                  "object_owner": "ORA12_01"
                ,
                
                  "object_name": "TABLE_NAME"
                ,
                
                  "object_alias": "THIS_@SEL$1"
                ,
                
                  "object_instance": 1
                ,
                
                  "object_type": "TABLE"
                ,
                
                  "optimizer": "ANALYZED"
                ,
                
                  "search_columns": 0
                ,
                
                  "id": 1
                ,
                
                  "depth": 1
                ,
                
                  "position": 1
                ,
                
                  "cost": 550
                ,
                
                  "cardinality": 1
                ,
                
                  "bytes": 143
                ,
                
                  "other_tag": ""
                ,
                
                  "partition_start": ""
                ,
                
                  "partition_stop": ""
                ,
                
                  "partition_id": 0
                ,
                
                  "other": ""
                ,
                
                  "distribution": ""
                ,
                
                  "cpu_cost": 86299946
                ,
                
                  "io_cost": 545
                ,
                
                  "temp_space": 0
                ,
                
                  "access_predicates": ""
                ,
                
                  "filter_predicates": "TO_NUMBER(\"THIS_\".\"col2\")\u003d1 AND TO_NUMBER(\"THIS_\".\"col3\")\u003d1 AND TO_NUMBER(\"THIS_\".\"col1\")\u003d1 AND \"THIS_\".\"col4\"\u003dTO_TIMESTAMP(\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027)"
                ,
                
                  "projection": "\"THIS_\".\"col1\"[NUMBER,22], \"THIS_\".\"RDPD\"[NUMBER,22], \"THIS_\".\"col6\"[VARCHAR2,400], \"THIS_\".\"col1\"[VARCHAR2,400], \"THIS_\".\"col2\"[VARCHAR2,400], \"THIS_\".\"col2\"[VARCHAR2,400], \"THIS_\".\"RDPD1\"[NUMBER,22], \"THIS_\".\"RMPD\"[NUMBER,22],\"THIS_\".\"RDPD3\"[NUMBER,22], \"THIS_\".\"RDPD2\"[NUMBER,22], \"THIS_\".\"RMP\"[NUMBER,22], \"THIS_\".\"col5\"[NUMBER,22], \"THIS_\".\"col3\"[VARCHAR2,400], \"THIS_\".\"DATEMODIFIED\"[TIMESTAMP,11], \"THIS_\".\"col4\"[TIMESTAMP,11], \"THIS_\".\"RDP\"[NUMBER,22]"
                ,
                
                  "time": 1
                ,
                
                  "qblock_name": "SEL$1"
                ,
                
                  "other_xml": "\u003cother_xml\u003e\u003cinfo type\u003d\"db_version\"\u003e12.1.0.2\u003c/info\u003e\u003cinfo type\u003d\"parse_schema\"\u003e\u003c![CDATA[\"ORA12_01\"]]\u003e\u003c/info\u003e\u003cinfo type\u003d\"plan_hash_full\"\u003e187850946\u003c/info\u003e\u003cinfo type\u003d\"plan_hash\"\u003e2346669158\u003c/info\u003e\u003cinfo type\u003d\"plan_hash_2\"\u003e187850946\u003c/info\u003e\u003coutline_data\u003e\u003chint\u003e\u003c![CDATA[FULL(@\"SEL$1\" \"THIS_\"@\"SEL$1\")]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[OUTLINE_LEAF(@\"SEL$1\")]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[ALL_ROWS]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[DB_VERSION(\u002712.1.0.2\u0027)]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[OPTIMIZER_FEATURES_ENABLE(\u002712.1.0.2\u0027)]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]\u003e\u003c/hint\u003e\u003c/outline_data\u003e\u003c/other_xml\u003e"
                ,
                
                  "parent_id": 0
                
              ]
            
          ]
        
      ]
    

Oracle 19c 的查询执行计划如下:


  "version": "1",
  "signature": "Oracle JDBC driver",
  "date": "2021-08-02T18:58:29.743771600",
  "sql": "select\r\n        this_.col1 as col10_0_, \r\n        this_.RDPD as RDPD0_0_,\r\n        this_.col2 as col2_, \r\n        this_.col6 as ProductN6_0_0_,\r\n        this_.RDPD1 as RDPD7_0_0_,\r\n        this_.RMPD as RMPD0_0_,\r\n        this_.RDPD3 as RDPD10_0_0_,\r\n        this_.RDPD2 as RDPD11_0_0_,\r\n       ,\r\n        this_.RMP as RMP0_0_,\r\n        \r\n        ,\r\n        ,\r\n       ,\r\n        ,\r\n        this_.RDP as RDP0_0_,\r\n        this_.col1 as Product20_0_0_,\r\n        this_.col3 as RetailZ21_0_0_,\r\n        this_.col4 as Effecti22_0_0_,\r\n        this_.col2 as col20_0_ \r\n    from\r\n        TABLE_NAME this_ \r\n    where\r\n        (\r\n            (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n\t\t\t\t) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n            or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n\t\t\t or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ) \r\n\t\t\t or (\r\n                this_.col1\u003d1 \r\n                and this_.col3\u003d1 \r\n                and this_.col2\u003d1 \r\n                and this_.col4\u003d\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027\r\n            ))",
  "root": [
    
      "name": "",
      "kind": "Node",
      "type": "SELECT STATEMENT",
      "cond": "",
      "desc": "",
      "attributes": [
        
          "statement_id": "kruo2pkopof6w4"
        ,
        
          "plan_id": 15
        ,
        
          "timestamp": 1627910826000
        ,
        
          "remarks": ""
        ,
        
          "operation": "SELECT STATEMENT"
        ,
        
          "options": ""
        ,
        
          "object_node": ""
        ,
        
          "object_owner": ""
        ,
        
          "object_name": ""
        ,
        
          "object_alias": ""
        ,
        
          "object_instance": 0
        ,
        
          "object_type": ""
        ,
        
          "optimizer": "ALL_ROWS"
        ,
        
          "search_columns": 0
        ,
        
          "id": 0
        ,
        
          "depth": 0
        ,
        
          "position": 546
        ,
        
          "cost": 546
        ,
        
          "cardinality": 1
        ,
        
          "bytes": 5411
        ,
        
          "other_tag": ""
        ,
        
          "partition_start": ""
        ,
        
          "partition_stop": ""
        ,
        
          "partition_id": 0
        ,
        
          "other": ""
        ,
        
          "distribution": ""
        ,
        
          "cpu_cost": 14359036
        ,
        
          "io_cost": 545
        ,
        
          "temp_space": 0
        ,
        
          "access_predicates": ""
        ,
        
          "filter_predicates": ""
        ,
        
          "projection": ""
        ,
        
          "time": 1
        ,
        
          "qblock_name": ""
        ,
        
          "other_xml": ""
        ,
        
          "parent_id": 0
        
      ],
      "child": [
        
          "name": "TABLE_NAME",
          "kind": "Node",
          "type": "TABLE ACCESS",
          "cond": "",
          "desc": "",
          "attributes": [
            
              "statement_id": "kruo2pkopof6w4"
            ,
            
              "plan_id": 15
            ,
            
              "timestamp": 1627910826000
            ,
            
              "remarks": ""
            ,
            
              "operation": "TABLE ACCESS (FULL)"
            ,
            
              "options": "FULL"
            ,
            
              "object_node": ""
            ,
            
              "object_owner": "ORA19C_1"
            ,
            
              "object_name": "TABLE_NAME"
            ,
            
              "object_alias": "\"THIS_\"@\"SEL$1\""
            ,
            
              "object_instance": 1
            ,
            
              "object_type": "TABLE"
            ,
            
              "optimizer": ""
            ,
            
              "search_columns": 0
            ,
            
              "id": 1
            ,
            
              "depth": 1
            ,
            
              "position": 1
            ,
            
              "cost": 546
            ,
            
              "cardinality": 1
            ,
            
              "bytes": 5411
            ,
            
              "other_tag": ""
            ,
            
              "partition_start": ""
            ,
            
              "partition_stop": ""
            ,
            
              "partition_id": 0
            ,
            
              "other": ""
            ,
            
              "distribution": ""
            ,
            
              "cpu_cost": 14359036
            ,
            
              "io_cost": 545
            ,
            
              "temp_space": 0
            ,
            
              "access_predicates": ""
            ,
            
              "filter_predicates": "TO_NUMBER(\"THIS_\".\"col3\")\u003d1 AND TO_NUMBER(\"THIS_\".\"col2\")\u003d1 AND TO_NUMBER(\"THIS_\".\"col1\")\u003d1 AND \"THIS_\".\"col4\"\u003dTO_TIMESTAMP(\u0027THU, 29 JUL 2021 00:00:00 GMT\u0027)"
            ,
            
              "projection": "\"THIS_\".\"col1\"[NUMBER,22], \"THIS_\".\"col6\"[VARCHAR2,400], \"THIS_\".\"col1\"[VARCHAR2,400], \"THIS_\".\"col2\"[VARCHAR2,4]\"THIS_\".\"RDPD3\"[NUMBER,22], \"THIS_\".\"RDPD2\"[NUMBER,22], \"THIS_\".\"RMP\"[NUMBER,22],\"THIS_\".\"col3\"[VARCHAR2,400], \"THIS_\".\"col4\"[TIMESTAMP,11], \"THIS_\".\"RDP\"[NUMBER,22]"
            ,
            
              "time": 1
            ,
            
              "qblock_name": "SEL$1"
            ,
            
              "other_xml": "\u003cother_xml\u003e\u003cinfo type\u003d\"has_user_tab\"\u003eyes\u003c/info\u003e\u003cinfo type\u003d\"db_version\"\u003e19.0.0.0\u003c/info\u003e\u003cinfo type\u003d\"parse_schema\"\u003e\u003c![CDATA[\"ORA19C_1\"]]\u003e\u003c/info\u003e\u003cinfo type\u003d\"plan_hash_full\"\u003e187850946\u003c/info\u003e\u003cinfo type\u003d\"plan_hash\"\u003e2346669158\u003c/info\u003e\u003cinfo type\u003d\"plan_hash_2\"\u003e187850946\u003c/info\u003e\u003cstats type\u003d\"compilation\"\u003e\u003cstat name\u003d\"bg\"\u003e10\u003c/stat\u003e\u003c/stats\u003e\u003cqb_registry\u003e\u003cq o\u003d\"2\" f\u003d\"y\"\u003e\u003cn\u003e\u003c![CDATA[SEL$1]]\u003e\u003c/n\u003e\u003cf\u003e\u003ch\u003e\u003ct\u003e\u003c![CDATA[THIS_]]\u003e\u003c/t\u003e\u003cs\u003e\u003c![CDATA[SEL$1]]\u003e\u003c/s\u003e\u003c/h\u003e\u003c/f\u003e\u003c/q\u003e\u003c/qb_registry\u003e\u003coutline_data\u003e\u003chint\u003e\u003c![CDATA[FULL(@\"SEL$1\" \"THIS_\"@\"SEL$1\")]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[OUTLINE_LEAF(@\"SEL$1\")]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[ALL_ROWS]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[DB_VERSION(\u002719.1.0\u0027)]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[OPTIMIZER_FEATURES_ENABLE(\u002719.1.0\u0027)]]\u003e\u003c/hint\u003e\u003chint\u003e\u003c![CDATA[IGNORE_OPTIM_EMBEDDED_HINTS]]\u003e\u003c/hint\u003e\u003c/outline_data\u003e\u003c/other_xml\u003e"
            ,
            
              "parent_id": 0
            
          ]
        
      ]
    
  ]

【问题讨论】:

你是如何升级的? 解释计划曾经是清晰的东西。 col4 是约会吗?如果是这样,请将其与日期进行比较,而不是字符串。 【参考方案1】:

如果你没有最初的执行计划,可能很难处理已经改变的事情,但也许至少可以通过做来看看时间都花在了哪里

exec dbms_monitor.session_trace_enable(waits=>true) 运行查询 执行 dbms_monitor.session_trace_disable

或者只是通过运行查询来查看优化器是否正在处理糟糕的统计数据(或做出糟糕的决定)

select /*+ gather_plan_statistics */ ...
from ...

(即查询的其余部分),然后检查实际值与估计值

select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'))

或者(如果您获得许可)您可以通过 SQL Monitor 执行此操作。可以在here找到一个视频演示@

【讨论】:

以上是关于从 oracle 12c 升级到 oracle 19c 导致查询性能下降的主要内容,如果未能解决你的问题,请参考以下文章

由于 Oracle DB 从 12c 升级到 19c,对现有 SQL、PL/SQL 脚本和数据模型有何影响?

Upgrade Oracle GI from 11.2 to 12C

Oracle Database 12c 第 1 版 与Oracle Database 11g 第 2 版 有啥区别 啊!???

12c19c 从 no-cdb升级到pdb步骤

oracle11g和12c有啥区别?

如何把Oracle 11g导出的数据库导入到12c里面