ORACLE WITH AS 用法 ORACLE WITH AS 用法

 

turn:

ORACLE WITH AS 用法

With query to select not start, but in the beginning "WITH" keywords
    can be considered in advance before the actual query constructs a temporary table, then it can be used several times for further analysis and processing

WITH Clause advantage of the method of
     increasing the readability of SQL, if constructed with multiple sub-queries, the structure will be clearer; more important is: "one analysis, use many times", which is why the place will provide performance, reaching "read less" objective.

     The first method uses a subquery table is scanned twice, and use the WITH Clause method, the table is scanned only once. This can greatly improve the efficiency of data analysis and query.

     In addition, the observation WITH Clause method implementation plan "SYS_TEMP_XXXX" is constructed in the middle of statistics during operation temporary table.

 

grammar:

with tempName as (select ....)
select ...

 

Copy the code
- For an alias
 with tmp AS ( the SELECT * from tb_name) - For more aliases with tmp AS ( the SELECT * from tb_name), TMP2 (AS the SELECT * from tb_name2), tmp3 AS ( the SELECT * from tb_name3), ...
Copy the code

 

Copy the code
- equivalent to built a temporary table e
 with e AS ( SELECT * from scott.emp e WHERE e.empno = 7499 ) SELECT * from e; - equivalent built e, d temporary table with e AS ( SELECT * from scott.emp), D AS ( SELECT * from scott.dept) SELECT * from E, D WHERE e.deptno = d.deptno;
Copy the code

 

In fact, the repeated use of the sql statement on a lot with as inside, an alias, followed by a query you can use it, for such large quantities of sql statement optimization plays a role, but clear.

Insert data into a table with as usage:

insert into table2
with
    s1 as (select rownum c1 from dual connect by rownum <= 10), s2 as (select rownum c2 from dual connect by rownum <= 10) select a.c1, b.c2 from s1 a, s2 b where...;

with as the equivalent of a virtual view.

with as phrases, also called sub-query part (subquery factoring), allows you to do a lot of things, the definition of a piece of sql, the sql fragment will be used throughout the sql statement.

Sometimes, in order to make more readable some sql statements, there may be different parts of the union all, as part of providing the data.
  
Particularly useful for all union.

Because each part of the union all may be the same, but if each part to perform it again, then the cost is too high, it can be used with as a phrase, then just run it again.

If with the phrase as defined in the table name was called twice more, the optimizer will automatically with data obtained as a phrase into a temp table, if only called once, it will not.

The prompt will materialize is mandatory data with as in the phrase into a global temporary table.

Many of the queries by this method can improve the speed.

Copy the code
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);
Copy the code

with as advantage
increases the legibility sql, if constructed with multiple sub-queries, the structure will be clearer;
more important is: "one analysis, use many times", which is why the provision of local performance, to achieve a "less read "the goal

Guess you like

Origin www.cnblogs.com/libin6505/p/12144347.html