统计信息不准导致执行计划出错跑不出结果,优化后只要1分钟

一天查看数据库长会话,发现1个sql跑得很慢,1个多小时不出结果,花了点时间把它给优化了。

优化前:

SELECT 20131023,
       "A2"."ORG_ID",
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'DP' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'BOX' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'ONU' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'OBD' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '001' AND "A2"."RES_TYPE" = 'DP') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '002' AND "A2"."RES_TYPE" = 'BOX') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '0011' AND "A2"."RES_TYPE" = 'ONU') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '0022' AND "A2"."RES_TYPE" = 'OBD') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY'''))
  FROM "CRM_SZ"."AAA" "A2",
       "CRM_SZ"."BBB" "A1"
 WHERE "A1"."RES_ID"(+) = "A2"."RES_CODE"
 GROUP BY "A2"."ORG_ID"

执行计划:
Plan hash value: 2627707252

---------------------------------------------------------------------------------------
| Id  | Operation           | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |                 |     1 |  1065 |     3  (34)| 00:00:01 |
|   1 |  SORT GROUP BY      |                 |     1 |  1065 |     3  (34)| 00:00:01 |
|   2 |   NESTED LOOPS OUTER|                 |     1 |  1065 |     2   (0)| 00:00:01 |
|   3 |    TABLE ACCESS FULL| AAA             |     1 |   539 |     2   (0)| 00:00:01 |
|*  4 |    INDEX FULL SCAN  | IX_MO_CON_VALUE |     1 |   526 |     0   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("A1"."RES_ID"(+)="A2"."RES_CODE")
       filter("A1"."RES_ID"(+)="A2"."RES_CODE")

cbo估算错了,rows全是1,导致走nl

手工count了一把:

select count(*) from  "CRM_SZ"."AAA"   ;--1365564

select count(*) from  "CRM_SZ"."BBB";--119949

走nl那岂不是sb啦。

第一次优化后:

SELECT/*+use_hash(A1,A2) swap_join_inputs(A1)*/20131023,
       "A2"."ORG_ID",
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'DP' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'BOX' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'ONU' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'OBD' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '001' AND "A2"."RES_TYPE" = 'DP') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '002' AND "A2"."RES_TYPE" = 'BOX') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '0011' AND "A2"."RES_TYPE" = 'ONU') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '0022' AND "A2"."RES_TYPE" = 'OBD') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY'''))
  FROM "CRM_SZ"."AAA"        "A2",
       "CRM_SZ"."BBB" "A1"
 WHERE "A1"."RES_ID"(+) = "A2"."RES_CODE"
 GROUP BY "A2"."ORG_ID"

63 rows selected.

Elapsed: 00:00:47.64

Execution Plan
----------------------------------------------------------
Plan hash value: 3074972763

------------------------------------------------------------------------------------------
| Id  | Operation              | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |                 |     1 |  1065 |     4  (50)| 00:00:01 |
|   1 |  SORT GROUP BY         |                 |     1 |  1065 |     4  (50)| 00:00:01 |
|*  2 |   HASH JOIN RIGHT OUTER|                 |     1 |  1065 |     3  (34)| 00:00:01 |
|   3 |    INDEX FULL SCAN     | IX_MO_CON_VALUE |     1 |   526 |     0   (0)| 00:00:01 |
|   4 |    TABLE ACCESS FULL   | AAA             |     1 |   539 |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("A1"."RES_ID"(+)="A2"."RES_CODE")

Statistics
----------------------------------------------------------
       1065  recursive calls
          3  db block gets
      13375  consistent gets
      16369  physical reads
          0  redo size
       4862  bytes sent via SQL*Net to client
        791  bytes received via SQL*Net from client
          6  SQL*Net roundtrips to/from client
         12  sorts (memory)
          1  sorts (disk)
         63  rows processed

第二次优化后:

SELECT/*+use_hash(A1,A2) full(A1) full(A2) parallel(A1,5) parallel(A2,5) swap_join_inputs(A1)*/20131023,
       "A2"."ORG_ID",
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'DP' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'BOX' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'ONU' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE "A2"."RES_TYPE"
                       WHEN 'OBD' THEN
                        "A2"."RES_CODE"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '001' AND "A2"."RES_TYPE" = 'DP') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '002' AND "A2"."RES_TYPE" = 'BOX') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '0011' AND "A2"."RES_TYPE" = 'ONU') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY''')),
       COUNT(DISTINCT NLSSORT(CASE
                       WHEN ("A1"."CON_TYPE" = '0022' AND "A2"."RES_TYPE" = 'OBD') THEN
                        "A1"."RES_ID"
                     END,
                     'nls_sort=''BINARY'''))
  FROM "CRM_SZ"."AAA"        "A2",
       "CRM_SZ"."BBB" "A1"
 WHERE "A1"."RES_ID"(+) = "A2"."RES_CODE"
 GROUP BY "A2"."ORG_ID";

63 rows selected.

Elapsed: 00:00:10.21

Execution Plan
----------------------------------------------------------
Plan hash value: 4044842257

-------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                      | Name                  | Rows  | Bytes | Cost (%CPU)| Time     |    TQ  |IN-OUT| PQ Distrib |
-------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT               |                       |     1 |  1065 |     6  (34)| 00:00:01 |        |      |            |
|   1 |  PX COORDINATOR                |                       |       |       |            |          |        |      |            |
|   2 |   PX SEND QC (RANDOM)          | :TQ10004              |     1 |  1065 |     6  (34)| 00:00:01 |  Q1,04 | P->S | QC (RAND)  |
|   3 |    SORT GROUP BY               |                       |     1 |  1065 |     6  (34)| 00:00:01 |  Q1,04 | PCWP |            |
|   4 |     PX RECEIVE                 |                       |     1 |  1065 |     6  (34)| 00:00:01 |  Q1,04 | PCWP |            |
|   5 |      PX SEND HASH              | :TQ10003              |     1 |  1065 |     6  (34)| 00:00:01 |  Q1,03 | P->P | HASH       |
|   6 |       SORT GROUP BY            |                       |     1 |  1065 |     6  (34)| 00:00:01 |  Q1,03 | PCWP |            |
|   7 |        PX RECEIVE              |                       |     1 |  1065 |     6  (34)| 00:00:01 |  Q1,03 | PCWP |            |
|   8 |         PX SEND HASH           | :TQ10002              |     1 |  1065 |     6  (34)| 00:00:01 |  Q1,02 | P->P | HASH       |
|   9 |          SORT GROUP BY         |                       |     1 |  1065 |     6  (34)| 00:00:01 |  Q1,02 | PCWP |            |
|* 10 |           HASH JOIN RIGHT OUTER|                       |     1 |  1065 |     5  (20)| 00:00:01 |  Q1,02 | PCWP |            |
|  11 |            PX RECEIVE          |                       |     1 |   526 |     2   (0)| 00:00:01 |  Q1,02 | PCWP |            |
|  12 |             PX SEND HASH       | :TQ10000              |     1 |   526 |     2   (0)| 00:00:01 |  Q1,00 | P->P | HASH       |
|  13 |              PX BLOCK ITERATOR |                       |     1 |   526 |     2   (0)| 00:00:01 |  Q1,00 | PCWC |            |
|  14 |               TABLE ACCESS FULL| BBB                   |     1 |   526 |     2   (0)| 00:00:01 |  Q1,00 | PCWP |            |
|  15 |            PX RECEIVE          |                       |     1 |   539 |     2   (0)| 00:00:01 |  Q1,02 | PCWP |            |
|  16 |             PX SEND HASH       | :TQ10001              |     1 |   539 |     2   (0)| 00:00:01 |  Q1,01 | P->P | HASH       |
|  17 |              PX BLOCK ITERATOR |                       |     1 |   539 |     2   (0)| 00:00:01 |  Q1,01 | PCWC |            |
|  18 |               TABLE ACCESS FULL| AAA                   |     1 |   539 |     2   (0)| 00:00:01 |  Q1,01 | PCWP |            |
-------------------------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

  10 - access("A1"."RES_ID"(+)="A2"."RES_CODE")

Statistics
----------------------------------------------------------
        585  recursive calls
          4  db block gets
      14267  consistent gets
      13126  physical reads
        808  redo size
       4888  bytes sent via SQL*Net to client
        840  bytes received via SQL*Net from client
          6  SQL*Net roundtrips to/from client
         23  sorts (memory)
          0  sorts (disk)
         63  rows processed

优化前,执行计划走了nl,1个多小时查不出结果

第一次优化后,首次00:00:47.64可以出结果

第二次优化后,首次00:00:10.21出结果,重复执行(有缓存)的情况下3s出结果。

统计信息不准导致执行计划出错跑不出结果,优化后只要1分钟

时间: 2024-11-08 19:04:53

统计信息不准导致执行计划出错跑不出结果,优化后只要1分钟的相关文章

dblink导致执行计划出错,hint也无效

开发的同事发来一条语句,让我帮忙查看下ods和源端的结果是否一致.因为一下执行没出来,问开发人员,这个语句要跑2-3分钟. 因为他们是从本地用dblink连到ods的,我这里把dblink去掉直接从ods查看执行计划. SELECT XSY_CODE,--发展销售员编码 SLY_CODE,--受理销售员编码 XSD_CODE,--销售点编码 DZS_CODE,--店中商编码 JYZT_CODE--销售员所属经营主体编码 FROM (select /*+parallel(a,10) paralle

什么情况下会导致执行计划不走索引?

不走索引的情况还是蛮多的1.条件字段选择性弱,查出的结果集较大,不走索引:2.where条件等号两边字段类型不同,不走索引:3.优化器分析的统计信息陈旧也可能导致不走索引:4.索引字段 is null 不走索引:5.对于count(*)当索引字段有not null约束时走索引,否则不走索引:6.like 后面的字符当首位为通配符时不走索引:7.使用不等于操作符如:<>.!= 等不走索引:8.索引字段前加了函数或参加了运算不走索引:什么情况下会导致执行计划不走索引?

Oracle统计信息不准(谓词越界)造成的性能问题

什么是谓词越界?谓词越界其实就是SQL语句的查询条件超出了数据库统计信息所记录的范围.谓词越界会导致Oracle优化器错误的选择SQL语句的执行计划,导致性能问题. 这里举一个简单的例子说明谓词越界导致优化器选择了错误的执行计划. create table t1 (col1 number); create index idx_t1 on t1(col1); begin for i in 1..10000 loop insert into t1 values (i); end loop; comm

表收集错误导致执行计划错误

有时候,表收集信息错误,导致执行计划错误问题. SQL> insert into test values(1,'user1'); 已创建 1 行. SQL> commit; 提交完成. SQL> select * from test; ID NAME ---------- ---------------------------------------- 1 user1 SQL> insert into test values(2,'user2'); 已创建 1 行. SQL>

类型转换导致执行计划不走索引测试案例

测试环境模拟: SQL> drop table t_col_type purge; create table t_col_type(id varchar2(20),col2 varchar2(20),col3 varchar2(20)); insert into t_col_type select rownum,'abc','efg' from dual connect by level<=10000; commit; create index idx_id on t_col_type(id)

SQL alwayson 辅助接点查询统计信息“丢失”导致查询失败

ALWAYSON 出现以下情况已经2次了,记录下: DBCC 执行完毕.如果 DBCC 输出了错误信息,请与系统管理员联系. 消息 2767,级别 16,状态 1,过程 sp_table_statistics2_rowset,第 105 行无法在系统目录中找到统计信息 '_WA_Sys_0000001C_090A5324'.DBCC 执行完毕.如果 DBCC 输出了错误信息,请与系统管理员联系. 查询方式如下图: 临时解决办法: 主库上执行: drop statistics table_name

统计当前JOB的执行计划

目前我的情况是job分散在多个服务器,近400个JOB都是单独执行的,为了统计出所有JOB的执行计划,我分了四种情况,分别如下:每天执行一次的,每天循环执行的,按星期和按月份执行的四类.SQL 语句如下: SELECT JOB.name,SysSche.name, substring(right('00'+cast(active_start_time as varchar(6)),6),1,2)+':' +substring(right('00'+cast(active_start_time a

当node升级后导致webpack打包出错,node-saas出问题的解决办法

报错信息如下: ERROR in ./node_modules/[email protected]@extract-text-webpack-plugin/dist/loader.js?{"omit":1,"remove":true}!D:/work/nl_web/node_modules/[email protected]@vue-style-loader!D:/work/nl_web/node_modules/[email protected]@css-load

SQL Server 执行计划利用统计信息对数据行的预估原理以及SQL Server 2014中预估策略的改变

前提  本文仅讨论SQL Server查询时, 对于非复合统计信息,也即每个字段的统计信息只包含当前列的数据分布的情况下, 在用多个字段进行组合查询的时候,如何根据统计信息去预估行数的. 利用不同字段的统计信息做数据行数预估的算法原理,以及SQL Server 2012和SQL Server 2014该算法的差异情况, 这里暂时不涉及复合统计信息,暂不涉及统计信息的更新策略及优化相关话题,以及其他SQL Server版本计算方式. 统计信息是什么 简单说就是对某些字段的数据分布的一种描述,让SQ