WITH AS: 就是将一个子查询部分独立出来,有时候是为了提高SQL语句的可读性,有时候是为了提高SQL语句性能。
如果一个SQL语句中,某个表会被访问多次,而且每次访问的限制条件一样的话,就可以使用with as来提高性能。
注意:如果 with as 短语没有被调用2次以上,CBO就不会讲这个短语获取的数据放入temp表,如果想要讲数据放入temp表需要使用materialize hint
如果 with as 短语被调用了2次以上,CBO会自动将 with as 短语的数据放入一个临时表,这个时候不用写materialize hint 举个例子(本例基于Scott用户) SQL> explain plan for
with a as (select /*+ materialize */ ename,job,deptno from emp where sal>(select avg(sal) from emp))
select * from a ;
2 3
Explained. SQL> select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------------
Plan hash value: 2006423466 -------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | 8 (0)| 00:00:01 |
| 1 | TEMP TABLE TRANSFORMATION | | | | | |
| 2 | LOAD AS SELECT | SYS_TEMP_0FD9D6605_E16CE | | | | |
|* 3 | TABLE ACCESS FULL | EMP | 1 | 21 | 3 (0)| 00:00:01 |
| 4 | SORT AGGREGATE | | 1 | 4 | | |
| 5 | TABLE ACCESS FULL | EMP | 14 | 56 | 3 (0)| 00:00:01 |
| 6 | VIEW | | 1 | 26 | 2 (0)| 00:00:01 |
| 7 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6605_E16CE | 1 | 17 | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 3 - filter("SAL"> (SELECT AVG("SAL") FROM "EMP" "EMP")) 19 rows selected. 去掉 /*+ materialize */ ,由于只访问了一次a,所以CBO不会将a的查询结果生成一个临时表 SQL> explain plan for
with a as (select ename,job,deptno from emp where sal>(select avg(sal) from emp))
select * from a ; 2 3 Explained. SQL> select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------
Plan hash value: 1876299339 ----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 21 | 6 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL | EMP | 1 | 21 | 3 (0)| 00:00:01 |
| 2 | SORT AGGREGATE | | 1 | 4 | | |
| 3 | TABLE ACCESS FULL| EMP | 14 | 56 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 1 - filter("SAL"> (SELECT AVG("SAL") FROM "EMP" "EMP")) 15 rows selected. WITH AS 语句调用一次 使用多次 需要写hints 如果 表 只 扫描 1次,你些materialize hints 结果读了一次 还写入temp, 再从temp读出来
临时表写入是1次,但是读要多次。 继续测试:
SQL> explain plan for
with a as (select ename,job,deptno from emp where sal>(select avg(sal) from emp))
select * from a union all select * from a; 2 3 Explained. SQL> select * from table(dbms_xplan.display()); PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------------
Plan hash value: 2575088720 --------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 52 | 4 (50)| 00:00:01 |
| 1 | TEMP TABLE TRANSFORMATION | | | | | |
| 2 | LOAD AS SELECT | SYS_TEMP_0FD9D6601_4DC46A | | | | |
|* 3 | TABLE ACCESS FULL | EMP | 1 | 39 | 3 (0)| 00:00:01 |
| 4 | SORT AGGREGATE | | 1 | 13 | | |
| 5 | TABLE ACCESS FULL | EMP | 14 | 182 | 3 (0)| 00:00:01 |
| 6 | UNION-ALL | | | | | |
| 7 | VIEW | | 1 | 26 | 2 (0)| 00:00:01 |
| 8 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6601_4DC46A | 1 | 26 | 2 (0)| 00:00:01 |
| 9 | VIEW | | 1 | 26 | 2 (0)| 00:00:01 |
| 10 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6601_4DC46A | 1 | 26 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 3 - filter("SAL"> (SELECT AVG("SAL") FROM "EMP" "EMP")) Note
-----
- dynamic sampling used for this statement (level=2) 26 rows selected. 充分证明 :
1.当with as 语句没有被调用2次以上时,如果表需要访问多次,那么需要加hints /*+ materialize */ 2.如果with as 语句被调用2次以上时,自动会将 with as 短语的数据放入一个临时表,这个时候不用写materialize hint

WITH AS and materialize hints的更多相关文章

  1. 转://WITH AS and materialize hints

    WITH AS: 就是将一个子查询部分独立出来,有时候是为了提高SQL语句的可读性,有时候是为了提高SQL语句性能. 如果一个SQL语句中,某个表会被访问多次,而且每次访问的限制条件一样的话,就可以使 ...

  2. with as 加上 materialize hint 生成实质临时表

    WITH AS: 就是将一个子查询部分独立出来,有时候是为了提高SQL语句的可读性,有时候是为了提高SQL语句性能. 如果一个SQL语句中,某个表会被访问多次,而且每次访问的限制条件一样的话,就可以使 ...

  3. 利用WITH AS 优化FILTER

    SQL> explain plan for select * from fxqd_list_20131115_new where (acct_no, oper_no, seqno, trans_ ...

  4. 微信 {"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"}

    {"errcode":,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]" ...

  5. 微信 {"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}

    {"errcode":,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]&q ...

  6. Oracle Hints详解

    在向大家详细介绍Oracle Hints之前,首先让大家了解下Oracle Hints是什么,然后全面介绍Oracle Hints,希望对大家有用.基于代价的优化器是很聪明的,在绝大多数情况下它会选择 ...

  7. Materialize - 响应式 Material Design 框架

    由谷歌创建和设计的 Material Design(材料设计)是一种设计语言,结合成功的设计的经典原则以及创新科技.谷歌的目标是开发一个设计系统,让所有的产品在任何平台上拥有统一的用户体验. Mate ...

  8. Materialize一款不错的框架(装逼必备,想想一帮渣渣们还在说bootstrap的时候,你用materialize,高端洋气,别人仰望着,同事们鄙视的看着你还能不能愉快的玩耍的时候,那种孤高的感觉!-_-//意淫结束)

    这个materialize感觉比bootstrap好一点 当然啦中文文档还木有!所以想搞个materialize中文网的可以抢先咯! materialize是谷歌设计制作的一款框架. HOHO,出去别 ...

  9. 19 Using Optimizer Hints

    19.1 Overview of Optimizer Hints A hint is an instruction to the optimizer. In a test or development ...

随机推荐

  1. Linux删除除了某些文件之外的所有文件(夹)

    例如:删除当前目录下除了.tar.gz和.py结尾的其他文件 shopt -s extglob rm -rf !(*.py|*.tar.gz)

  2. dhcp源码编译支持4G上网卡

    1. tar xvzf dhcp-4.2.5-P1.tar.gz 2. ./configure --host=arm-linux ac_cv_file__dev_random=yes 3. vi bi ...

  3. HDU-1114(背包DP)

    Piggy-Bank Problem Description Before ACM can do anything, a budget must be prepared and the necessa ...

  4. Android- Context理解

    学习了安卓以后还是不理解Context的用法:Api文档链接http://wear.techbrood.com/reference/android/content/Context.html 一个非常好 ...

  5. 不安装oracle客户端,连接到服务器的oracle (注:针对 odp.net)

    前几天在研究怎样不安装oracle客户端去访问oracle,并把里面的数据同步到本地的Sql Server数据库中. 准备工作:首先你得有如下.dll,我这个是针对oracle10g的,如果是更高的版 ...

  6. maven中tomcat7-maven-plugin插件的使用

    1.(挺清晰,但是我在项目上尝试没有成功) http://blog.csdn.net/yhhazr/article/details/7866501 2.(算是有一些详细的运行命令吧,例如自动打包命令或 ...

  7. CI 笔记,借鉴的4个辅助自定义函数

    在System的core的common.php中,借鉴的4个自定义函数, 摘自后盾网的CI教程 /** * 格式化打印函数 * @param [type] $arr [数组] * @return [t ...

  8. iOS textfield实现一行的数字限制,超出进行弹框

    步骤一:添加textfield协议‘ @interface LsGeXingQianMingVC ()<UITextFieldDelegate> 步骤2:设置代理 _GeXingQianM ...

  9. C语言中short的意思

    short和int等一样,是C或C++的一种内部数据类型.用于表示有符号整数.不同的是,他们在内存中所占的空间大小不同,short通常为int所占一半,也有一些实现为和int一样,但不会比int大.所 ...

  10. Java面向对象程序设计--接口和内部类

    1.接口的定义: In the Java programming language, an interface is not a class but          staff[0] =       ...