SQL Server 2014引入了内存数据库,这是个非常好的功能。数据可以直接在内存中,这样可以直接对内存进行操作,性能有很大的提高。
今天想到一个问题,如果表增长的大了,而数据库内存不够会出现什么样的情况。因为微软的MSDNS上是这样写的额:
A computer with enough memory to hold the data in memory-optimized tables. Memory-optimized data must not use more than 80% of the maximum server memory.
也就是不要超过整个INSTANCE内存的80%,但是数据库的增长有时候是无法预料的啊。下面开始我的测试:
--Create database with memory-optimizeddata filegroup
CREATE DATABASE Hekaton_Demo
ON
PRIMARY(NAME = [hekaton_demo_data],
FILENAME = 'C:\DATA\hekaton_demo_data.mdf',size=500MB)
, FILEGROUP [hekaton_demo_fg] CONTAINSMEMORY_OPTIMIZED_DATA(
NAME = [hekaton_demo_dir],
FILENAME = 'C:\DATA\hekaton_demo_dir')
LOG ON (name = [hekaton_demo_log], Filename='C:\DATA\hekaton_demo_log.ldf',size=500MB)
COLLATE Latin1_General_100_BIN2;
GO
USE Hekaton_Demo;
GO
CREATE TABLE Destination1
(
--See the section on bucket_count for more details on setting the bucket count.
col1 INT NOT NULL PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 1000000),
col2 varchar(1000) NOT NULL,
col3 varchar(1000) NOT NULL
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA)
GO
尝试把BUCKET_COUNT再乘以100倍创建的时候就报错了:
Msg 701, Level 17, State137, Line 37
There is insufficientsystem memory in resource pool 'default' to run this query.
--尝试插入大量数据:
DECLARE @INT INT
SET @INT = 1
WHILE @INT < 1000000
BEGIN
INSERT INTO Destination1 VALUES(@INT,REPLICATE('A',1000),REPLICATE('A',1000))
SET @INT = @INT +1
END
结果执行了一段时间之后数据库连接直接断开了,SSMS看到的错误信息:
Timeout expired. The timeout period elapsed prior tocompletion of the operation or the server is not responding. (.Net SqlClientData Provider)
数据库错误日志:
Msg 701, Level 17, State137, Line 37
There is insufficientsystem memory in resource pool 'default' to run this query.
重启一次失败:
13-06-30 18:23:55.23Logon Error: 17188, Severity: 16,State: 1.
2013-06-30 18:23:55.23Logon SQL Server cannot accept newconnections, because it is shutting down. The connection has been closed.[CLIENT: <local machine>]
再次重启:
2013-06-30 18:30:40.98 spid23s Error: 41316, Severity: 23, State: 4.
2013-06-30 18:30:40.98 spid23s Restore operation failed for database'Hekaton_Demo'.
2013-06-30 18:13:18.08 spid38s Error: 802, Severity: 17, State: 0.
2013-06-30 18:13:18.08 spid38s There is insufficient memory available inthe buffer pool.
还是失败,没办法直接删除数据库重启成功。
不知道SQL Server是如何在创建表的时候计算内存的,对于变量如何计算呢?因为值根本是不确定的。如果开始创建表不报错在后面的时候出现这种问题那就比性能问题更严重了。
再去找找文档看看微软是如何解释的。