Today, Dussehra Or Vijayadasami is considered to be the most auspicious day to start learning something new.Let us take this opportunity to start learning about InMemory OLTP in SQL Server.
In-Memory OLTP is one of the most fascinating features in SQL Server 2014.
First off, if you are going to work with InMemory OLTP, then you need to know about Memory optimized data Filegroup. In short, for any database, where memory optimized table(In Memory tables) needs to be created, there should be provided with separate FileGroup called MEMORY_OPTIMIZED_DATA. This filegroup is extremely important for the feature to store the Checkpoint information and delta files for SQL Server during the disaster(Any restart/crash).Let us look at the syntax to create the database and filegroup.
CREATE DATABASE [TestDB] ON PRIMARY ( NAME = N'TestDB', FILENAME = N'D:\data\TestDB.mdf' ) LOG ON ( NAME = N'ManTest_log', FILENAME = N'D:\data\TestDB_log.ldf') ALTER DATABASE [TestDB] ADD FILEGROUP TestDB_MO CONTAINS MEMORY_OPTIMIZED_DATA ALTER DATABASE TestDB ADD FILE ( NAME = TestDB_MO , FILENAME = N'D:\data\Datasample_database_1') TO FILEGROUP TestDB_MO
If you have not created memory optimized data file group and you try to create a memory optimized table, you may be facing the below error message.
Msg 41337, Level 16, State 0, Line 9 Cannot create memory optimized tables in a database that does not have an online and non-empty MEMORY_OPTIMIZED_DATA filegroup.
Another important point is you can only create MEMORY_OPTIMIZED_DATA filegroup only in 64 bit SQL Server. Otherwise, you may end up with the below error message
Msg 41356, Level 15, State 1, Line 1 Filegroups with MEMORY_OPTIMIZED_DATA can only be created in 64-bit installations of SQL Server.
As the error message clearly suggests In-Memory OLTP will only support in 64 bit SQL Server.
You can use @@VERSION global variable to know about your SQL Server bit version.
Once you created the database successfully, you will be able to create Memory optimized table as below:
CREATE TABLE dbo.SampleTable ( id INT NOT NULL, item_id INT NOT NULL, CONSTRAINT PK_SampleTable PRIMARY KEY NONCLUSTERED (id,item_id) ) WITH (MEMORY_OPTIMIZED=ON)
Now, we covered very basic requirements for In Memory OLTP concept in SQL Server.To summarize the above
1. make sure you have installed SQL Server 2014 with 64 bit
2. create a memory optimized data filegroup for the database
Next post, We will cover more on this topic…until then happy Dussehra.