Tag: InMemory OLTP

In-Memory OLTP: Compatibility Check for Datatypes in SQL Server

Hope you enjoyed my earlier posts on
In-Memory OLTP and Memory Optimized data filegroup in SQL Server 2014
In-Memory OLTP: Compatibility Check for SQL Server and Database

Here, as continuation of compatibility check, we are moving further one more step on Datatype compatibility.

The below script is primarily focusing on major limitations like unsupported datatypes,computed columns,sparse columns, unavailability of primary keys and presence of clustered index. The script has been developed for SQL Server 2014 version(if there are any of the above limitations removed in later versions, you may need to change the script accordingly.).

Note: Please change the result view as “Results To Text(Ctrl + T)” for better readability of the result.


PRINT '*******************************************************'
PRINT 'DATATYPE COMPATIBILITY CHECK'
PRINT '*******************************************************'

PRINT 'THE BELOW TABLE(S) ARE NOT COMPATIBLE TO IN-MEMORY AS IS DUE TO UNSUPPORTED DATATYPES. '
SELECT NAME FROM SYS.TABLES WHERE NAME IN
	(SELECT OBJECT_NAME(OBJECT_ID) FROM SYS.COLUMNS WHERE 
	(TYPE_NAME(USER_TYPE_ID) IN 
            ('IMAGE','TEXT','NTEXT','DATETIMEOFFSET','XML','GEOMETRY',
             'DATETIMEOFFSET','GEOGRAPHY','TIMESTAMP','SQL_VARIANT'))
	OR
	(TYPE_NAME(user_type_id) IN 
            ('VARCHAR','CHAR','NVARCHAR','NCHAR','VARBINARY') AND MAX_LENGTH = -1)
	OR
	/*FILESTREAM can only be enabled with VARBINARY(MAX), however, the defined form of datatype is
		not supported in In-Memory Tables, provided the below for FILESTREAM/FILETABLE for completeness,*/
	(TYPE_NAME(USER_TYPE_ID) IN ('VARBINARY') AND MAX_LENGTH = -1 AND (IS_FILESTREAM =1 OR IS_FILETABLE = 1))
	)

--COMPUTED COLUMNS
PRINT 'THE BELOW TABLE(S) ARE NOT COMPATIBLE TO IN-MEMORY AS IS DUE TO UNSUPPORTED COMPUTED COLUMNS'
SELECT NAME FROM SYS.TABLES WHERE NAME IN
	(SELECT OBJECT_NAME(OBJECT_ID) FROM SYS.COLUMNS WHERE IS_COMPUTED =1)

--SPARSE COLUMNS
PRINT 'THE BELOW TABLE(S) ARE NOT COMPATIBLE TO IN-MEMORY AS THEY HAVE SPARSE COLUMNS'
SELECT NAME FROM SYS.TABLES WHERE NAME IN
	(SELECT OBJECT_NAME(OBJECT_ID) FROM SYS.COLUMNS WHERE is_sparse =1) 

-- ABSENCE OF PRIMARY KEY
PRINT 'THE BELOW TABLE(S) ARE NOT COMPATIBLE TO IN-MEMORY AS THEY DO NOT HAVE PRIMARY KEYS'
SELECT NAME FROM SYS.TABLES WHERE NAME NOT IN 
	(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS  WHERE CONSTRAINT_TYPE ='PRIMARY KEY')

--PRESENCE OF CLUSTERED INDEX
PRINT 'THE BELOW TABLE(S) ARE NOT COMPATIBLE TO IN-MEMORY AS THEY HAVE CLUSTERED INDEX'
SELECT B.NAME FROM SYS.INDEXES A
INNER JOIN SYS.TABLES B ON A.OBJECT_ID =B.OBJECT_ID WHERE A.TYPE_DESC = 'CLUSTERED'
Advertisements

In-Memory OLTP and Memory Optimized data filegroup in SQL Server 2014

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.

Syntax:


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.