- 浏览: 51559 次
- 性别:
- 来自: 北京
最新评论
查看( 15 ) / 评论( 0 ) / 评分( 0 / 0 )
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 sql1
union all
select * from sql2
union all
select '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 a as (select * from test)
select * from a;
其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它
这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了
这是搜索到的英文文档资料(说得比较全,但是本人英文特菜,还没具体了解到,希望各高手具体谈谈这个with
as 的好处)
About Oracle WITH clause
Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.
The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:
• The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
• Formally, the “WITH clause” is called subquery factoring
• The SQL “WITH clause” is used when a subquery is executed multiple times
• Also useful for recursive queries (SQL-99, but not Oracle SQL)
To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query.
We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.
The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:
WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);
Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH clause”:
WITH
sum_sales AS
select /*+ materialize */
sum(quantity) all_sales from stores
number_stores AS
select /*+ materialize */
count(*) nbr_stores from stores
sales_by_store AS
select /*+ materialize */
store_name, sum(quantity) store_sales from
store natural join sales
SELECT
store_name
FROM
store,
sum_sales,
number_stores,
sales_by_store
where
store_sales > (all_sales / nbr_stores)
;
Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.
It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.
To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy
The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:
WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);
Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:
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 sql1
union all
select * from sql2
union all
select '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 a as (select * from test)
select * from a;
其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它
这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了
这是搜索到的英文文档资料(说得比较全,但是本人英文特菜,还没具体了解到,希望各高手具体谈谈这个with
as 的好处)
About Oracle WITH clause
Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.
The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:
• The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
• Formally, the “WITH clause” is called subquery factoring
• The SQL “WITH clause” is used when a subquery is executed multiple times
• Also useful for recursive queries (SQL-99, but not Oracle SQL)
To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query.
We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.
The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:
WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);
Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH clause”:
WITH
sum_sales AS
select /*+ materialize */
sum(quantity) all_sales from stores
number_stores AS
select /*+ materialize */
count(*) nbr_stores from stores
sales_by_store AS
select /*+ materialize */
store_name, sum(quantity) store_sales from
store natural join sales
SELECT
store_name
FROM
store,
sum_sales,
number_stores,
sales_by_store
where
store_sales > (all_sales / nbr_stores)
;
Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.
It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.
To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy
The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:
WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);
Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:
发表评论
-
Oracle的left join中on和where的区别
2012-10-26 15:43 960Oracle的left join中on和where的区别 数 ... -
动态sql拼接单引号与 变量赋值
2012-06-21 10:09 3827if (lower(s_table)='gl_deta ... -
ora-00031:session marked for kill处理oracle中杀不掉的锁
2012-06-19 16:36 1042转: 一些ORACLE中的进程被杀掉后,状态被置为" ... -
使用DBLINK为远程数据库的用户表创建同义词
2012-06-19 14:39 9121、创建db_link create database l ... -
以字符串数组为输入参数的存储过程
2012-06-19 14:27 2725今天项目中需要用到存 ... -
函数的使用
2012-06-04 17:09 967create or replace function Fun_ ... -
Oracle中decode的使简单例子
2012-06-04 17:06 1462decode(字段,表达式1,表达式2,表达式。。。) 当,字 ... -
oracle触发器
2012-04-20 09:11 722表: A 字段:a, b, c 当修改a字段的值时触发 c字段 ... -
oracle的全文索引
2012-04-16 14:29 781已有几个项目组开始使 ... -
单独的plsql链接数据库
2011-10-14 16:03 895转的: plsql 可不可以 ... -
Oracle经验集锦
2011-09-24 13:32 6801.删除表空间 DROP ... -
两个数据库字符集不一样,如何快速增量同步数据.
2011-09-23 15:00 1136环境: DB-A 字符集:US7ASC ... -
Oracle分区表详解 .
2011-08-06 10:42 636一、Oracle分区简介 ORACLE的分区是一种处理超大型 ... -
利用 rowid 提升update性能 .
2011-08-04 23:52 1798能不能想办法 提升一下如下update语句的性能 UPDA ... -
oracle全文索引的简单配置
2011-08-04 23:49 9951.创建数据存储定义(Datastore),使用多列数据存储在 ... -
用java调用oracle存储过程总结
2011-08-04 23:45 6351、什么是存储过程。存 ... -
Oracle SCN详解
2011-08-04 23:38 600pre. Oracle中的SCN(system change ... -
oracle 数据库里查看表空间使用状况
2011-08-04 23:34 635oracle表空间的事情状况要经常查看,一般空闲比例过低的时候 ... -
采用全文索引解决模糊查询速度慢的问题
2011-08-04 23:31 749众所周知,使用 like 进行模糊查询速度极差,包括 like ... -
oracle9i在windows上的dataguard配置
2011-08-04 23:23 701主库:win2003 server ora9i(9.2.0.1 ...
相关推荐
一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ...
下面是一个简单的文件操作示例,展示了`with...as`的用法: ```python with open("/tmp/foo.txt") as file: data = file.read() ``` 在这个例子中: 1. `open("/tmp/foo.txt")`表达式被求值,返回一个文件对象。 ...
详细介绍oracle数据库中新出的with_as语法以及相关使用
with expression as variable: block ``` 这里的 `expression` 是一个可调用对象,通常是一个打开文件、数据库连接或其他需要初始化的资源。`as variable` 是可选的,用于在 `with` 代码块内部引用该资源。 在 `...
### Oracle数据库中的START WITH 和 CONNECT BY 用法详解 在Oracle数据库中,处理层次结构数据时,`START WITH` 和 `CONNECT BY` 是非常有用的两个关键字。这些关键字可以帮助我们在查询时构建出树形或者层级结构的...
这篇文章主要介绍了python with (as)语句实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 with语句适用于对资源进行访问的场合,确保不管使用过程中...
with EXPRESSION [ as VARIABLE] WITH-BLOCK 基本思想是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法。 紧跟with后面的语句被求值后,返回对象的__enter__()方法被调用,这个方法的返回值将被...
本文将深入探讨WITH CHECK OPTION的用法及其背后的原理,帮助读者更好地理解并掌握这一特性。 ### 什么是WITH CHECK OPTION? 在创建视图时,可以通过添加WITH CHECK OPTION子句来指定视图的某些约束条件。这个...
with context expression [as target(s)]: with 代码块 在上面的语法格式中,context_expression 用于创建可自动关闭的资源。 例如,程序使用 with 语句来读取文件: import codecs # 使用with语句打开文件,该...
with/as 使用open打开过文件的对with/as都已经非常熟悉,其实with/as是对try/finally的一种替代方案。 当某个对象支持一种称为”环境管理协议”的协议时,就会通过环境管理器来自动执行某些善后清理工作,就像...
在英语中,"as-to"和"as-for"是两个常用的介词短语,它们在句子中的用法和含义略有不同。以下是对这两个短语的详细解析: 首先,"as for"主要用于转换话题或者强调某个特定的观点。它可以出现在句首,以引入与前文...
临时表分为三种类型:本地临时表(以#开头),全局临时表(以##开头)以及行集函数中的表变量(如WITH AS)。在本文中,我们将探讨这三种临时表的使用场景,以及`INSERT INTO`、`SELECT INTO`和`WITH AS`语句的应用...
with open('cardlog.txt','r') as item : for line in item : print line; 在file的结束,会自动关闭该文件句柄。 在python2.6中,with正式成为了关键字 所以在python2.5以前,要利用with的话,需要使用...
with expression [as target]: suite ``` 这里的 `expression` 是一个产生上下文管理器的对象,它可以是一个函数调用或者类的实例。`as target` 部分是可选的,如果存在,`__enter__()` 方法返回的结果将会被赋值给...
Oracle 下 WITH CHECK OPTION 用法 WITH CHECK OPTION 是 Oracle 中的一种视图定义选项,它可以确保数据库中正在修改的数据的完整性。该选项通常用在视图定义中,以确保任何引用该视图的 INSERT 或 UPDATE 语句都...
当使用`with A() as a:`时,`__enter__()`会在进入`with`块之前被调用,`__exit__()`则在退出`with`块时调用,参数`e_type`, `e_value`, `traceback`用于处理异常情况。 Python的`contextlib`模块提供了更高级的上...
在这个例子中,`open`函数返回了一个文件对象,我们通过`as`关键字将其绑定到变量`file`上,然后在`with`语句块内使用`file`来读取文件内容。 #### 四、总结 通过上述介绍可以看出,`as`关键字在Python编程中扮演...
本博客简单介绍一下oracle分组函数之rollup的用法,rollup函数常用于分组统计,也是属于oracle分析函数的一种 环境准备 create table dept as select * from scott.dept; create table emp as select * from ...
软件开发网在此之前给大家介绍过图片加载...Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget() { @Override public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> gl