sql怎么把查詢的數據在新建一個表,sql語句用變量替換表名_使用內存優化表替換SQL臨時表和表變量

 2023-10-18 阅读 18 评论 0

摘要:sql語句用變量替換表名 TempDB usage can be considered as a performance bottleneck for workloads that use SQL temp tables and table variables intensively resulted in heavy IO usage. A valuable alternatives for the SQL temp table and table variable are SCHEM

sql語句用變量替換表名

TempDB usage can be considered as a performance bottleneck for workloads that use SQL temp tables and table variables intensively resulted in heavy IO usage. A valuable alternatives for the SQL temp table and table variable are SCHEMA_ONLY Memory-Optimized tables and the Memory-optimized Table Variable, where the data will be completely stored in the memory without the need to touch the TempDB database, providing the best data access performance. SCHEMA_ONLY Memory-Optimized table and the Memory-optimized Table Variable are stored only in the memory with no related component in the disk. It involves no IO activity or TempDB utilization. It can also participate in the transactions without the need to log the transactions.

對于使用SQL臨時表和大量使用表變量導致IO大量使用的工作負載,可以將TempDB使用視為性能瓶頸。 SCHEMA_ONLY內存優化表和內存優化表變量是SQL臨時表和表變量的寶貴替代品,其中數據將完全存儲在內存中,而無需觸摸TempDB數據庫,從而提供最佳的數據訪問性能。 SCHEMA_ONLY內存優化表和內存優化表變量僅存儲在內存中,而磁盤中沒有相關組件。 它不涉及IO活動或TempDB利用率。 它也可以參與交易,而無需記錄交易。

In this article, we will prove practically that the SCHEMA_ONLY Memory-Optimized Table and the Memory- Optimized Variable Tables are the best replacements for the SQL temp tables and variable tables with better CPU, IO and execution time performance.

在本文中,我們將實際證明SCHEMA_ONLY內存優化表和內存優化變量表是SQL臨時表和具有更好CPU,IO和執行時間性能的變量表的最佳替代。

sql怎么把查詢的數據在新建一個表、 SQL Server In-Memory OLTP, also known as ‘Hekaton’, is a new in-memory processing technology that was introduced in SQL Server 2014, integrated into the SQL Server Database Engine and optimized for an Online Transaction Processing (OLTP) workload.

SQL Server內存中OLTP,也稱為“ Hekaton”,是SQL Server 2014中引入的一種新的內存中處理技術,已集成到SQL Server數據庫引擎中,并針對聯機事務處理(OLTP)工作負載進行了優化。

In-Memory OLTP introduces three new data structures;

內存中OLTP引入了三種新的數據結構:

  • Memory-Optimized Tables that store its data into memory in multiple versions without causing any locks or latches on the data, as it reads from the underlying disk only during the database recovery, achieving the best performance and the most efficient data access. 內存優化表 ,可以將其數據以多種版本存儲到內存中,而不會導致數據上的任何鎖或閂鎖,因為它僅在數據庫恢復期間從基礎磁盤讀取數據,從而實現了最佳性能和最有效的數據訪問。
  • The second data structure is Natively-Compiled Stored Procedures that are compiled to native code (DLL) when they is created and interact only with Memory-Optimized Tables, where any call to that Natively-Compiled Stored Procedure is a call to its related DLL, providing the most efficient query processing and business logic execution.
  • 第二種數據結構是本機編譯的存儲過程 ,該過程在創建時會編譯為本機代碼(DLL),并且僅與內存優化表進行交互,其中對該本地編譯的存儲過程的任何調用都是對其相關DLL的調用,提供最有效的查詢處理和業務邏輯執行。
  • Memory-Optimized Table Variable that is created using the Memory Optimized Table Type. SQL Server generates a DLL for each created Memory-Optimized Table Type that includes the functions required for accessing the indexes and retrieving data from the related Memory-Optimized Table Variable. When that table variable is created using that table type, an instance of the table type components will be created on that table. 變量 。 SQL Server為每個創建的內存優化表類型生成一個DLL,其中包括訪問索引和從相關的內存優化表變量中檢索數據所需的功能。 當使用該表類型創建該表變量時,將在該表上創建表類型組件的實例。

組態 (Configuration)

Memory-Optimized tables can be configured as Durable to recover data in the case of any failure from the transaction logs, which is the default setting, or configured as Non-Durable where the data will not be recovered in the case of failure as the memory buffers will be flushed. The durable setting is used for tables with many updates on daily basis, where any change is required. And the Non-Durable setting is useful in the case of the staging tables that are used for temporary calculations and truncated on daily basis. Setting the Durability option to SCHEMA_ONLY value will recover the table schema only in the case of any failure, where the SCHEMA_AND_DATA Durability value will recover both the schema and the data in the case of any failure.

內存優化表可以配置為“ 持久”以在事務日志出現任何故障的情況下恢復數據(這是默認設置),也可以配置為“ 非持久” ,其中在發生故障的情況下無法恢復數據作為內存緩沖區將被刷新。 持久設置用于每天需要多次更新的表,需要進行任何更改。 對于用于臨時計算并每天被截斷的登臺表,“非持久”設置很有用。 將Durability選項設置為SCHEMA_ONLY值將僅在發生任何故障的情況下恢復表模式,而SCHEMA_AND_DATA Durability值將在發生任何故障的情況下恢復模式和數據。

修改表數據的sql語句、 Another option that should be set when defining non-clustered indexes, while creating the Memory-Optimized Tables, taking into consideration that the clustered index is not supported in the Memory-Optimized Tabled, is the BUCKET_COUNT. The Microsoft recommended value for that option is between 1.5 to 2 times the estimated number of unique values for the column involved in the non-clustered index.

考慮到內存優化表中不支持聚集索引,在創建非內存索引時創建內存優化表時應設置的另一個選項是BUCKET_COUNT 。 Microsoft對于該選項的建議值是非聚集索引中涉及的列的唯一值的估計數量的1.5到2倍。

局限性 (Limitations)

There are some Memory-Optimized OLTP features limitations in SQL Server 2014 that are resolved in SQL Server 2016, such as the supported collation for the string columns involved in the non-clustered index, where the only supported collation was the *_BIN2 collation. Now SQL Server 2016 supports all collation types for that kind of columns.

SQL Server 2014中有一些內存優化的OLTP功能限制,SQL Server 2016中已解決這些限制,例如,非聚集索引所涉及的字符串列的受支持的排序規則,其中唯一受支持的歸類是* _BIN2排序規則。 現在,SQL Server 2016支持此類列的所有排序規則類型。

Another limitation in SQL Server 2014 is that you can’t ALTER any Memory-Optimized Table after creating it, but it is allowed now in SQL Server 2016.

SQL Server 2014中的另一個限制是創建表后不能更改任何內存優化表,但是SQL Server 2016現在允許使用它。

sql一個表的字段更新另一個表的值、 Also, many other features are supported now in SQL Server 2016 such as

此外,SQL Server 2016現在還支持許多其他功能,例如

  • ALTER the Natively-Compiled Stored procedures,

    更改本機編譯的存儲過程,
  • Transparent Data Encryption,

    透明數據加密
  • DML triggers in the Memory-Optimized Tables,

    內存優化表中的DML觸發器,
  • Natively-compiled scalar user-defined functions,

    本機編譯的標量用戶定義函數,
  • Indexes on null-able columns,

    可空列的索引,
  • Parallel plan execution for the operations that access the Memory Optimized tables,

    訪問內存優化表的操作的并行計劃執行,
  • the use of LEFT and RIGHT OUTER JOIN, UNION and UNION ALL, SELECT DISTINCT,

    使用LEFT和RIGHT OUTER JOIN,UNION和UNION ALL,SELECT DISTINCT,
  • OR and NOT statements,

    OR和NOT語句,
  • Nested SELECT statements and stored procedures calling and Large Objects (LOB) usage such as VARCHAR(MAX), NVARCHAR(MAX) or VARBINARY(MAX).

    嵌套的SELECT語句和存儲過程調用以及大對象(LOB)用法,例如VARCHAR(MAX),NVARCHAR(MAX)或VARBINARY(MAX)。

The Memory-Optimized OLTP feature is available in SQL Server 2014 Enterprise Edition only, but in SQL Server 2016 SP1, it is available now in the Standard, Web and Express Editions too.

內存優化的OLTP功能僅在SQL Server 2014 Enterprise Edition中可用,但在SQL Server 2016 SP1中,現在在Standard Edition,Web Edition和Express Edition中也可用。

In this article’s demo we will compare the performance of using five tables types; the SCHEMA_ONLY Memory-Optimized Table, the normal SQL temp table, the physical table, the normal variable table and the Memory- Optimized Variable Table.

在本文的演示中,我們將比較使用五種表類型的性能。 SCHEMA_ONLY內存優化表,常規SQL臨時表,物理表,常規變量表和內存優化變量表。

數據庫中定義變量的語句是。 We will start our demo by preparing the SQLShackDemo test database to host Memory-Optimized tables. This can be achieved by adding a new filegroup that will contain the Memory_Optimized_Data and create a data file under that filegroup. Once the filegroup and database data file created, we will enable the MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT option that is used to lessen the need to have table hints on the Memory-Optimized Tables:

我們將通過準備SQLShackDemo測試數據庫來托管內存優化表來開始演示。 這可以通過添加一個新文件組來實現,該文件組將包含Memory_Optimized_Data并在該文件組下創建一個數據文件。 創建文件組和數據庫數據文件后,我們將啟用MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT選項,該選項用于減少在內存優化表上具有表提示的需求:

?
USE SQLShackDemo 
GO
ALTER DATABASE SQLShackDemo ADD FILEGROUP MemoryOpt_FG CONTAINS MEMORY_OPTIMIZED_DATA?? 
ALTER DATABASE SQLShackDemo ADD FILE (name='MemoryOptDataF',
filename='D:\Data\MemoryOptDataF') TO FILEGROUP MemoryOpt_FG?? 
ALTER DATABASE SQLShackDemo SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT=ON??
GO

You can verify that the Memory-Optimized filegroup is added or add it from the Filegroups page of the Database Properties window as follows:

您可以驗證是否已添加“內存優化”文件組,或從“數據庫屬性”窗口的“文件組”頁面中添加它,如下所示:

After that, we will create the Memory-Optimized Table by turning on the MEMORY_OPTIMIZED option on that table and set the Durability to SCHEMA_ONLY as mentioned previously to simulate the case of SQL temp table, where the table schema will be recovered only in the case of restart or crash:

sql日志查出的語句都有變量。 之后,我們將通過打開該表上的MEMORY_OPTIMIZED選項來創建“內存優化表”,并將持久性設置為SCHEMA_ONLY(如前所述),以模擬SQL臨時表的情況,其中表模式僅在以下情況下才能恢復:重新啟動或崩潰:

?
USE SQLShackDemo 
GO
CREATE TABLE [MemoryOptTempTable]
([ID] INT IDENTITY(1,1) NOT NULL ,[First_Name] nvarchar(10) NULL,[FLast_Name] nvarchar(10) NULL,CONSTRAINT [PK_MemoryOptTempTable]??PRIMARY KEY NONCLUSTERED 
(ID ASC
)
)WITH ( MEMORY_OPTIMIZED = ON , DURABILITY = SCHEMA_ONLY )
GO

We will create also a normal physical table that will be used to store the data temporarily:

我們還將創建一個普通的物理表,該表將用于臨時存儲數據:

?
USE SQLShackDemo 
GO
CREATE TABLE [PhysicalTempTable]
([ID] INT IDENTITY(1,1) NOT NULL ,[First_Name] nvarchar(10) NULL,[Last_Name] nvarchar(10) NULL,CONSTRAINT [PK_PhysicalTempTable]??PRIMARY KEY NONCLUSTERED 
(ID ASC
)
)
GO

In order to create the Memory-Optimized Variable Table, we should create a Memory-Optimized Table Type using the CREATE TYPE statement as below:

為了創建內存優化的變量表,我們應該使用CREATE TYPE語句創建一個內存優化的表類型,如下所示:

?
USE SQLShackDemo 
GO
CREATE TYPE MemoryOptTabVar as TABLE ([ID] INT IDENTITY(1,1) NOT NULL ,[First_Name] nvarchar(10) NULL,[Last_Name] nvarchar(10) NULL,INDEX IX_MemoryOptTabVar_ID NONCLUSTERED (ID)
) WITH (MEMORY_OPTIMIZED = ON)
GO

And to complete the full scenario, we will create five stored procedures, with simple INSERT statements to fill the five tables types mentioned previously. In the case of the normal SQL temp table, variable table and Memory-Optimized Variable Table, the table definition will be included within the stored procedure itself as in the below script:

sql語句where條件是變量、 為了完成整個場景,我們將創建五個存儲過程,并使用簡單的INSERT語句填充前面提到的五種表類型。 對于普通SQL臨時表,變量表和內存優化的變量表,表定義將包含在存儲過程本身中,如以下腳本所示:

?
USE SQLShackDemo 
GO
CREATE PROCEDURE dbo.TempDB_TempTableTest --- Normal SQL temp table
AS
BEGIN
SET NOCOUNT ON;CREATE TABLE #NorTempTable (ID INT IDENTITY(1,1) PRIMARY KEY NONCLUSTERED, [First_Name] 
nvarchar(10) NULL,?? [Last_Name] nvarchar(10) NULL)INSERT INTO #NorTempTable (First_Name,Last_Name) VALUES ('Sanya','J')DROP TABLE #NorTempTableEND
GOCREATE PROCEDURE dbo.MemoryOptTableTest --- Memory Optimized SQL temp table
AS
BEGIN
SET NOCOUNT ON;INSERT INTO MemoryOptTempTable (First_Name,Last_Name) VALUES ('Sanya','J')END
GOCREATE PROCEDURE dbo.PhysicalTableTest --- Physical SQL temp table
AS
BEGIN
SET NOCOUNT ON;INSERT INTO [PhysicalTempTable] (First_Name,Last_Name) VALUES ('Sanya','J')END
GOCREATE PROCEDURE dbo.VariableTable --- normal variable TableASBEGIN
SET NOCOUNT ON;DECLARE @variableTab TABLE (ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, [First_Name] 
nvarchar(10) NULL,?? [Last_Name] nvarchar(10) NULL) ;INSERT??@variableTab (First_Name,Last_Name) VALUES ('Sanya','J')
END
GOCREATE PROCEDURE dbo.MemoryOptVarTable --- Memory Optimized variable TableASBEGIN
SET NOCOUNT ON;DECLARE @MemOptVarTab MemoryOptTabVar ;INSERT @MemOptVarTab (First_Name,Last_Name) VALUES ('Sanya','J')
END
GO

To perform our testing, we will execute the five stored procedures and use it to insert 20000 records each time, leaving 5 seconds delay between each execution to make it easy for us to get the performance difference between it. We will use the PerfMon performance counters monitoring tool to check the performance of these tables, where we will monitor the Processor and Log Bytes Flushed/Sec counters to get the CPU and I/O of each execution as follows:

為了執行測試,我們將執行五個存儲過程,并使用它每次插入20000條記錄,每次執行之間留有5秒的延遲,以使我們能夠輕松地獲得它們之間的性能差異。 我們將使用PerfMon性能計數器監視工具來檢查這些表的性能,在這里我們將監視處理器日志字節刷新/秒計數器,以獲取每次執行的CPU和I / O,如下所示:

?
USE SQLShackDemo
GOEXEC dbo.TempDB_TempTableTest
GO 20000WAITFOR DELAY '00:00:05'
GO 
EXEC dbo.MemoryOptTableTest
GO 20000WAITFOR DELAY '00:00:05'
GO 
EXEC dbo.PhysicalTableTest 
GO 20000WAITFOR DELAY '00:00:05'
GO 
EXEC dbo.MemoryOptVarTable
GO 20000

It is clear from the following PerfMon window that the normal SQL temp table consumes the CPU and I/O resources and took long time to finish compared to the other tables types. You can also derive from the same image that the Memory-Optimized SQL temp table and the Memory-Optimized Variable Table consumed the minimum I/O and CPU resources compared with the rest types of tables:

從下面的PerfMon窗口中可以清楚地看到,與其他表類型相比,普通SQL臨時表占用了CPU和I / O資源,并且花了很長時間完成。 您還可以從同一張圖得出,與其余表類型相比,內存優化SQL臨時表和內存優化的變量表消耗的I / O和CPU資源最少。

sql語句給變量賦值、

The previous script can be modified to show the duration of each stored procedure’s execution as below:

可以修改前面的腳本,以顯示每個存儲過程的執行時間,如下所示:

?
USE SQLShackDemo
GO
CREATE TABLE #StartEndTime(TableType nvarchar(100),StartTime datetime, EndTime Datetime)
INSERT INTO #StartEndTime VALUES('TempTable', GETDATE(),NULL)
GO
EXEC dbo.TempDB_TempTableTest
GO 20000??UPDATE #StartEndTime SET EndTime =GETDATE() WHERE TableType ='TempTable'WAITFOR DELAY '00:00:05'
GO INSERT INTO #StartEndTime VALUES('MemoryOptTable', GETDATE(),NULL)
GO
EXEC dbo.MemoryOptTableTest
GO 20000UPDATE #StartEndTime SET EndTime =GETDATE() WHERE TableType ='MemoryOptTable'WAITFOR DELAY '00:00:05'
GO INSERT INTO #StartEndTime VALUES('PhysicalTable', GETDATE(),NULL)
GO
EXEC dbo.PhysicalTableTest 
GO 20000UPDATE #StartEndTime SET EndTime =GETDATE() WHERE TableType ='PhysicalTable'WAITFOR DELAY '00:00:05'
GO INSERT INTO #StartEndTime VALUES('VariableTable', GETDATE(),NULL)
GO
EXEC dbo.MemoryOptVarTable
GO 20000UPDATE #StartEndTime SET EndTime =GETDATE() WHERE TableType ='VariableTable'WAITFOR DELAY '00:00:05'
GOINSERT INTO #StartEndTime VALUES('Memory Opt VariableTable', GETDATE(),NULL)
GO
EXEC dbo.MemoryOptVarTable
GO 20000UPDATE #StartEndTime SET EndTime =GETDATE() WHERE TableType ='Memory Opt VariableTable'SELECT TableType , DATEDIFF (SS ,StartTime,EndTime) AS Duration FROM #StartEndTime

The result of the previous script will show us that the SP that used the normal SQL temp table is taking the longest execution time compared to the ones using other tables types, and the ones that use the Memory-Optimized SQL temp table and the Memory-Optimized required small period of time to insert 20000 records. Although the SP that uses the Variable table required the minimum time, it consumes the I\O and CPU resources:

上一個腳本的結果將向我們顯示,與使用其他表類型的SP,使用內存優化SQL臨時表和Memory-Optim的SP相比,使用普通SQL臨時表的SP執行時間最長。優化所需的短時間來插入20000條記錄。 盡管使用變量表的SP需要最少的時間,但它會消耗I \ O和CPU資源:

sql如何定義變量,

結論: (Conclusion:)

In-Memory OLTP is a new feature, introduced in SQL Server 2014 and enhanced in SQL Server 2016 by addressing most of the limitations, that stores the data directly to the memory, eliminating the locks and latches occurred during the reading process and providing the best data access performance. From the testing scenario we performed, it is clear that using the SCHEMA_ONLY Memory-Optimized Table and the Memory-Optimized Variable Table excelled on the rest of tables types in having the best performance that can be seen by the minimum CPU and I/O resources consumption and the minimum execution time required to fill these two tables types compared with the other tables, making it the best alternatives to normal SQL temp tables and variable tables.

內存中OLTP是一項新功能,在SQL Server 2014中引入并在SQL Server 2016中得到了增強,通過解決大多數限制,該功能將數據直接存儲到內存中,消除了讀取過程中發生的鎖和閂鎖,并提供了最佳功能數據訪問性能。 從我們執行的測試方案來看,很明顯,使用SCHEMA_ONLY內存優化表和內存優化變量表在其余表類型上表現出色,這是由最少的CPU和I / O資源所能看到的最佳性能與其他表相比,填充這兩個表類型所需的資源消耗和最短執行時間使其成為普通SQL臨時表和變量表的最佳替代方案。

翻譯自: https://www.sqlshack.com/using-memory-optimized-tables-to-replace-sql-server-temp-tables-and-table-variables/

sql語句用變量替換表名

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/144812.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息