SQL server 内存数据库
2016-05-04 19:12
525 查看
下面是一个SQLserver 内存数据库的简单例子
注意:C盘根目录需建立一个data目录
注意: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
相关文章推荐
- redis学习笔记(4)---跳表zskiplist
- sql中on与where的区别
- Oracle 故障处理总结
- 搭建ORACLE高可用 高性能 高扩展的 MMM_APE 架构
- Activiti-Explorer中设计的流程图保存到哪里去了?
- plsql常用方法-转
- 数据库事务隔离级别-- 脏读、幻读、不可重复读(清晰解释)
- 企业级-Mysql双主互备高可用负载均衡架构(基于GTID主从复制模式)
- Mysql之建立主从同步
- Mysql之源码安装
- mongodb 客户端推荐
- mysql慢查询的配置及mysql进程查看
- SQLite + FMDB 性能笔记
- Oracle Crystal ball 11.1.* 注册码
- mysql数据库的读写分离
- SharePoint 2013 数据库中手动更新用户信息
- SQLite的优化总结
- 易生活(三)-web---商品排行和分页综合处理、sql占位符异常处理
- 第一章 数据库语言基础
- sql语句的预处理mysqli pdo绑定参数