Original | Reasonable use of temporary tables for SQL optimization

LookLarge sea view'S tweets? Will usSet as starJust~

SQL column

SQL basic knowledge summary

SQL advanced knowledge summary

Today we will talk about optimization techniques for temporary tables

Temporary table, as the name implies, is a table that is temporarily used. 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.

1. Local temporary table

The local temporary table can add # in front of the table name, let's take a look at the characteristics of the local temporary table

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:

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

SELECT * FROM #temp;

The results are as follows:

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

2. Global temporary table

The global temporary table can be added with ## before the table name, and 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:

We 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.

3. The optimization method of temporary table

After introducing the temporary table, let's talk about how to use it to optimize

The optimization of temporary tables is generally called nested queries when there are many sub-queries. 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 implementation:

It can be seen that the logical read here is relatively high.

Let's use the temporary table to re-look at the implementation. We insert the first and second layer query results 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:

Compared with the last logical read, the number of logical reads is reduced by a factor of two.When adjusting the performance of a query, if the logical read value drops, it indicates that the server resources used by the query decrease and the performance of the query improves. If the logical read value increases, it means that the adjustment measures reduce the performance of the query. In the case of other conditions unchanged, the fewer logical reads a query uses, the higher its efficiency and the faster the query.

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

That’s all for today’s class. If you don’t understand, you can leave a message below. I will reply one by one.

——End——

Pay attention to the public account of SQL database development, reply keywords in the background:Information collection, You can get a carefully organized technical dry goods.

Recommended reading

Click "Read the original''Understanding SQL Training Camp

Intelligent Recommendation

Dynamic tables and temporary tables in Flink SQL

1 dynamic table 1.1 Relational query on data stream The comparison between relational SQL and stream processing is as follows: SQL Stream processing Finite element group Infinite tuple Queries on the ...

Sql server cursor instance: use cursors, temporary tables to query data from multiple tables

This example table item is divided into monthly items such as Item201901 and Item201902 according to the month, so how to freely query the data of the time period across the month....

SQL Server database engine optimization consultant-6. Sorting rules conflict with temporary tables and table variables.

When your user database sorting rules do not match the sorting rules of TEMPDB, you have a potential problem. Temporary tables and table variables are created in TEMPDB. When you do not specify the so...

Use temporary table optimization big table connection query in SQL Server

In recent projects, there is a problem with a functional problem. Excel data of timing tasks fails to send it to the customer's email. The reason for inspection is that the CLR cancels the process aft...

[SQL Server] SQL usage and delete temporary tables

In this example, it represents a temporary table, a '#' denotes a local temporary tables, two '##' denotes a global temporary table, typically when the connection is closed by the DBMS delete temporar...

More Recommendation

Use of Oracle temporary tables 2

Temp Table features: (1) Independence of multi-user operation: For different users who use the same temporary table, ORACLE will allocate a separate Temp Segment, which avoids the intersection of mult...

Use of Oracle temporary tables 1

1. Session-specific temporary table           CREATE   GLOBAL   TEMPORARY   <TABLE_NAME>   (<column   specification>)      ...

Use of temporary tables in stored procedures

The following is the use of temporary tables in the stored procedure  Temporary tables   1. You can create local and global temporary tables. Local temporary tables are only visible in the c...

The use of Oracle's two temporary tables

Since the data format used in the project is xml, the database of the old system is storing the xml data in the clob field. However, due to the system upgrade, the data stored in the clob field is not...

Why use internal temporary tables

Why use internal temporary tables The semantics of union is to take the union of the results of these two subqueries, and only one row is reserved for duplicate rows. The key=Primary in the second lin...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top