您的位置:首页 > 数据库

SQL server 内存数据库

2016-05-04 19:12 525 查看
下面是一个SQLserver 内存数据库的简单例子

注意:C盘根目录需建立一个data目录

--1.创建库 内存表
CREATE DATABASE imoltp
GO

ALTER DATABASE imoltp ADD FILEGROUP imoltp_mod CONTAINS MEMORY_OPTIMIZED_DATA ALTER DATABASE imoltp ADD FILE (name='imoltp_mod1', filename='c:\data\imoltp_mod1') TO FILEGROUP imoltp_mod ALTER DATABASE imoltp SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT=ON
GO

USE imoltp
GO
--2 create a durable (data will be persisted) memory-optimized table -- two of the columns are indexed

CREATE TABLE dbo.ShoppingCart
( ShoppingCartId INT IDENTITY(1,1) PRIMARY KEY NONCLUSTERED,
UserId INT NOT NULL INDEX ix_UserId NONCLUSTERED HASH WITH (BUCKET_COUNT=1000000),
CreatedDate DATETIME2 NOT NULL,
TotalPrice MONEY
)
WITH (MEMORY_OPTIMIZED=ON)
GO

CREATE TABLE dbo.UserSession ( SessionId INT IDENTITY(1,1) PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT=400000), UserId int NOT NULL, CreatedDate DATETIME2 NOT NULL, ShoppingCartId INT, INDEX ix_UserId NONCLUSTERED HASH (UserId) WITH (BUCKET_COUNT=400000) )
WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_ONLY)
GO

-- 3 insert data into the tables

INSERT dbo.UserSession (UserId, CreatedDate, ShoppingCartId) VALUES (342, SYSDATETIME(), 4)
INSERT dbo.UserSession (UserId, CreatedDate, ShoppingCartId) VALUES (65, SYSDATETIME(), NULL)
INSERT dbo.UserSession (UserId, CreatedDate, ShoppingCartId) VALUES (8798, SYSDATETIME(), 1)
INSERT dbo.UserSession (UserId, CreatedDate, ShoppingCartId) VALUES (80, SYSDATETIME(), NULL)
INSERT dbo.UserSession (UserId, CreatedDate, ShoppingCartId) VALUES (4321, SYSDATETIME(), NULL)
INSERT dbo.UserSession (UserId, CreatedDate, ShoppingCartId) VALUES (8578, SYSDATETIME(), NULL)

INSERT dbo.ShoppingCart (UserId, CreatedDate, TotalPrice) values (8798, SYSDATETIME(), NULL)
GO
-- 4 创建内存表存储过程

if exists( select 1 from sysobjects where name = 'usp_AssignCart' )
begin
drop procedure usp_AssignCart
end
GO
-- natively compiled stored procedure for assigning a shopping cart to a session
CREATE PROCEDURE dbo.usp_AssignCart
@SessionId int,
@test int output
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
AS
BEGIN ATOMIC WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
DECLARE @UserId INT, @ShoppingCartId INT
SELECT @UserId=UserId,
@ShoppingCartId=ShoppingCartId
FROM dbo.UserSession
WHERE SessionId=@SessionId
IF @UserId IS NULL THROW 51000, N'The session or shopping cart does not exist.', 1

UPDATE dbo.UserSession SET ShoppingCartId=@ShoppingCartId WHERE SessionId=@SessionId
select @test = @ShoppingCartId
END
GO

--2.老存储过程中调用,内存表存储过程
use mydb
go

print 'Create procedure sp_do_action_test... '
go

if exists( select 1 from sysobjects where name = 'sp_do_action_test' )
begin
drop procedure sp_do_action_test
end
go

create procedure sp_do_action_test  @l_action_in int
-- with encryption
as
set rowcount 0
set transaction isolation level read uncommitted
-- 调用内存表存储过程 begin
if @l_action_in = 1
begin
exec imoltp..usp_AssignCart 1,@error_no output
select @error_info = '[500901]l_action_in[' + convert(varchar, @l_action_in) +']测试调用内存表存储过程'
end
-- 调用内存表存储过程 end
set transaction isolation level read committed
select @error_no as 'error_no', @error_info as 'error_info'
return @error_no
go

--老存储过程测试
exec mydb..sp_do_action_test 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: