【Oracle】Oracle wrong result一则(优化器问题)
现象如下:
SYS@proc> select * from v$version where rownum=1;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
SYS@proc> drop table test purge;
Table dropped.
SYS@proc> with t as (select a.*,rownum rn from dba_objects a) select count(*) from t where t.rn=(select max(rn) from t);
COUNT(*)
----------
1
SYS@proc> create table test as select * from dba_objects;
Table created.
SYS@proc> with t as (select a.*,rownum rn from test a) select * from t where t.rn=(select max(rn) from t);
no rows selected
先看下两者的执行计划:
SYS@proc> set long 9999 pagesize 9999 lines 500
SYS@proc> set autotrace traceonly
SYS@proc> with t as (select a.*,rownum rn from dba_objects a) select count(*) from t where t.rn=(select max(rn) from t);
Execution Plan
----------------------------------------------------------
Plan hash value: 1413014202
--------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 1336 (1)| 00:00:17 |
| 1 | TEMP TABLE TRANSFORMATION | | | | | |
| 2 | LOAD AS SELECT | SYS_TEMP_0FD9D6606_10C429 | | | | |
| 3 | COUNT | | | | | |
| 4 | VIEW | DBA_OBJECTS | 86954 | 13M| 301 (1)| 00:00:04 |
| 5 | UNION-ALL | | | | | |
|* 6 | TABLE ACCESS BY INDEX ROWID| SUM$ | 1 | 9 | 1 (0)| 00:00:01 |
|* 7 | INDEX UNIQUE SCAN | I_SUM$_1 | 1 | | 0 (0)| 00:00:01 |
| 8 | TABLE ACCESS BY INDEX ROWID| OBJ$ | 1 | 30 | 3 (0)| 00:00:01 |
|* 9 | INDEX RANGE SCAN | I_OBJ1 | 1 | | 2 (0)| 00:00:01 |
|* 10 | FILTER | | | | | |
|* 11 | HASH JOIN | | 86953 | 10M| 299 (2)| 00:00:04 |
| 12 | TABLE ACCESS FULL | USER$ | 92 | 1564 | 3 (0)| 00:00:01 |
|* 13 | HASH JOIN | | 86953 | 9000K| 296 (2)| 00:00:04 |
| 14 | INDEX FULL SCAN | I_USER2 | 92 | 2024 | 1 (0)| 00:00:01 |
|* 15 | TABLE ACCESS FULL | OBJ$ | 86953 | 7132K| 294 (1)| 00:00:04 |
| 16 | NESTED LOOPS | | 1 | 29 | 2 (0)| 00:00:01 |
|* 17 | INDEX SKIP SCAN | I_USER2 | 1 | 20 | 1 (0)| 00:00:01 |
|* 18 | INDEX RANGE SCAN | I_OBJ4 | 1 | 9 | 1 (0)| 00:00:01 |
| 19 | NESTED LOOPS | | 1 | 105 | 2 (0)| 00:00:01 |
| 20 | TABLE ACCESS FULL | LINK$ | 1 | 88 | 2 (0)| 00:00:01 |
| 21 | TABLE ACCESS CLUSTER | USER$ | 1 | 17 | 0 (0)| 00:00:01 |
|* 22 | INDEX UNIQUE SCAN | I_USER# | 1 | | 0 (0)| 00:00:01 |
| 23 | SORT AGGREGATE | | 1 | 13 | | |
|* 24 | VIEW | | 86954 | 1103K| 517 (1)| 00:00:07 |
| 25 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6606_10C429 | 86954 | 13M| 517 (1)| 00:00:07 |
| 26 | SORT AGGREGATE | | 1 | 13 | | |
| 27 | VIEW | | 86954 | 1103K| 517 (1)| 00:00:07 |
| 28 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6606_10C429 | 86954 | 13M| 517 (1)| 00:00:07 |
--------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - filter(BITAND("S"."XPFLAGS",8388608)=8388608)
7 - access("S"."OBJ#"=:B1)
9 - access("EO"."OBJ#"=:B1)
10 - filter("O"."TYPE#"<>4 AND "O"."TYPE#"<>5 AND "O"."TYPE#"<>7 AND "O"."TYPE#"<>8 AND
"O"."TYPE#"<>9 AND "O"."TYPE#"<>11 AND "O"."TYPE#"<>12 AND "O"."TYPE#"<>13 AND "O"."TYPE#"<>14 AND
"O"."TYPE#"<>22 AND "O"."TYPE#"<>87 AND "O"."TYPE#"<>88 OR BITAND("U"."SPARE1",16)=0 OR
("O"."TYPE#"=4 OR "O"."TYPE#"=5 OR "O"."TYPE#"=7 OR "O"."TYPE#"=8 OR "O"."TYPE#"=9 OR "O"."TYPE#"=10
OR "O"."TYPE#"=11 OR "O"."TYPE#"=12 OR "O"."TYPE#"=13 OR "O"."TYPE#"=14 OR "O"."TYPE#"=22 OR
"O"."TYPE#"=87) AND (SYS_CONTEXT('userenv','current_edition_name')='ORA$BASE' AND "U"."TYPE#"<>2 OR
"U"."TYPE#"=2 AND "U"."SPARE2"=TO_NUMBER(SYS_CONTEXT('userenv','current_edition_id')) OR EXISTS
(SELECT 0 FROM SYS."USER$" "U2",SYS."OBJ$" "O2" WHERE "O2"."OWNER#"="U2"."USER#" AND "O2"."TYPE#"=88
AND "O2"."DATAOBJ#"=:B1 AND "U2"."TYPE#"=2 AND "U2"."SPARE2"=TO_NUMBER(SYS_CONTEXT('userenv','current_
edition_id')))))
11 - access("O"."SPARE3"="U"."USER#")
13 - access("O"."OWNER#"="U"."USER#")
15 - filter("O"."TYPE#"<>10 AND "O"."NAME"<>'_NEXT_OBJECT' AND
"O"."NAME"<>'_default_auditing_options_' AND "O"."LINKNAME" IS NULL AND BITAND("O"."FLAGS",128)=0)
17 - access("U2"."TYPE#"=2 AND "U2"."SPARE2"=TO_NUMBER(SYS_CONTEXT('userenv','current_edition_id')))
filter("U2"."TYPE#"=2 AND "U2"."SPARE2"=TO_NUMBER(SYS_CONTEXT('userenv','current_edition_id')))
18 - access("O2"."DATAOBJ#"=:B1 AND "O2"."TYPE#"=88 AND "O2"."OWNER#"="U2"."USER#")
22 - access("L"."OWNER#"="U"."USER#")
24 - filter("T"."RN"= (SELECT MAX("RN") FROM (SELECT /*+ CACHE_TEMP_TABLE ("T1") */ "C0"
"OWNER","C1" "OBJECT_NAME","C2" "SUBOBJECT_NAME","C3" "OBJECT_ID","C4" "DATA_OBJECT_ID","C5"
"OBJECT_TYPE","C6" "CREATED","C7" "LAST_DDL_TIME","C8" "TIMESTAMP","C9" "STATUS","C10"
"TEMPORARY","C11" "GENERATED","C12" "SECONDARY","C13" "NAMESPACE","C14" "EDITION_NAME","C15" "RN"
FROM "SYS"."SYS_TEMP_0FD9D6606_10C429" "T1") "T"))
Statistics
----------------------------------------------------------
12 recursive calls
1342 db block gets
3718 consistent gets
2626 physical reads
1116 redo size
526 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SYS@proc>
SYS@proc>
SYS@proc> with t as (select a.*,rownum rn from test a) select * from t where t.rn=(select max(rn) from t);
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 1063871704
------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 94909 | 19M| 676 (1)| 00:00:09 |
|* 1 | VIEW | | 94909 | 19M| 338 (1)| 00:00:05 |
| 2 | COUNT | | | | | |
| 3 | TABLE ACCESS FULL | TEST | 94909 | 18M| 338 (1)| 00:00:05 |
| 4 | SORT AGGREGATE | | 1 | 13 | | |
| 5 | VIEW | | 94909 | 1204K| 338 (1)| 00:00:05 |
| 6 | COUNT | | | | | |
| 7 | TABLE ACCESS FULL| TEST | 94909 | | 338 (1)| 00:00:05 |
------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("T"."RN"= (SELECT MAX("RN") FROM (SELECT ROWNUM "RN"
FROM "TEST" "A") "T"))
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2486 consistent gets
2480 physical reads
0 redo size
1407 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
从第二个执行计划的Predicate Information处看出,filter里边的T表给解析成只有一个字段”RN”(也应该有结果出来?)。
尝试加hint让with as的部分放在temp表,执行结果以及执行计划如下:
SYS@proc> set autotrace off
SYS@proc> with t as (select /*+MATERIALIZE*/ a.*,rownum rn from test a) select count(*) from t where t.rn=(select max(rn) from t);
COUNT(*)
----------
1
SYS@proc> set autotrace traceonly
SYS@proc> l
1* with t as (select /*+MATERIALIZE*/ a.*,rownum rn from test a) select count(*) from t where t.rn=(select max(rn) from t)
SYS@proc> /
Execution Plan
----------------------------------------------------------
Plan hash value: 340454420
--------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 1790 (1)| 00:00:22 |
| 1 | TEMP TABLE TRANSFORMATION | | | | | |
| 2 | LOAD AS SELECT | SYS_TEMP_0FD9D6608_10C429 | | | | |
| 3 | COUNT | | | | | |
| 4 | TABLE ACCESS FULL | TEST | 94909 | 18M| 338 (1)| 00:00:05 |
| 5 | SORT AGGREGATE | | 1 | 13 | | |
|* 6 | VIEW | | 94909 | 1204K| 726 (1)| 00:00:09 |
| 7 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6608_10C429 | 94909 | 18M| 726 (1)| 00:00:09 |
| 8 | SORT AGGREGATE | | 1 | 13 | | |
| 9 | VIEW | | 94909 | 1204K| 726 (1)| 00:00:09 |
| 10 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6608_10C429 | 94909 | 18M| 726 (1)| 00:00:09 |
--------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - filter("T"."RN"= (SELECT MAX("RN") FROM (SELECT /*+ CACHE_TEMP_TABLE ("T1") */ "C0"
"OWNER","C1" "OBJECT_NAME","C2" "SUBOBJECT_NAME","C3" "OBJECT_ID","C4" "DATA_OBJECT_ID","C5"
"OBJECT_TYPE","C6" "CREATED","C7" "LAST_DDL_TIME","C8" "TIMESTAMP","C9" "STATUS","C10"
"TEMPORARY","C11" "GENERATED","C12" "SECONDARY","C13" "NAMESPACE","C14" "EDITION_NAME","C15"
"RN" FROM "SYS"."SYS_TEMP_0FD9D6608_10C429" "T1") "T"))
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
12 recursive calls
1339 db block gets
3873 consistent gets
3866 physical reads
764 redo size
526 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
结果已经执行出来了。
另外该语句在10g是可以不用加hint就能正常执行:
SYS@proc> select * from v$version where rownum=1;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
SYS@proc> with t as (select a.*,rownum rn from test a) select count(*) from t where t.rn=(select max(rn) from t);
COUNT(*)
----------
1
这里尝试将11g里边的参数optimizer_features_enable修改成10g的,在看看执行计划以及结果:
SYS@proc> show parameter optimizer_features_enable
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_features_enable string 11.2.0.4
SYS@proc> alter session set optimizer_features_enable='10.2.0.4';
Session altered.
SYS@proc> show parameter optimizer_features_enable
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
optimizer_features_enable string 10.2.0.4
SYS@proc> set pagesize 9999 long 9999 lines 500
SYS@proc> with t as (select a.*,rownum rn from test a) select count(*) from t where t.rn=(select max(rn) from t);
COUNT(*)
----------
0
SYS@proc> set autotrace traceonly
SYS@proc> l
1* with t as (select a.*,rownum rn from test a) select count(*) from t where t.rn=(select max(rn) from t)
SYS@proc> /
Execution Plan
----------------------------------------------------------
Plan hash value: 4022736097
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 677 (1)| 00:00:09 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
|* 2 | VIEW | | 94909 | 1204K| 338 (1)| 00:00:05 |
| 3 | COUNT | | | | | |
| 4 | TABLE ACCESS FULL | TEST | 94909 | | 338 (1)| 00:00:05 |
| 5 | SORT AGGREGATE | | 1 | 13 | | |
| 6 | VIEW | | 94909 | 1204K| 338 (1)| 00:00:05 |
| 7 | COUNT | | | | | |
| 8 | TABLE ACCESS FULL| TEST | 94909 | | 338 (1)| 00:00:05 |
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("T"."RN"= (SELECT MAX("RN") FROM (SELECT "A"."OWNER"
"OWNER","A"."OBJECT_NAME" "OBJECT_NAME","A"."SUBOBJECT_NAME"
"SUBOBJECT_NAME","A"."OBJECT_ID" "OBJECT_ID","A"."DATA_OBJECT_ID"
"DATA_OBJECT_ID","A"."OBJECT_TYPE" "OBJECT_TYPE","A"."CREATED"
"CREATED","A"."LAST_DDL_TIME" "LAST_DDL_TIME","A"."TIMESTAMP"
"TIMESTAMP","A"."STATUS" "STATUS","A"."TEMPORARY"
"TEMPORARY","A"."GENERATED" "GENERATED","A"."SECONDARY"
"SECONDARY","A"."NAMESPACE" "NAMESPACE","A"."EDITION_NAME"
"EDITION_NAME",ROWNUM "RN" FROM "TEST" "A") "T"))
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2486 consistent gets
2480 physical reads
0 redo size
525 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Predicate Information的filter已经没错了,但是还是出不来结果。
这里不是很懂了。
【Oracle】Oracle wrong result一则(优化器问题)的更多相关文章
- Oracle的优化器介绍
Oracle优化器介绍 本文讲述了Oracle优化器的概念.工作原理和使用方法,兼顾了Oracle8i.9i以及最新的10g三个版本.理解本文将有助于您更好的更有效的进行SQL优化工作. RBO优化器 ...
- Oracle优化器介绍
Oracle优化器介绍 本文讲述了Oracle优化器的概念.工作原理和使用方法,兼顾了Oracle8i.9i以及最新的10g三个版本.理解本文将有助于您更好的更有效的进行SQL优化工作. RBO优化器 ...
- Oracle性能优化之 Oracle里的优化器
优化器(optimizer)是oracle数据库内置的一个核心子系统.优化器的目的是按照一定的判断原则来得到它认为的目标SQL在当前的情形下的最高效的执行路径,也就是为了得到目标SQL的最佳执行计划. ...
- PLSQL_性能优化系列04_Oracle Optimizer优化器
2014-09-25 Created By BaoXinjian
- optimizer_mode优化器模式
查询优化器最主要的工作就是接受输入的SQL以及各种环境参数.配置参数,生成合适的SQL执行计划(Execution Plan). Query Optimizer一共经历了两个历史阶段: RBO: Ru ...
- ORACLE优化器RBO与CBO介绍总结
RBO和CBO的基本概念 Oracle数据库中的优化器又叫查询优化器(Query Optimizer).它是SQL分析和执行的优化工具,它负责生成.制定SQL的执行计划.Oracle的优化器有两种,基 ...
- Oracle 课程五之优化器和执行计划
课程目标 完成本课程的学习后,您应该能够: •优化器的作用 •优化器的类型 •优化器的优化步骤 •扫描的基本类型 •表连接的执行计划 •其他运算方式的执行计划 •如何看执行计划顺序 •如何获取执行计划 ...
- oracle里的优化器
1.1 oracle里的优化器 RBO(Rule-Based-Optinizer):基于规则的优化器 CBO(Cost-Based-Optinizer): 基于成本的优化器 SQL语句执行过程 待执行 ...
- Oracle优化器
本文参照:https://www.cnblogs.com/Dreamer-1/p/6076440.html 读优化器之前建议先读: https://www.cnblogs.com/zhougongji ...
- Oracle优化器基础知识之访问数据的方法
目录 一.访问数据的方法 1.直接访问数据 2.访问索引 一.访问数据的方法 Oracle访问表中数据的方法有两种,一种是直接表中访问数据,另外一种是先访问索引,如果索引数据不符合目标SQL,就回表, ...
随机推荐
- Java实现第十届蓝桥杯最大降雨量
试题 E: 最大降雨量 本题总分:15 分 [问题描述] 由于沙之国长年干旱,法师小明准备施展自己的一个神秘法术来求雨. 这个法术需要用到他手中的 49 张法术符,上面分别写着 1 至 49 这 49 ...
- java实现第七届蓝桥杯路径之谜
路径之谜 题目描述 小明冒充X星球的骑士,进入了一个奇怪的城堡. 城堡里边什么都没有,只有方形石头铺成的地面. 假设城堡地面是 n x n 个方格.[如图1.png]所示. 按习俗,骑士要从西北角走到 ...
- ClickHouse基本操作(一)
常用SQL 创建表 1 2 3 4 5 6 7 CREATE TABLE b6logs( eventDate Date, impid UInt64, uid String, idfa String, ...
- Javascript:跳转到指定页面
<div> <input type="text" id="jumpPage"/> <input type="button ...
- NetAnalyzer笔记 之 十四 NetAnalyzer 6.0 的使用方法 -- 3.协议分析与统计
数据分析 完成了数据的抓取,那么接下来就是NetAnalyzer的第二个重点部分了,协议分析作为整个软件的核心之一,在最新的NetAnalyzer中已经得到了巨大的提升.NetAnalyzer中协议分 ...
- leetcode75之颜色分类
题目描述: 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白 ...
- Photoshop 使用过程中遇到的问题
1.关于图片像素 根据不同用途设置不同的分辨率: 印洗照片300像素及以上, 高清写真海报96-200像素, 网络图片网页界面72像素, 大型喷绘25到50像素
- nodejs使用redis实现单例锁
一个while(true)下使用redis的setnx命令,创建一个唯一标识,在操作执行完后,删除这个标识. 注意resource_name用一个常量,而my_random_value使用一个随机值. ...
- 全网最完整的Redis入门指导
前言 本文提供全网最完整的Redis入门指导教程,下面我们从下载Redis安装包开始,一步一步的学习使用. 下载Redis 官网提供的Redis安装包是服务于Linux的,而我们需要在Window下使 ...
- 关于UDP的检验和计算(附代码)
关于UDP的检验和计算(附代码) 在下午的学习过程中https://www.cnblogs.com/roccoshi/p/13032356.html 有一张图讲述了UDP的校验方法, 如下: 老师只粗 ...