Temporary table of SQL server

Temporary table definition

	SQL Server临时表是临时对象的一种,临时对象是以#或者##为前缀的,
诸如临时存储过程、临时函数等都是临时对象,临时对象都存储在tempdb数据库中。
当我们关闭数据库时,临时表会自动删除。

Temporary table classification

	临时表分为本地临时表和全局临时表,它们在名称、可见性以及可用性上有区别。
以一个#号为前缀的临时表是本地的,只有当前用户会话才可以访问,以两个#号
也就是##为前缀的临时表是全局的。

The characteristics of temporary tables

	本地临时表就是用户在创建表的时候添加了"#"前缀的表,其特点是根据数据库
连接独立。只有创建本地临时表的数据库连接有表的访问权限,其它连接不能访问
该表;不同的数据库连接中,创建的本地临时表虽然"名字"相同,但是这些表之间
相互并不存在任何关系;在SQLSERVER中,通过特别的命名机制保证本地临时表在数
据库连接上的独立性,意思是你可以在不同的连接里使用相同的本地临时表名称。
	全局临时表是用户在创建表的时候添加"##"前缀的表,其特点是所以数据库连接
均可使用该全局临时表,当所有引用该临时表的数据库连接断开后自动删除。
	全局临时表相比本地临时表,命名上就需要注意了,与本地临时表不同的是,全
局临时表名不能重复。
	临时表利用了数据库临时表空间,由数据库系统自动进行维护,因此节省了物理
表空间。并且由于临时表空间一般利用虚拟内存,大大减少了硬盘的I/O次数,因此
也提高了系统效率。临时表在事务完毕或会话完毕数据库会自动清空,不必记得用完
后删除数据。

Local temporary tables
The names of local temporary tables start with a single number sign "#"; they are only visible to the current user connection (that is, the connection that created the local temporary table); they are deleted when the user disconnects from the SQL Server instance .
We take the Customers table as an example. The table data is as follows:
Insert picture description here
We create a new connection. Whenever "new query" represents a connection is opened, the connection ID is the number after sa, and our connection ID is 57.
Insert picture description here
Here we are The query page creates a temporary table.

SELECT * INTO #Customers FROM Customers

So we have built a temporary table, you can query the data of the temporary table #Customers. Consistent with Customers content.
Insert picture description here
What if we open another page and also query the #Customers table?
Insert picture description here
We execute the above query statement on the newly opened query page, and the result is as follows: It
Insert picture description here
means that the local temporary table does not support cross-connection query. It can only be accessed in the current connection (or the current query page).
Where exactly is the local temporary table? How is it stored?
Insert picture description here
This is the temporary table we just created, which is not represented by #Cusomters in the system.

Global Temporary Table
The name of a global temporary table starts with two number signs "##". After creation, it is visible to any database connection. When all database connections referencing the table are disconnected from SQL Server, they are deleted.
Global Temporary Table Example
Let’s follow the steps above.
First open a query page and enter the following query statement:

SELECT * INTO ##Customers FROM Customers

After executing the above query statement, we close the query page and reopen a page to query the content in ##Customers

SELECT * FROM ##Customers

The result is as follows:
Insert picture description here
At this time, it will not report an error like the local temporary table.
The location of the global temporary table is as follows:
Insert picture description here
its name is consistent with our customized name, and the system will not add additional information.

Use of temporary tables

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 many sub-queries, also known as nested 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)
)

This is a relatively simple two-level nested subquery. Let's take a look at the execution:
Insert picture description here
you can see that the logical read here is relatively high.

We use the temporary table to revisit the execution situation. We insert the first and second level 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:
Insert picture description hereInsert picture description here
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. With other conditions 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.

Annotated
temporary tables are used in SQL Server or other platforms. It can greatly improve query efficiency in query optimization. The temporary tables of SQL Server platform are easier to create and use than other platforms, and its superiority is not to mention Yu. Therefore, if you are in the process of working or studying, the temporary table can be used frequently as a necessary skill.

Guess you like

Origin blog.csdn.net/Y_6155/article/details/105843242