6,129
社区成员
发帖
与我相关
我的任务
分享
USE [master]
GO
CREATE DATABASE [DB_TEST_MEMTB]
ON PRIMARY
( NAME = N'DB_TEST_MEMTB_DATA', FILENAME = N'D:\DB_TEST_MEMTB_DATA.mdf' , SIZE = 512000KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ),
--下面的文件就是数据流文件了
FILEGROUP [MEM_DIR] CONTAINS MEMORY_OPTIMIZED_DATA DEFAULT
( NAME = N'DB_TEST_MEMTB_DIR', FILENAME = N'D:\DB_TEST_MEMTB_DIR' , MAXSIZE = UNLIMITED)
LOG ON
( NAME = N'DB_TEST_MEMTB_LOG', FILENAME = N'D:\DB_TEST_MEMTB_LOG.' , SIZE = 512000KB , MAXSIZE = 2048GB , FILEGROWTH = 1024KB)
GO
USE DB_TEST_MEMTB
GO
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'xx')
DROP PROCEDURE xx
GO
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'sql')
DROP TABLE sql
GO
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'hash')
DROP TABLE hash
GO
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'hash1')
DROP TABLE hash1
GO
create table [sql]
(
c1 int not null primary key,
c2 nchar(48) not null
)
go
create table [hash]
(
c1 int not null primary key nonclustered hash with (bucket_count=1000000),
c2 nchar(48) not null
) with (memory_optimized=on, durability = schema_and_data)
go
create table [hash1]
(
c1 int not null primary key nonclustered hash with (bucket_count=1000000),
c2 nchar(48) not null
) with (memory_optimized=on, durability = schema_and_data)
go
CREATE PROCEDURE xx
@rowcount int,
@c nchar(48)
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
AS
BEGIN ATOMIC
WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
declare @i int = 1
while @i <= @rowcount
begin
INSERT INTO [dbo].[hash1] values (@i, @c)
set @i += 1
end
END
GO
set statistics time off
set nocount on
-- inserts - 1 at a time
declare @starttime datetime2 = sysdatetime(),
@timems int
declare @i int = 1
declare @rowcount int = 100000
declare @c nchar(48) = N'12345678901234567890123456789012345678'
-----------------------------
--- disk-based table and interpreted Transact-SQL
-----------------------------
begin tran
while @i <= @rowcount
begin
insert into [sql] values (@i, @c)
set @i += 1
end
commit
set @timems = datediff(ms, @starttime, sysdatetime())
select 'Disk-based table and interpreted Transact-SQL: ' + cast(@timems as varchar(10)) + ' ms'
-----------------------------
--- Interop Hash
-----------------------------
set @i = 1
set @starttime = sysdatetime()
begin tran
while @i <= @rowcount
begin
insert into [hash] values (@i, @c)
set @i += 1
end
commit
set @timems = datediff(ms, @starttime, sysdatetime())
select ' memory-optimized table w/ hash index and interpreted Transact-SQL: ' + cast(@timems as varchar(10)) + ' ms'
-----------------------------
--- Compiled Hash
-----------------------------
set @starttime = sysdatetime()
exec xx @rowcount, @c
set @timems = datediff(ms, @starttime, sysdatetime())
select 'memory-optimized table w/hash index and native SP:' + cast(@timems as varchar(10)) + ' ms'
declare @t datetime2
set @t=sysdatetime()
select * from sql
print 'sql: ' + convert(varchar(20) , datediff(ms, @t, sysdatetime()))
set @t=sysdatetime()
select * from hash
print 'hash: ' + convert(varchar(20) , datediff(ms, @t, sysdatetime()))
set @t=sysdatetime()
select * from hash1
print 'hash1: ' + convert(varchar(20) ,datediff(ms, @t, sysdatetime()))
--下面是三次测试的结果
--sql: 1434
--hash: 1434
--hash1: 1377
--sql: 1366
--hash: 1443
--hash1: 1405
--sql: 1384
--hash: 1479
--hash1: 1445