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是如何在建立表的時候計算記憶體的,對於變數如何計算呢?因為值根本是不確定的。如果開始建立表不報錯在後面的時候出現這種問題那就比效能問題更嚴重了。
再去找找文檔看看微軟是如何解釋的。