加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长百科 > 正文

关于oracle with as用法

发布时间:2021-03-31 20:23:38 所属栏目:站长百科 来源:网络整理
导读:with as语法 –针对一个别名 with tmp as (select * from tb_name) –针对多个别名 with ?? tmp as (select * from tb_name), ?? tmp2 as (select * from tb_name2), ?? tmp3 as (select * from tb_name3), ?? … 1 2 3 4 5 6 7 8 9 --相当于建了个e临时表 w
1 2 3 4 5 6 7 8 9 10 with????sql1 as? (select? to_char(a) s_name from? test_tempa),????sql2 as? (select? to_char(b) s_name from? test_tempb where? not? exists (select? s_name from? sql1 where? rownum=1))select? * from? sql1union? allselect? * from? sql2union? allselect? ‘no records‘? from? dual???????where? not? exists (select? s_name from? sql1 where? rownum=1)???????and? not? exists (select? s_name from? sql2 where? rownum=1);

with as优点增加了sql的易读性,如果构造了多个子查询,结构会更清晰;更重要的是:“一次分析,多次使用”,这也是为什么会提供性能的地方,达到了“少读”的目标

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

with as短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个sql片断,该sql片断会被整个sql语句所用到。有的时候,是为了让sql语句的可读性更高些,也有可能是在union all的不同部分,作为提供数据的部分。
  
特别对于union all比较有用。因为union all的每个部分可能相同,但是如果每个部分都去执行一遍的话,则成本太高,所以可以使用with as短语,则只要执行一遍即可。如果with as短语所定义的表名被调用两次以上,则优化器会自动将with as短语所获取的数据放入一个temp表里,如果只是被调用一次,则不会。而提示materialize则是强制将with as短语里的数据放入一个全局临时表里。很多查询通过这种方法都可以提高速度。

推荐文章
    热点阅读