Using with as to improve performance simplifies nested SQL

Source: Internet
Author: User

A Meaning of with AS

The with as phrase, also called the subquery section (subquery factoring), allows you to do a lot of things, define a SQL fragment that will
is used by the entire SQL statement. Sometimes, it is to make the SQL statement more readable, or it may be in different parts of union all, as a number
of the data.
Especially useful for union all. Because each part of union all may be the same, but if each part is executed again, the cost is too high,
So you can use the with as phrase, and just do it once. If the table name defined by the with as phrase is called more than two times, the optimizer automatically
The data obtained by the with as phrase is placed in a temp table and will not if it is called only once. The hint materialize is to force the with AS
The data in the phrase is placed in a global temporary table. Many queries can improve speed in this way.

Two How to use

Let's look at one of the following nested query statements:


The query statement above uses a subquery. Although this SQL statement is not complex, it can make SQL statements very difficult to read if too many layers are nested.
Read and maintain. Therefore, you can also use table variables to solve this problem.

The SQL statements are as follows:


Insert into @t (Countryregioncode)



Although the above SQL statement is more complex than the first, it puts the subquery in the table variable @t, which makes the SQL statement easier to maintain, but
will bring another problem, is the loss of performance. Because table variables actually use temporary tables, which increases the additional I/O overhead, the way the table variables
is not very suitable for large data volumes and frequent queries. To do this, another solution is provided in SQL Server 2005, which is a common table expression (CTE) that uses a CTE to increase the maintainability of the SQL statement while the CTE is much more efficient than the table variable.

Here is the syntax for the CTE:

[With <common_table_expression> [, N]]
<common_table_expression>::=
Expression_name [(column_name [, N])]
As
(cte_query_definition)


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







Where CR is a common table expression that is similar to a table variable in use, except that SQL Server 2005 has a way of handling common table expressions
are different.

The following points should be noted when using a CTE:

1. The CTE must be followed directly with the SQL statement that uses the CTE (such as SELECT, Insert, UPDATE, and so on), otherwise the CTE will fail. The following SQL statement cannot be
Frequently used CTE:






SELECT * FROM person. CountryRegion --This SQL statement should be removed


2. The CTE can also be followed by other CTE, but only one with, the middle of multiple CTE separated by commas (,), as shown in the following SQL statement:















3. If the expression name of the CTE is the same as a data table or view, the SQL statement immediately following the CTE is still using the CTE, of course, the following SQL statement
The data table or view is used, as shown in the following SQL statement:

--  table1 is a table that actually exists





SELECT * FROM table1 - uses a common table expression named table1
SELECT * FROM table1 - using a data table named Table1


4. The CTE can refer to itself, or it can refer to a pre-defined CTE 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 the TOP clause is specified)

(3) into

(4) OPTION clause with query hint

(5) for XML

(6) for BROWSE


6. If the CTE is used in a statement that is part of a batch, then the statement before it must end with a semicolon, as shown in the following SQL:



; --Must add a semicolon





Reprint : http://wudataoge.blog.163.com/blog/static/80073886200961652022389/

Using with as to improve performance simplifies nested SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.