Last week I’ve mentioned on Twitter that we ran into wrong result bug. We found workaround quickly but I’ve decided to spend some time to reproduce error and write blog post to warn you about this optimizer behavior. 
Special thanks to my colleague who spotted odd results which led us to this finding.

My test (virtual) environment is:
OS: Oracle Enterprise Linux 5.8
DB: Oracle EE 11.1.0.7.12

In test I will use three tables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CONT
Name    Null Type         
------- ---- -------------
CUST_ID      NUMBER(38)   
CODE         VARCHAR2(100)
 
CUST
Name    Null     Type      
------- -------- ----------
CUST_ID NOT NULL NUMBER(38)
 
DRAG
Name    Null Type     
------- ---- ---------
DRAG_ID      NUMBER(6)

To gather fresh statistics for the tables:

1
2
3
4
5
6
begin
  dbms_stats.gather_table_stats(ownname=>user,tabname=>'CONT',estimate_percent=>100, cascade=>TRUE);
  dbms_stats.gather_table_stats(ownname=>user,tabname=>'CUST',estimate_percent=>100, cascade=>TRUE);
  dbms_stats.gather_table_stats(ownname=>user,tabname=>'DRAG',estimate_percent=>100, cascade=>TRUE);
end;
/

More details about tables:

1
2
3
4
5
6
7
8
9
10
select table_name, num_rows, blocks, partitioned, last_analyzed
from dba_tables
where table_name in ('CONT','CUST','DRAG');
 
 
TABLE_NAME     NUM_ROWS     BLOCKS PARTITIONED LAST_ANALYZED    
------------ ---------- ---------- ----------- -------------------
CONT            1181949       2892 NO          04.02.2014 14:49:24
DRAG                314          5 NO          04.02.2014 14:49:25
CUST             576233        902 NO          04.02.2014 14:49:25

Information about indexes:

1
2
3
4
5
6
7
8
9
select index_name, table_name, uniqueness, distinct_keys, clustering_factor
from dba_indexes
where table_name in ('CONT','CUST','DRAG');
 
 
INDEX_NAME     TABLE_NAME   UNIQUENESS DISTINCT_KEYS CLUSTERING_FACTOR
-------------- ------------ ---------- ------------- -----------------
I_CUST_ID      CONT         NONUNIQUE         468738            753983
PK_CUST_ID     CUST         UNIQUE            576233               878

We have three small and simple tables with just two indexes. CUST table has primary key on “cust_id” column.

After this little introduction it is time for some tests.

I will flush buffer cache and shared pool before every query execution.

1
2
3
4
5
SQL> alter system flush shared_pool;
System altered.
 
SQL> alter system flush buffer_cache;
System altered.

First query execution and execution plan:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
select /*+ gather_plan_statistics */
       count(co.code) as cnt
from drag t,
     cust cus,
     cont co
where 1=1
and t.drag_id = cus.cust_id
and cus.cust_id = co.cust_id(+)
group by t.drag_id;
 
            CNT
---------------
              2
              2
              2
              2
              1
              2
              2
              2
              2
...
303 rows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
 
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------
SQL_ID  gpnrgy2vawafq, child number 0
-------------------------------------
select /*+ gather_plan_statistics */        count(co.code) as cnt from
drag t,      cust cus,      cont co where 1=1 and t.drag_id =
cus.cust_id and cus.cust_id = co.cust_id(+) group by t.drag_id
 
Plan hash value: 3989628059
 
---------------------------------------------------------------------------------------------------------------------------
| Id  | Operation            | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
---------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |      |      1 |        |    303 |00:00:00.62 |    3734 |   3724 |       |       |          |
|   1 |  HASH GROUP BY       |      |      1 |    303 |    303 |00:00:00.62 |    3734 |   3724 |  1096K|  1096K| 1264K (0)|
|*  2 |   HASH JOIN OUTER    |      |      1 |    792 |   1084 |00:00:00.16 |    3734 |   3724 |  1206K|  1206K| 1244K (0)|
|*  3 |    HASH JOIN         |      |      1 |    314 |    314 |00:00:00.04 |     890 |    885 |  1452K|  1452K| 1470K (0)|
|   4 |     TABLE ACCESS FULL| DRAG |      1 |    314 |    314 |00:00:00.01 |       7 |      6 |       |       |          |
|   5 |     TABLE ACCESS FULL| CUST |      1 |    576K|    576K|00:00:00.02 |     883 |    879 |       |       |          |
|   6 |    TABLE ACCESS FULL | CONT |      1 |   1181K|   1181K|00:00:00.01 |    2844 |   2839 |       |       |          |
---------------------------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   2 - access("CUS"."CUST_ID"="CO"."CUST_ID")
   3 - access("T"."DRAG_ID"="CUS"."CUST_ID")

Check result of the query - this is correct query result.

Now to simulate what we experienced in production.

1
2
3
4
SQL> alter system flush shared_pool;
System altered.
SQL> alter system flush buffer_cache;
System altered.  

With hint I want to force PK_CUST_ID index usage because this was preferred plan in production.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
select /*+ gather_plan_statistics index(cus PK_CUST_ID) */
       count(co.code) as cnt
from drag t,
     cust cus,
     cont co
where 1=1
and t.drag_id = cus.cust_id
and cus.cust_id = co.cust_id(+)
group by t.drag_id;
 
            CNT
---------------
              0
              0
              0
              0
              0
              0
              0
              0
              0
              0
... 303 rows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
 
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  9vf9uf7mhdmdz, child number 0
-------------------------------------
select /*+ gather_plan_statistics index(cus PK_CUST_ID) */
count(co.code) as cnt from drag t,      cust cus,      cont co where
1=1 and t.drag_id = cus.cust_id and cus.cust_id = co.cust_id(+) group
by t.drag_id
 
Plan hash value: 3263881209
 
-----------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation              | Name       | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |            |      1 |        |    303 |00:00:00.70 |    3459 |   3094 |       |       |          |
|   1 |  HASH GROUP BY         |            |      1 |    303 |    303 |00:00:00.70 |    3459 |   3094 |   934K|   934K| 1267K (0)|
|*  2 |   HASH JOIN OUTER      |            |      1 |    764 |   1046 |00:00:00.22 |    3459 |   3094 |  1134K|  1134K| 1198K (0)|
|   3 |    NESTED LOOPS        |            |      1 |    303 |    303 |00:00:02.02 |     615 |    255 |       |       |          |
|   4 |     VIEW               | VW_GBC_9   |      1 |    303 |    303 |00:00:00.01 |       7 |      6 |       |       |          |
|   5 |      HASH GROUP BY     |            |      1 |    303 |    303 |00:00:00.01 |       7 |      6 |  1012K|  1012K| 1249K (0)|
|   6 |       TABLE ACCESS FULL| DRAG       |      1 |    314 |    314 |00:00:00.01 |       7 |      6 |       |       |          |
|*  7 |     INDEX UNIQUE SCAN  | PK_CUST_ID |    303 |      1 |    303 |00:00:00.22 |     608 |    249 |       |       |          |
|   8 |    TABLE ACCESS FULL   | CONT       |      1 |   1181K|   1181K|00:00:00.01 |    2844 |   2839 |       |       |          |
-----------------------------------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   2 - access("CUS"."CUST_ID"="CO"."CUST_ID")
   7 - access("ITEM_1"="CUS"."CUST_ID")

Check result of the query!
Count is displaying all 0 values because it received only NULLs to count. 
Other functions like max and min are also affected by this error.

Check steps 4,5 and 6 in execution plan.

Instead of quick full scan on DRAG table Oracle transformed query and created inline view using smart group-by optimization.

In 10053 trace I could easily find what Oracle was doing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT
  /*+ INDEX ("CUS" "PK_CUST_ID") */
  SUM("VW_GBC_9"."ITEM_2") "CNT"
FROM
  (SELECT "T"."DRAG_ID" "ITEM_1",
    COUNT("CO"."CODE") "ITEM_2",
    "T"."DRAG_ID" "ITEM_3"
  FROM "ADMIN"."DRAG" "T"
  WHERE 1=1
  GROUP BY "T"."DRAG_ID",
    "T"."DRAG_ID"
  ) "VW_GBC_9",
  "ADMIN"."CUST" "CUS",
  "ADMIN"."CONT" "CO"
WHERE "VW_GBC_9"."ITEM_1"="CUS"."CUST_ID"
AND "CUS"."CUST_ID"      ="CO"."CUST_ID"(+)
GROUP BY "VW_GBC_9"."ITEM_3";

Quick workaround to fix this bug:
- Set "_optimizer_group_by_placement"=FALSE.

You could check in 10053 trace value of this parameter.
In my case: _optimizer_group_by_placement = true

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
SQL> alter session set "_optimizer_group_by_placement"=FALSE;
 
Session altered.
 
select /*+ gather_plan_statistics index(cus PK_CUST_ID) */
       count(co.code) as cnt
from drag t,
     cust cus,
     cont co
where 1=1
and t.drag_id = cus.cust_id
and cus.cust_id = co.cust_id(+)
group by t.drag_id;
 
       CNT
----------
         2
         2
         2
         2
         1
         2
         2
         2
         2
...
303 rows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
SQL> select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
 
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------------------------------
SQL_ID  a91bzhvupzquh, child number 0
-------------------------------------
select /*+ gather_plan_statistics index(cus PK_CUST_ID)*/
count(co.code) as cnt from drag t,      cust cus,      cont co where
1=1 and t.drag_id = cus.cust_id and cus.cust_id = co.cust_id(+) group
by t.drag_id
 
Plan hash value: 2460166079
 
------------------------------------------------------------------------------------------------------------------------
| Id  | Operation            | Name       | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |            |      1 |        |    303 |00:00:00.16 |    3481 |       |       |          |
|   1 |  HASH GROUP BY       |            |      1 |    303 |    303 |00:00:00.16 |    3481 |  1096K|  1096K| 1232K (0)|
|*  2 |   HASH JOIN OUTER    |            |      1 |    792 |   1084 |00:00:00.01 |    3481 |  1206K|  1206K| 1529K (0)|
|   3 |    NESTED LOOPS      |            |      1 |    314 |    314 |00:00:00.01 |     637 |       |       |          |
|   4 |     TABLE ACCESS FULL| DRAG       |      1 |    314 |    314 |00:00:00.01 |       7 |       |       |          |
|*  5 |     INDEX UNIQUE SCAN| PK_CUST_ID |    314 |      1 |    314 |00:00:00.01 |     630 |       |       |          |
|   6 |    TABLE ACCESS FULL | CONT       |      1 |   1181K|   1181K|00:00:00.01 |    2844 |       |       |          |
------------------------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   2 - access("CUS"."CUST_ID"="CO"."CUST_ID")
   5 - access("T"."DRAG_ID"="CUS"."CUST_ID")
 
 
27 rows selected. 

Oracle Support note associated with "_optimizer_group_by_placement" parameter.

Note.8945586.8 Ext/Pub Bug 8945586 - Wrong results using GROUP BY placement:
Description
Wrong results can occur when using GROUP BY placement where the aggregate column gets pruned from select list.

I’ve even found that “_optimizer_group_by_placement” parameter was mentioned in "Oracle® Fusion Middleware Oracle WebCenter Analytics Installation and Upgrade Guide".

Oracle 11g (11.1.0.6 and above) in default or Oracle Real Application Clusters (RAC) configuration

When running Oracle 11g versions prior to 11.1.0.7.0 the Oracle system parameter _optimizer_group_by_placement must be set to false. This can either be set in the init.ora file of the respective database instances or by by issuing an ALTER SYSTEM command as follows:

SQLPLUS /nolog
CONNECT / AS SYSDBA
ALTER SYSTEM SET "_optimizer_group_by_placement"=false

group by的优化bug还是挺多的,还有比如_optimizer_aggr_groupby_elim,但是不出bug的情况下,性能提升还是非常明显的,大家一定要仔细检查结果,不要只看性能。

oracle group by placement可能导致错误结果的bug的更多相关文章

  1. oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)

    LAST UPDATE:     1 Dec 15, 2016 APPLIES TO:     1 2 3 4 Oracle Database - Enterprise Edition - Versi ...

  2. django继承修改 User表导致的问题 fields.E304(permissions/group都会有这样的错误)

    问题: django继承修改 User表时,进行migrations操作时会导致的问题 fields.E304(permissions/group都会有这样的错误)如图: 根源: django文档中有 ...

  3. 登陆Oracle,报oracle initializationg or shutdown in progress 错误提示

    前两天,登陆Oracle,发现登陆不上去了,报”oracle initializationg or shutdown in progress 错误提示” 错误. 然后就想着怎么去解决,首先自己到win ...

  4. oracle:数据库版本问题导致的bug

    公司开发出来的系统,由于各现场oracle数据库版本有10.2.0.4.11.2.0.1.11.2.0.3.11.2.0.4: 进而会导致版本不一导致错误问题.下面列举2个: 1.wm_concat ...

  5. Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法

    Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法 登陆数据库时提示 “ORA-01033”错误在命令窗口以s ...

  6. oracle group by中cube和rollup字句的使用方法及区别

    oracle group by中rollup和cube的区别:  Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句. 如果是ROLLUP(A, B, C)的话,先 ...

  7. oracle所在磁盘空间不足导致了数据库异常

    oracle所在磁盘空间不足导致了数据库异常.需要减小数据文件的大小来解决. 1.检查数据文件的名称和编号 select file#,name from v$datafile; 2.看哪个数据文件所占 ...

  8. MVC4 路由参数带点 文件名后缀导致错误

    错误描述 最近在研究office在线预览,用到mvc4  apicontroller 需要传参是文件名,如test.docx导致错误"指定的目录或文件在 Web 服务器上不存在", ...

  9. Oracle问题之ORA-12560TNS:协议适配器错误

    Oracle问题之ORA-12560TNS:协议适配器错误 一.造成ORA-12560: TNS: 协议适配器错误的问题的原因有三个: 1.监听服务没有起起来.windows平台个一如下操作:开始-- ...

随机推荐

  1. [原创]WB Android客户端架构总结:发WB工作队列设计

    先简单说下需求,发一条WB包含多种类型,例如图片.视频.文字等,发送工作不能阻塞UI,工作队列易于扩展,方便优化. 几个重要的类: JobManager:统一管理Job列表,包括job的添加.启动.终 ...

  2. web.py框架之高级应用

    二.高级应用 2.1 web.ctx 获取客户端信息,比如:来源页面.客户端浏览器类型等. web.ctx基于 threadeddict类,又被叫做 ThreadDict.这个类创建了一个类似字典(d ...

  3. 创建多进程Process

    注册一个进程: from multiprocessing import Process import os def func(args): # 在子进程里面.args接收一个参数,如果要接受多个参数使 ...

  4. namecheap 添加二级域名

    namecheap Docs Nginx 添加一个server server { listen 80; server_name video.ajanuw.fun; location / { root ...

  5. oracle忘记密码用户名被锁定_解决方案

    本方案参考http://www.cnblogs.com/iosundersunshine/p/5313174.html 解决方案(window): 进入cmd命令 按照图上五步,即可 1,输入 ech ...

  6. http proxy模块参数

    http proxy模块参数 nginx功能的代理功能是是通过http proxy模块来实现的.默认在安装Nginx是已经安装了http proxy模块,可以直接使用. http模块相关参数 说明 p ...

  7. linux考试题改错

    符号链接和硬链接有什么区别? 改:符号链接存储文件路径,可以指向不同分区文件,源文件删除后失效. 改:硬链接指向文件索引节点,仅能指向同一分区文件,源文件删除后可以访问. 请描述文件和目录9位权限位的 ...

  8. 网站ASHX不执行故障

    今天修改之前做的一个网站,添加了ashx文件,但调试时发现里面的代码不执行. 检查webconfig文件发现其中有一项配置了ashx的处理方式: <system.web> <http ...

  9. HR在ERP实施过程中的作用

    ERP实施涉及到部门职责.个人职责的改变,在实施过程中HR有着不可估量的作用: 实施制度设计 包括如何对实施人员的激励,对实施人员进行合理的岗位职责调整: 某些企业在实施ERP时自项目经理到关键用户都 ...

  10. 最全的MonkeyRunner自动化测试从入门到精通(3)

    一.eclipse的下载安装与配置成安卓开发环境 步骤一:在官网上面进行下载eclipse,官网的网址:https://www.eclipse.org/downloads/ 步骤二:下载完成后可以在你 ...