当前位置:   article > 正文

SQLServer2014内存优化表评测_sql2014的优势

sql2014的优势

内存优化表, 以下简称内存表。

SQLServer2014的使用基本要求
1. .Net Framework 3.5 sp1 ,

2. .Net Framework 4.0

3. 硬盘:>=6G

4. 内存:最小值:1G,推荐:>=4G

5. CPU:最小值:x86:1.0 GHZ, x64:1.4 GHZ

6. 操作系统:Win7、WinServer2008 及以上 (WindowsServer2003不支持)

内存表基本要求
1. 64 位 Enterprise、Developer 或 Evaluation 版 SQL Server 2014。(注:即只有64位系统才能使用内存优化表的功能,32位系统能安装SQL Server2014,但无法使用内存表功能)

2. SQL Server 需要有足够的内存来保留内存优化表和索引中的数据。 若要容纳行版本,您应当提供两倍于内存优化表和索引预期大小的内存量。

内存表与磁盘表的DML对比

 

 

有回收线程不断回收同标识的旧行。

内存表比磁盘表快的原理
1. 内存读取比磁盘读取快;

2. 取消了锁,采用行版本机制,读取和更新不冲突。

内存表适合的场合
需要大量的并行操作的表

 

 

具有内存优化对象(包括内存优化数据文件组)的数据库不支持以下 SQL Server 功能。注:支持AlwaysOn

内存表与磁盘表DML性能对比
测试环境:

CPU: Intel Core i3-3240 3.40GHz 

内存:4.00GB(3.86GB可用)

系统类型: Windows Server 2008 R2 Enterprise 64位

 

两次测试取平均值, 测试SQL见后面的附录

 

总结
效率:内存表对比普通的磁盘表, 在增、删、改方面有非常大的优势, 甚至达到了上百倍!但查询方面并没有太大的区别。

 

可行性:内存表的限制比较大,比如数据库用了内存表之后就不能使用复制、镜像、链接服务器, 内存表也不能使用触发器、约束, 每行的字节数不能超过8060字节, 内存表的结构和索引建立之后就不能修改等等。 而且必须配合本地编译的存储过程效率才能提升。仅适用于数据库不需要被限制的功能(复制、镜像等), 而且表的增、删、改非常频繁的情况。

 

SqlServer2014内存表对比oracle 12C的 inmemory 选件, 后者易用性更高( alter table tableName inmemory 即可), 而且其使用对比普通表没有太大区别, 限制很少。

 

SqlServer2014内存表感觉有些鸡肋, 期待下一版的改进。

附录
以下是性能评测SQL:

  1. ------------------------- 1. 建库 -------------------------
  2. USE [master]
  3. GO
  4. if exists(select * from sysdatabases where name='DB_TEST_MEMTB')
  5. DROP DATABASE DB_TEST_MEMTB
  6. go
  7. CREATE DATABASE [DB_TEST_MEMTB]
  8. ON PRIMARY
  9. (
  10. NAME = N'DB_TEST_MEMTB_DATA',
  11. FILENAME = N'e:\db\test\DB_TEST_MEMTB_DATA.mdf',
  12. SIZE = 512000KB,
  13. MAXSIZE = UNLIMITED,
  14. FILEGROWTH = 1024KB
  15. ),
  16. --下面的文件就是数据流文件了
  17. FILEGROUP [MEM_DIR] CONTAINS MEMORY_OPTIMIZED_DATA DEFAULT
  18. (
  19. NAME = N'DB_TEST_MEMTB_DIR',
  20. FILENAME =N'e:\db\test\DB_TEST_MEMTB_DIR',
  21. MAXSIZE = UNLIMITED
  22. )
  23. LOG ON
  24. (
  25. NAME = N'DB_TEST_MEMTB_LOG',
  26. FILENAME = N'e:\db\test\DB_TEST_MEMTB_LOG.ldf',
  27. SIZE = 512000KB,
  28. MAXSIZE = 2048GB,
  29. FILEGROWTH = 1024KB
  30. )
  31. GO
  32. ------------------------- 2. 建表和本地编译存储过程 -------------------------
  33. USE DB_TEST_MEMTB
  34. GO
  35. -- 1. 建立普通磁盘表
  36. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[t_disk]') AND type in (N'U'))
  37. DROP TABLE [dbo].[t_disk]
  38. GO
  39. create table [t_disk]
  40. (
  41. c1 int not null primary key,
  42. c2 nchar(48) not null
  43. )
  44. go
  45. -- 2. 建立内存优化表 (后面的测试不使用本地编译存储过程)
  46. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[t_mem]') AND type in (N'U'))
  47. DROP TABLE [dbo].[t_mem]
  48. GO
  49. create table [t_mem]
  50. (
  51. c1 int not null primary key nonclustered hash with (bucket_count=10000000),
  52. c2 nchar(48) not null
  53. ) with (memory_optimized=on, durability = schema_and_data)
  54. GO
  55. -- 3.0 建立内存优化表 (后面的测试使用本地编译存储过程 NATIVE_COMPILATION)
  56. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[t_mem_nc]') AND type in (N'U'))
  57. DROP TABLE [dbo].t_mem_nc
  58. GO
  59. create table t_mem_nc
  60. (
  61. c1 int not null primary key nonclustered hash with (bucket_count=10000000),
  62. c2 nchar(48) not null
  63. ) with (memory_optimized=on, durability = schema_and_data)
  64. GO
  65. -- 3.1 本地编译存储过程_insert
  66. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Proc_t_mem_nc_Insert]') AND type in (N'P', N'PC'))
  67. DROP PROCEDURE [dbo].[Proc_t_mem_nc_Insert]
  68. GO
  69. CREATE PROCEDURE [Proc_t_mem_nc_Insert]
  70. @rowcount int,
  71. @c nchar(48)
  72. WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
  73. AS
  74. BEGIN ATOMIC
  75. WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
  76. declare @i int = 1
  77. while @i <= @rowcount
  78. begin
  79. INSERT INTO [dbo].t_mem_nc values (@i, @c)
  80. set @i += 1
  81. end
  82. END
  83. GO
  84. -- 3.2 本地编译存储过程_delete
  85. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Proc_t_mem_nc_delete]') AND type in (N'P', N'PC'))
  86. DROP PROCEDURE [dbo].[Proc_t_mem_nc_delete]
  87. GO
  88. CREATE PROCEDURE [Proc_t_mem_nc_delete]
  89. @rowcount int
  90. WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
  91. AS
  92. BEGIN ATOMIC
  93. WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
  94. DECLARE @i INT = 1
  95. while @i<=@rowcount
  96. begin
  97. DELETE FROM dbo.t_mem_nc WHERE c1=@i
  98. set @i += 1
  99. end
  100. END
  101. GO
  102. -- 3.3 本地编译存储过程_update
  103. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Proc_t_mem_nc_update]') AND type in (N'P', N'PC'))
  104. DROP PROCEDURE [dbo].[Proc_t_mem_nc_update]
  105. GO
  106. CREATE PROCEDURE [Proc_t_mem_nc_update]
  107. @rowcount INT,
  108. @c nchar(48)
  109. WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
  110. AS
  111. BEGIN ATOMIC
  112. WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
  113. DECLARE @i INT = 1
  114. while @i<=@rowcount
  115. begin
  116. UPDATE dbo.t_mem_nc SET c2=@c WHERE c1=@i
  117. set @i += 1
  118. end
  119. END
  120. GO
  121. -- 3.4 本地编译存储过程_select
  122. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Proc_t_mem_nc_select]') AND type in (N'P', N'PC'))
  123. DROP PROCEDURE [dbo].[Proc_t_mem_nc_select]
  124. GO
  125. CREATE PROCEDURE [Proc_t_mem_nc_select]
  126. WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
  127. AS
  128. BEGIN ATOMIC
  129. WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english')
  130. SELECT c1,c2 FROM dbo.t_mem_nc
  131. END
  132. GO
  133. ------------------------- 3. 效率评测 -------------------------
  134. DECLARE @i INT=1,@iMax INT = 1000000 --最大一百万条记录
  135. DECLARE @v NCHAR(48)='123456789012345678901234567890123456789012345678'
  136. DECLARE @t DATETIME2 = sysdatetime()
  137. --3.1 insert
  138. --
  139. set nocount on
  140. while @i<=@iMax
  141. begin
  142. insert into t_disk (c1,c2) values(@i, @v)
  143. set @i+=1
  144. end
  145. select 'insert (t_disk): '+ convert(varchar(10), datediff(ms, @t, sysdatetime()))
  146. --
  147. set @i=1
  148. set @t=SYSDATETIME()
  149. while @i<=@iMax
  150. begin
  151. insert into t_mem (c1,c2) values(@i, @v)
  152. set @i+=1
  153. end
  154. select 'insert (t_mem): '+ convert(varchar(10), datediff(ms, @t, sysdatetime()))
  155. --
  156. set @t=SYSDATETIME()
  157. exec [Proc_t_mem_nc_Insert]
  158. @rowcount=@iMax,
  159. @c=@v
  160. select 'insert (t_mem_nc): '+ convert(varchar(10), datediff(ms, @t, sysdatetime()))
  161. --结果:
  162. --insert (t_disk): 242111
  163. --insert (t_mem): 221358
  164. --insert (t_mem_nc): 2147
  165. --insert (t_disk): 243174
  166. --insert (t_mem): 223465
  167. --insert (t_mem_nc): 2214
  168. --3.2 update
  169. --时间较长,故分段执行另设变量
  170. DECLARE @u INT=1,@uMax INT = 1000000 --最大一百万条记录
  171. DECLARE @uv NCHAR(48)='1234567890123456789012345678901234567890abcdefgh'
  172. DECLARE @ut DATETIME2 = sysdatetime()
  173. set nocount on
  174. while @u<=@uMax
  175. begin
  176. update t_disk set c2=@uv where c1=@u
  177. set @u+=1
  178. end
  179. select 'update (t_disk): '+ convert(varchar(10), datediff(ms, @ut, sysdatetime()))
  180. --
  181. set @u=1
  182. set @ut=SYSDATETIME()
  183. while @u<=@uMax
  184. begin
  185. update t_mem set c2=@uv where c1=@u
  186. set @u+=1
  187. end
  188. select 'update (t_mem): '+ convert(varchar(10), datediff(ms, @ut, sysdatetime()))
  189. --
  190. set @ut=SYSDATETIME()
  191. exec [Proc_t_mem_nc_Update]
  192. @rowcount=@uMax,
  193. @c=@uv
  194. select 'update (t_mem_nc): '+ convert(varchar(10), datediff(ms, @ut, sysdatetime()))
  195. --update (t_disk): 199369
  196. --update (t_mem): 368297
  197. --update (t_mem_nc): 3715
  198. --update (t_disk): 203251
  199. --update (t_mem): 355356
  200. --update (t_mem_nc): 3732
  201. --3.3 select
  202. DECLARE @st DATETIME2 = sysdatetime()
  203. set nocount on
  204. --
  205. select c1,c2 from t_disk
  206. select 'select (t_disk): '+ convert(varchar(10), datediff(ms, @st, sysdatetime()))
  207. set @st=SYSDATETIME()
  208. select c1,c2 from t_mem
  209. select 'select (t_mem): '+ convert(varchar(10), datediff(ms, @st, sysdatetime()))
  210. set @st=SYSDATETIME()
  211. exec Proc_t_mem_nc_select
  212. select 'select (t_mem_nc): '+ convert(varchar(10), datediff(ms, @st, sysdatetime()))
  213. --select (t_disk): 8934
  214. --select (t_mem): 9278
  215. --select (t_mem_nc): 8889
  216. --select (t_disk): 8861
  217. --select (t_mem): 9978
  218. --select (t_mem_nc): 9108
  219. --3.4 delete
  220. --时间较长,故分段执行另设变量
  221. DECLARE @d INT=1,@dMax INT = 1000000 --最大一百万条记录
  222. DECLARE @dt DATETIME2 = sysdatetime()
  223. set nocount on
  224. while @d<=@dMax
  225. begin
  226. delete from t_disk where c1=@d
  227. set @d+=1
  228. end
  229. select 'delete (t_disk): '+ convert(varchar(10), datediff(ms, @dt, sysdatetime()))
  230. --
  231. set @d=1
  232. set @dt=SYSDATETIME()
  233. while @d<=@dMax
  234. begin
  235. delete from t_mem where c1=@d
  236. set @d+=1
  237. end
  238. select 'delete (t_mem): '+ convert(varchar(10), datediff(ms, @dt, sysdatetime()))
  239. --
  240. set @dt=SYSDATETIME()
  241. exec [dbo].[Proc_t_mem_nc_delete] @rowcount=@dMax
  242. select 'delete (t_mem_nc): '+ convert(varchar(10), datediff(ms, @dt, sysdatetime()))
  243. --delete (t_disk): 199438
  244. --delete (t_mem): 342959
  245. --delete (t_mem_nc): 928
  246. --delete (t_disk): 199637
  247. --delete (t_mem): 341771
  248. --delete (t_mem_nc): 803

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/201332
推荐阅读
相关标签
  

闽ICP备14008679号