Oracle 自动生成的视图VM_NSO_1
有时候优化sql的时候,在执行计划中看到有VM_NSO_X的视图,在Oracle定义中,可以吧NSO理解为nested subquery optimizing,功能就是把in转换为join,把not in转换为anti join等,当然转换的时候有一定的限制。下面我们来简单看下会生成VM_NSO_1视图的几个例子
1. 创建2个测试表
SQL> create table test_jerry as select * from dba_objects; Table created. SQL> create table test_jerry2 as select * from dba_objects; Table created. SQL> select count(*) from test_jerry where object_id not in (select max(object_id) from test_jerry2 group by owner); Execution Plan
----------------------------------------------------------
Plan hash value: 3525080607 ------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | | 908 (1)| 00:00:11 |
| 1 | SORT AGGREGATE | | 1 | 26 | | | |
|* 2 | HASH JOIN RIGHT ANTI NA| | 89846 | 2281K| 2144K| 908 (1)| 00:00:11 |
| 3 | VIEW | VW_NSO_1 | 87509 | 1110K| | 349 (1)| 00:00:05 |
| 4 | HASH GROUP BY | | 87509 | 2563K| | 349 (1)| 00:00:05 |
| 5 | TABLE ACCESS FULL | TEST_JERRY2 | 87509 | 2563K| | 347 (1)| 00:00:05 |
| 6 | TABLE ACCESS FULL | TEST_JERRY | 89847 | 1140K| | 347 (1)| 00:00:05 |
------------------------------------------------------------------------------------------------ Predicate Information (identified by operation id):
--------------------------------------------------- 2 - access("OBJECT_ID"="MAX(OBJECT_ID)") Note
-----
- dynamic sampling used for this statement (level=2) Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2490 consistent gets
2484 physical reads
0 redo size
528 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed SQL> select count(*) from test_jerry where object_id not in (select max(object_id) from test_jerry2); Execution Plan
----------------------------------------------------------
Plan hash value: 3071647562 ------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 694 (1)| 00:00:09 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
|* 2 | TABLE ACCESS FULL | TEST_JERRY | 4492 | 58396 | 347 (1)| 00:00:05 |
| 3 | SORT AGGREGATE | | 1 | 13 | | |
| 4 | TABLE ACCESS FULL| TEST_JERRY2 | 87509 | 1110K| 347 (1)| 00:00:05 |
------------------------------------------------------------------------------------ Predicate Information (identified by operation id):
--------------------------------------------------- 2 - filter("OBJECT_ID"<> (SELECT MAX("OBJECT_ID") FROM "TEST_JERRY2"
"TEST_JERRY2")) Note
-----
- dynamic sampling used for this statement (level=2) Statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
2629 consistent gets
2484 physical reads
0 redo size
528 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed SQL> select count(*) from test_jerry where object_id in (select object_id from test_jerry2 where owner='SYS' minus select object_id from test_jerry where owner='SCOTT'); Execution Plan
----------------------------------------------------------
Plan hash value: 773093838 ----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | | 1307 (1)| 00:00:16 |
| 1 | SORT AGGREGATE | | 1 | 26 | | | |
|* 2 | HASH JOIN | | 32153 | 816K| | 1307 (1)| 00:00:16 |
| 3 | VIEW | VW_NSO_1 | 32153 | 408K| | 960 (1)| 00:00:12 |
| 4 | MINUS | | | | | | |
| 5 | SORT UNIQUE | | 32153 | 941K| 1272K| | |
|* 6 | TABLE ACCESS FULL| TEST_JERRY2 | 32153 | 941K| | 347 (1)| 00:00:05 |
| 7 | SORT UNIQUE | | 14 | 420 | | | |
|* 8 | TABLE ACCESS FULL| TEST_JERRY | 14 | 420 | | 347 (1)| 00:00:05 |
| 9 | TABLE ACCESS FULL | TEST_JERRY | 89847 | 1140K| | 347 (1)| 00:00:05 |
---------------------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 2 - access("OBJECT_ID"="OBJECT_ID")
6 - filter("OWNER"='SYS')
8 - filter("OWNER"='SCOTT') Note
-----
- dynamic sampling used for this statement (level=2) Statistics
----------------------------------------------------------
93 recursive calls
0 db block gets
4691 consistent gets
3726 physical reads
0 redo size
528 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (disk)
1 rows processed SQL> select count(*) from test_jerry where object_id in (select object_id from test_jerry2 where owner='SYS' union all select object_id from test_jerry where owner='SCOTT'); Execution Plan
----------------------------------------------------------
Plan hash value: 1173723582 --------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | 1041 (1)| 00:00:13 |
| 1 | SORT AGGREGATE | | 1 | 26 | | |
|* 2 | HASH JOIN | | 32167 | 816K| 1041 (1)| 00:00:13 |
| 3 | VIEW | VW_NSO_1 | 32167 | 408K| 694 (1)| 00:00:09 |
| 4 | HASH UNIQUE | | 32167 | 942K| 694 (1)| 00:00:09 |
| 5 | UNION-ALL | | | | | |
|* 6 | TABLE ACCESS FULL| TEST_JERRY2 | 32153 | 941K| 347 (1)| 00:00:05 |
|* 7 | TABLE ACCESS FULL| TEST_JERRY | 14 | 420 | 347 (1)| 00:00:05 |
| 8 | TABLE ACCESS FULL | TEST_JERRY | 89847 | 1140K| 347 (1)| 00:00:05 |
-------------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 2 - access("OBJECT_ID"="OBJECT_ID")
6 - filter("OWNER"='SYS')
7 - filter("OWNER"='SCOTT') Note
-----
- dynamic sampling used for this statement (level=2) Statistics
----------------------------------------------------------
82 recursive calls
0 db block gets
4669 consistent gets
3726 physical reads
0 redo size
527 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
8 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(*) from test_jerry where object_id in (SELECT LEVEL FROM DUAL CONNECT BY LEVEL<100); Execution Plan
----------------------------------------------------------
Plan hash value: 3708743834 --------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | 350 (1)| 00:00:05 |
| 1 | SORT AGGREGATE | | 1 | 26 | | |
|* 2 | HASH JOIN | | 1 | 26 | 350 (1)| 00:00:05 |
| 3 | VIEW | VW_NSO_1 | 1 | 13 | 3 (34)| 00:00:01 |
| 4 | HASH UNIQUE | | 1 | | 3 (34)| 00:00:01 |
|* 5 | CONNECT BY WITHOUT FILTERING (UNIQUE)| | | | | |
| 6 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
| 7 | TABLE ACCESS FULL | TEST_JERRY | 89847 | 1140K| 347 (1)| 00:00:05 |
-------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 2 - access("OBJECT_ID"="LEVEL")
5 - filter(LEVEL<100) Note
-----
- dynamic sampling used for this statement (level=2) Statistics
----------------------------------------------------------
4 recursive calls
0 db block gets
1315 consistent gets
1242 physical reads
0 redo size
526 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed
可以从上面的sql得出一个简单的结论,当子查询中出现max,rownum,group by,union all,minus,intersect等聚合函数的时候,Oracle就会自动把子查询转换成视图VM_NSO_X,其实在Oracle的子查询中如果出现上面的几种情况,也就限制了view merge,就无法对视图进行merge。如果需要去掉CBO生成VM_NSO_1,只需要在子查询中加一个no_unnest限制,这样CBO就会走filter了。下面看一个加了no_unnest的执行计划
SQL> select count(*) from test_jerry where object_id in (SELECT /*+ no_unnest() */ LEVEL FROM DUAL t CONNECT BY LEVEL<100); Execution Plan
----------------------------------------------------------
Plan hash value: 2000702637 -------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 5963 (1)| 00:01:12 |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
|* 2 | FILTER | | | | | |
| 3 | TABLE ACCESS FULL | TEST_JERRY | 89847 | 1140K| 347 (1)| 00:00:05 |
|* 4 | FILTER | | | | | |
|* 5 | CONNECT BY WITHOUT FILTERING (UNIQUE)| | | | | |
| 6 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 2 - filter( EXISTS (SELECT /*+ NO_UNNEST */ 0 FROM "SYS"."DUAL" "T" WHERE LEVEL=:B1 CONNECT
BY LEVEL<100))
4 - filter(LEVEL=:B1)
5 - filter(LEVEL<100) Note
-----
- dynamic sampling used for this statement (level=2) Statistics
----------------------------------------------------------
7 recursive calls
0 db block gets
1385 consistent gets
1242 physical reads
0 redo size
526 bytes sent via SQL*Net to client
524 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
86968 sorts (memory)
0 sorts (disk)
1 rows processed
- 本文固定链接: http://www.savedba.com/?p=824
- 转载请注明: 版权所有,文章允许转载,但必须以链接方式注明源地址,否则追究法律责任!
Oracle 自动生成的视图VM_NSO_1的更多相关文章
- Oracle 自动生成hive建表语句
从 oracle 数据库导数到到 hive 大数据平台,需要按照大数据平台的数据规范,重新生成建表的 SQL 语句,方便其间,写了一个自动生成SQL的存储过程. ① 创建一张表,用来存储源表的结构,以 ...
- oracle自动编号
oracle自动编号 在access中有自动编号的数据类型,MSSQL和MYSQL也都有自动增长的数据类型,插入记录时不用操作此字段,会自动获得数据值,而oracle没有自动增长的数据类型,我们需要建 ...
- ORACLE 创建与使用视图
一.what(什么是视图?) 1.视图是一种数据库对象,是从一个或者多个数据表或视图中导出的虚表,视图所对应的数据并不真正地存储在视图中,而是存储在所引用的数据表中,视图的结构和数据是对数据表进行查询 ...
- Oracle 创建和使用视图
一.what(什么是视图?) 1.视图是一种数据库对象,是从一个或者多个数据表或视图中导出的虚表,视图所对应的数据并不真正地存储在视图中,而是存储在所引用的数据表中,视图的结构和数据是对数据表进行查询 ...
- 利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引)
利用 Oracle EM 企业管理器 进行oracle SQL的优化(自动生成索引) ##应用情景 项目中有大量的SQL,尤其是涉及到统计报表时,表关联比较多,当初开发建表时也没搞好索引关联的,上线后 ...
- 基于OCILIB的oracle数据库操作总结及自动生成Model和Dao的工具
基于OCILIB的oracle数据库操作总结 1. 类图 2. 基类BaseOCIDao的设计与实现 BaseOCIDao.h头文件 #pragma once /* ----- ...
- 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)
前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...
- Oracle 每天自动生成AWR报告
经验丰富的老员工希望能够每天为数据库生成1个AWR报告,以便于后期分析数据库的性能变化,手动生成太麻烦,查了一下资料,发现可以自动生成,过程如下. 数据库环境:11gR2 RAC(双节点) AWR报告 ...
- Oracle 函数 “自动生成订单号”
create or replace function get_request_code return varchar2 AS --函数的作用:自动生成订单号 v_mca_no mcode_apply_ ...
随机推荐
- MySQL 索引原理及慢查询优化
MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更好的使用它,已经成为开发工程师的必修课,我们经常会从职位 ...
- Python中pass、continue、break、exit()的区别
pass :不做任何事情,只起到占位的作用 continue: 跳出本次循环 break:结束循环 exit():结束整个程序 由于continue和break较简单,这里就不给出代码
- Pytorch在colab和kaggle中使用TensorBoard/TensorboardX可视化
在colab和kaggle内核的Jupyter notebook中如何可视化深度学习模型的参数对于我们分析模型具有很大的意义,相比tensorflow, pytorch缺乏一些的可视化生态包,但是幸好 ...
- 微信小程序开源
| UI组件 | | | | | | | | | weui-wxss ★1873 - 同微信原生视觉体验一致的基础样式库 | | | | | | zanui-weapp ★794 - 好用易扩展的小程 ...
- Android Studio—增删改查—登录功能
SQLite数据库的常用操作: create table if not exists 表名(字段1 类型(长度),字段2 类型(长度),...)// 建表 drop table if ex ...
- css+vue实现流程图
主要用css+flex布局实现样式部分,vue实现组件逻辑.首先看下效果吧: 当空间不够时还可以使用拖拽功能 接下来说明下实现思路 1.首先是实现单个节点样式,这个很简单不谈了,节点后都跟有一小段连接 ...
- css设置使文字显示2行多余的为省略号...
1 2 3 4 5 6 7 8 9 10 11 12 .title{ font-size: .7rem; line-height: 1.5rem; ove ...
- Set详解
Set集合: 元素不可重复 hashCode 特点:速度快,数组->链表->红黑树 set集合报错元素唯一: 存储元素(String,Interger,....Student,Person ...
- nginx基础模块
http://www.nginx.cn/doc/ 基础模块 HTTP Core模块* HTTP Upstream 模块 HTTP Access 模块 HTTP Auth Basic 模块 HTTP A ...
- excel自增编号
1.首先在单元格 A2:C2 中输入数据. 2.选择 A3:A500, 我们按键盘的 CTRL G 键, 拉起定位框, 然后在参考位置, 输入 A3:A500, 然后单击 "确定" ...