-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathkey-value.sql
125 lines (106 loc) · 2.87 KB
/
key-value.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Make sure you are using a Business Critical or Premium Service Tier
*/
select * from sys.[database_service_objectives]
go
/*
Create In-Memory table to act as Key-Value store
*/
if (schema_id('cache') is null)
exec('create schema [cache];')
go
create table [cache].[MemoryStore]
(
[key] bigint not null,
[value] nvarchar(max) null,
index IX_Hash_Key unique hash ([key]) with (bucket_count= 100000)
) with (memory_optimized = on, durability = schema_only);
go
/*
Test Key-Value GET/PUT performances
*/
create or alter procedure cache.[TestPerformance]
with native_compilation, schemabinding
as
begin atomic with (transaction isolation level = snapshot, language = N'us_english')
declare @i int = 0;
declare @o int = 0;
while (@i < 100000)
begin
declare @r int = cast(rand() * 100000 as int)
declare @v nvarchar(max) = (select top(1) [value] from [cache].[MemoryStore] where [key]=@r);
set @o += 1;
if (@v is not null) begin
declare @c int = cast(json_value(@v, '$.counter') as int) + 1;
update [cache].[MemoryStore] set [value] = json_modify(@v, '$.counter', @c) where [key] = @r
set @o += 1;
end else begin
declare @value nvarchar(max) = '{"value": "' + cast(sysdatetime() as nvarchar(max)) + '", "counter": 1}'
insert into [cache].[MemoryStore] values (@r, @value)
set @o += 1;
end
set @i += 1;
end
select total_operations = @o;
end
go
/*
Run Performance Test
*/
declare @s datetime2, @e datetime2;
set @s = sysutcdatetime();
exec cache.[TestPerformance]
set @e = sysutcdatetime();
select datediff(millisecond, @s, @e)
go
/*
See data
*/
select top 100 * from [cache].[MemoryStore]
go
/*
Sample "GET" implementation
*/
create or alter procedure cache.[Get]
@key int
with native_compilation, schemabinding
as
begin atomic with (transaction isolation level = snapshot, language = N'us_english')
select top (1) [value] from [cache].[MemoryStore] where [key]=@key;
end
go
/*
Sample "PUT" implementation
*/
create or alter procedure cache.[Put]
@key int,
@value nvarchar(max)
with native_compilation, schemabinding
as
begin atomic with (transaction isolation level = snapshot, language = N'us_english')
if (isjson(@value) != 1) throw 50000, '@value is not a JSON object', 16
declare @f int = (select top(1) [key] from [cache].[MemoryStore] where [key]=@key);
if (@f is not null) begin
update [cache].[MemoryStore] set [value] = @value where [key] = @key
end else begin
insert into [cache].[MemoryStore] values (@key, @value)
end
end
go
/*
Sample GET/PUT usage
*/
declare @json as nvarchar(max) = N'{"value": "' + cast(sysdatetime() as nvarchar(max)) + '", "counter": 1}';
exec [cache].[Put] 12345, @json;
go
exec [cache].[Get] 12345;
go
/*
Cleanup
*/
drop procedure [cache].[Get];
drop procedure [cache].[Put];
drop procedure [cache].[TestPerformance];
drop table [cache].[MemoryStore];
drop schema [cache];
go