ایجاد جداول بهینه سازی شده برای حافظه در SQL Server 2014
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۳/۱۱ ۲۱:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
USE [master]
GO
ALTER DATABASE [testdb2]
ADD FILEGROUP [InMemory_InMemory] CONTAINS MEMORY_OPTIMIZED_DATA
GO
ALTER DATABASE [testdb2]
ADD FILE ( NAME = N'InMemory_InMemory', FILENAME = N'D:\SQL_Data\MSSQL11.MSSQLSERVER\MSSQL\DATA\InMemory_InMemory' )
TO FILEGROUP [InMemory_InMemory]
GO -- It is not a Memory Optimized
CREATE TABLE tblNormal
(
[CustomerID] int NOT NULL PRIMARY KEY NONCLUSTERED,
[Name] nvarchar(250) NOT NULL,
CustomerSince DATETIME not NULL
INDEX [ICustomerSince] NONCLUSTERED
)
-- DURABILITY = SCHEMA_AND_DATA
CREATE TABLE tblMemoryOptimized_Schema_And_Data
(
[CustomerID] INT NOT NULL
PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000000),
[Name] NVARCHAR(250) NOT NULL,
[CustomerSince] DATETIME NOT NULL
INDEX [ICustomerSince] NONCLUSTERED
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA)
-- DURABILITY = SCHEMA_ONLY
CREATE TABLE tblMemoryOptimized_Schema_Only
(
[CustomerID] INT NOT NULL
PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000000),
[Name] NVARCHAR(250) NOT NULL,
[CustomerSince] DATETIME NOT NULL
INDEX [ICustomerSince] NONCLUSTERED
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_ONLY) Indexes on character columns that do not use a *_BIN2 collation are not supported with indexes on memory optimized tables.
bit, tinyint, smallint, int, bigint, money, smallmoney, float, real, datetime, smalldatetime, datetime2, date, time, numberic, decimal, char(n), varchar(n) ,nchar(n), nvarchar(n), sysname, binary(n), varbinary(n), and Uniqueidentifier
set statistics time off
SET STATISTICS IO Off
set nocount on
go
-----------------------------
Print 'insert into tblNormal'
DECLARE @start datetime = getdate()
declare @insertCount int = 100000
declare @startId int = 1
declare @customerID int = @startId
while @customerID < @startId + @insertCount
begin
insert into tblNormal values (@customerID, 'Test', '2013-01-01T00:00:00')
set @customerID +=1
end
Print DATEDIFF(ms,@start,getdate());
go
-----------------------------
Print 'insert into tblMemoryOptimized_Schema_And_Data'
DECLARE @start datetime = getdate()
declare @insertCount int = 100000
declare @startId int = 1
declare @customerID int = @startId
while @customerID < @startId + @insertCount
begin
insert into tblMemoryOptimized_Schema_And_Data values (@customerID, 'Test', '2013-01-01T00:00:00')
set @customerID +=1
end
Print DATEDIFF(ms,@start,getdate());
Go
-----------------------------
Print 'insert into tblMemoryOptimized_Schema_Only'
DECLARE @start datetime = getdate()
declare @insertCount int = 100000
declare @startId int = 1
declare @customerID int = @startId
while @customerID < @startId + @insertCount
begin
insert into tblMemoryOptimized_Schema_Only values (@customerID, 'Test', '2013-01-01T00:00:00')
set @customerID +=1
end
Print DATEDIFF(ms,@start,getdate());
Go insert into tblNormal 36423 insert into tblMemoryOptimized_Schema_And_Data 30516 insert into tblMemoryOptimized_Schema_Only 3176
set nocount on print 'tblNormal' set statistics time on select count(CustomerID) from tblNormal set statistics time off go print 'tblMemoryOptimized_Schema_And_Data' set statistics time on select count(CustomerID) from tblMemoryOptimized_Schema_And_Data set statistics time off go print 'tblMemoryOptimized_Schema_Only' set statistics time on select count(CustomerID) from tblMemoryOptimized_Schema_Only set statistics time off go
tblNormal SQL Server Execution Times: CPU time = 46 ms, elapsed time = 52 ms. tblMemoryOptimized_Schema_And_Data SQL Server Execution Times: CPU time = 32 ms, elapsed time = 33 ms. tblMemoryOptimized_Schema_Only SQL Server Execution Times: CPU time = 31 ms, elapsed time = 30 ms.
set nocount on CHECKPOINT DBCC FREEPROCCACHE() DBCC DROPCLEANBUFFERS
UPDATE STATISTICS tblNormal WITH FULLSCAN, NORECOMPUTE; UPDATE STATISTICS tblMemoryOptimized_Schema_And_Data WITH FULLSCAN, NORECOMPUTE; UPDATE STATISTICS tblMemoryOptimized_Schema_Only WITH FULLSCAN, NORECOMPUTE;
Select POWER(
2,
CEILING( LOG( COUNT( 0)) / LOG( 2)))
AS 'BUCKET_COUNT'
FROM
(SELECT DISTINCT CustomerId
FROM tblMemoryOptimized_Schema_And_Data) T -- Insert 10000 records using 20 threads, Repeat Execution 3 times
-- disk-based
"C:\Program Files\Microsoft Corporation\RMLUtils\ostress.exe" –n20 –r3 -S. -E -dTestdb2 -q -Q"set statistics time off; SET STATISTICS IO Off; set nocount on; DECLARE @start datetime = getdate(); declare @insertCount int = 10000; declare @startId int = 1; while @startId < @insertCount begin insert into tblNormal values ('Test', '2013-01-01T00:00:00') set @startId +=1 end; Print DATEDIFF(ms,@start,getdate());" –oc:\temp\output
-- memory-optimized, tblMemoryOptimized_Schema_And_Data
"C:\Program Files\Microsoft Corporation\RMLUtils\ostress.exe" –n20 –r3 -S. -E -dTestdb2 -q -Q"set statistics time off; SET STATISTICS IO Off; set nocount on; DECLARE @start datetime = getdate(); declare @insertCount int = 10000; declare @startId int = 1; while @startId < @insertCount begin insert into tblMemoryOptimized_Schema_And_Data values ('Test', '2013-01-01T00:00:00') set @startId +=1 end; Print DATEDIFF(ms,@start,getdate());" –oc:\temp\output
-- memory-optimized, tblMemoryOptimized_Schema_Only
"C:\Program Files\Microsoft Corporation\RMLUtils\ostress.exe" –n20 –r3 -S. -E -dTestdb2 -q -Q"set statistics time off; SET STATISTICS IO Off; set nocount on; DECLARE @start datetime = getdate(); declare @insertCount int = 10000; declare @startId int = 1; while @startId < @insertCount begin insert into tblMemoryOptimized_Schema_Only values ('Test', '2013-01-01T00:00:00') set @startId +=1 end; Print DATEDIFF(ms,@start,getdate());" –oc:\temp\output -- It is not Memory Optimized
CREATE TABLE tblNormal
(
[CustomerID] int identity NOT NULL PRIMARY KEY NONCLUSTERED,
[Name] nvarchar(250) NOT NULL,
CustomerSince DATETIME not NULL
INDEX [ICustomerSince] NONCLUSTERED
)
-- DURABILITY = SCHEMA_AND_DATA
CREATE TABLE tblMemoryOptimized_Schema_And_Data
(
[CustomerID] INT identity NOT NULL
PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 131072),
[Name] NVARCHAR(250) NOT NULL,
[CustomerSince] DATETIME NOT NULL
INDEX [ICustomerSince] NONCLUSTERED
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA)
-- DURABILITY = SCHEMA_ONLY
CREATE TABLE tblMemoryOptimized_Schema_Only
(
[CustomerID] INT identity NOT NULL
PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 131072),
[Name] NVARCHAR(250) NOT NULL,
[CustomerSince] DATETIME NOT NULL
INDEX [ICustomerSince] NONCLUSTERED
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_ONLY) select @@VERSION
Microsoft SQL Server 2014 - 12.0.2254.0 (X64) Jul 25 2014 18:52:51 Copyright (c) Microsoft Corporation Developer Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: )