1. 程式人生 > >SQL server 記憶體資料庫

SQL server 記憶體資料庫

下面是一個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 
[email protected]
IF @UserId IS NULL THROW 51000, N'The session or shopping cart does not exist.', 1 UPDATE dbo.UserSession SET [email protected] WHERE [email protected] 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