SQL optimization lecture (3)-reasonable use of temporary tables

SQL column

Summary of SQL basics

SQL advanced knowledge summary

Today we will talk about optimization techniques for temporary tables

Temporary table, as the name implies, is only a temporary table. One is a local temporary table, which can only be used on the current query page. It cannot be used for newly opened queries. The other is a global temporary table, no matter how many query pages are opened. be usable.


Local temporary table

For local temporary tables, add # in front of the table name. Let’s take a look at the characteristics of local temporary tables.

We create a new query page and enter the following code:


SELECT TOP 10 * INTO #temp
FROM sales.Temp_Salesorder;
SELECT * FROM #temp;

The results are as follows:

SQL optimization lecture (3)-reasonable use of temporary tables

Let's open a new page and re-enter the following code:


SELECT * FROM #temp;

The results are as follows:

SQL optimization lecture (3)-reasonable use of temporary tables

Prove that the local temporary table can only be executed on the current page.

Global temporary table The
global temporary table can be used by adding ## in front of the table name. It can be used when opening any query page.

Repeat the above steps:


SELECT TOP 10 * INTO ##temp
FROM sales.Temp_Salesorder
SELECT * FROM ##temp;

The result is the same as above:

SQL optimization lecture (3)-reasonable use of temporary tables
Let's open a new page:

SELECT * FROM ##temp;

The result is still the same. Prove that all query pages of the global temporary table can be used.

Temporary table optimization method After
introducing the temporary table, let’s talk about how to use it to optimize

The optimization of temporary tables is generally used when there are more subqueries, which is also called nested query. We write the following subquery:


SELECT * FROM sales.Temp_Salesorder
WHERE SalesOrderDetailID IN
(SELECT SalesOrderDetailID FROM sales.SalesOrderDetail
WHERE UnitPrice IN
(SELECT UnitPrice FROM sales.SalesOrderDetail WHERE UnitPrice>0)
)

(Hint: the code can slide left and right)

This is a relatively simple two-level nested subquery, let's take a look at the execution:

SQL optimization lecture (3)-reasonable use of temporary tables

You can see that the logic read here is relatively high.

Let's use the temporary table to revisit the execution situation. We insert the query results of the first and second levels into #temp, and then query the results from the temporary table.


SELECT SalesOrderDetailID INTO #temp FROM sales.SalesOrderDetail
WHERE UnitPrice IN (SELECT UnitPrice FROM sales.SalesOrderDetail WHERE UnitPrice>0)

SELECT * FROM sales.Temp_Salesorder
WHERE SalesOrderDetailID IN
(SELECT SalesOrderDetailID FROM #temp)

The implementation is as follows:

SQL optimization lecture (3)-reasonable use of temporary tables

SQL optimization lecture (3)-reasonable use of temporary tables

Compared with the last logical read, the number of logical reads is doubled. When adjusting the performance of the query, if the logical read value drops, it indicates that the server resources used by the query are reduced and the performance of the query is improved. If the logical reading value increases, it means that the adjustment measures have reduced the performance of the query. When other conditions remain unchanged, the less logical reads a query uses, the higher its efficiency and the faster the query speed.

Therefore, we can see that temporary tables can improve query efficiency in more complex nested queries.

Today’s lesson will stop here. If you don’t understand, you can leave a message below, and I will reply one by one.

Guess you like

Origin blog.51cto.com/15057820/2656462