Oracle commonly used writing --with as usage to improve query performance

Database sql with as usage to improve query performance
1 . The meaning of  WITH AS The
    WITH AS phrase, also known as subquery factoring, allows you to do many things, define a SQL fragment that will be used by the entire SQL statement. Sometimes, it is to make the SQL statement more readable, or it may be in a different part of the UNION ALL, as the part that provides the data. 
Especially useful for UNION ALL. Because each part of UNION ALL may be the same, but if each part is executed once, the cost is too high, so the WITH AS phrase can be used, and only need to be executed once. If the table name defined by the WITH AS phrase is called more than twice, the optimizer will automatically put the data obtained by the WITH AS phrase into a TEMP table, if it is called only once, it will not. The prompt materialize is to force the data in the WITH AS phrase into a global temporary table. Many queries can be speeded up this way.


two. Usage First
look at the following nested query:


select * from person.StateProvince where CountryRegionCode in 
(select CountryRegionCode from person.CountryRegion where Name like 'C%')


    The above query uses a subquery. Although this SQL statement is not complicated, if there are too many levels of nesting, it will make the SQL statement very difficult to read and maintain. Therefore, table variables can also be used to solve this problem. The SQL statement is as follows:


declare @t table(CountryRegionCode nvarchar(3))
insert into @t(CountryRegionCode) (select CountryRegionCode from person.CountryRegion where Name like 'C%')


select * from person.StateProvince where CountryRegionCode in (select * from @t)




    Although the above SQL statement is more efficient than the first way Complex, but the sub-query is placed in the table variable @t, which will make the SQL statement easier to maintain, but it will bring another problem, which is the loss of performance. Since the table variable actually uses a temporary table, which adds additional I/O overhead, the table variable method is not very suitable for the case of a large amount of data and frequent queries. To this end, another solution is provided in SQL Server 2005, which is Common Table Expression (CTE). Using CTE can make SQL statements more maintainable. At the same time, CTE is much more efficient than table variables. .




3. With as syntax
    The following is the syntax of CTE:


[ WITH <common_table_expression> [ ,n ] ]
<common_table_expression>::=
        expression_name [ ( column_name [ ,n ] ) ]
    AS
        ( CTE_query_definition )


    Now use CTE to solve the above problem, The SQL statement is as follows:


with
cr as
(
    select CountryRegionCode from person.CountryRegion where Name like 'C%'
)


select * from person.StateProvince where CountryRegionCode in (select * from cr)


    where cr is a common table expression, which is similar in use to table variables, except SQL Server 2005 differs in how it handles common table expressions.


    When using CTE, you should pay attention to the following points:




1. CTE must be directly followed by SQL statements using CTE (such as select, insert, update, etc.), otherwise, CTE will be invalid.


For example, the following SQL statement will not work with CTE:
with
cr as
(
    select CountryRegionCode from person.CountryRegion where Name like 'C%'
)
select * from person.CountryRegion -- this SQL statement should be removed


-- use CTE's SQL The statement should immediately follow the relevant CTE --
select * from person.StateProvince where CountryRegionCode in (select * from cr)




2. The CTE can also be followed by other CTEs, but only one with can be used, and multiple CTEs can be separated by commas (,) separated


As shown in the following SQL statement:


with
cte1 as
(
    select * from table1 where name like 'abc%'
),
cte2 as
(
    select * from table2 where id > 20
),
cte3 as
(
    select * from table3 where price < 100
)
select a.* from cte1 a, cte2 b, cte3 c where a.id = b.id and a.id = c.id




3. If the expression name of the CTE has the same name as a data table or view, follow The SQL statement following the CTE still uses the CTE. Of course, the following SQL statement uses the data table or view,


as shown in the following SQL statement:


-- table1 is an actual table


with
table1 as
(
    select * from persons where age < 30
)
select * from table1 -- uses a common table expression named table1
select * from table1 -- A data table named table1 is used




4. A CTE can refer to itself or a CTE that is predefined in the same WITH clause. Forward references are not allowed.




5. The following clauses cannot be used in CTE_query_definition:


(1) COMPUTE or COMPUTE BY


(2) ORDER BY (unless a TOP clause is specified)


(3) INTO


(4) OPTION clause with query hints


(5) FOR XML


(6) FOR BROWSE




6. If a CTE is used in a statement that is part of a batch, the statement preceding it must end with a semicolon


as shown in the following SQL:


declare @s nvarchar(3)
set @s = 'C%'
; -- must add semicolon
with
t_tree as
(
    select CountryRegionCode from person.CountryRegion where Name like @s
)
select * from person.StateProvince where CountryRegionCode in (select * from t_tree)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326839669&siteId=291194637