First steps with Extreme Transaction Processing – Hekaton

(Be sure to checkout the FREE SQLpassion Performance Tuning Training Plan - you get a weekly email packed with all the essential knowledge you need to know about performance tuning on SQL Server.)

A few hours ago Microsoft released the first public CTP of SQL Server 2014. The download and the installation went very smooth, the only new restriction is that you are not allowed to install the CTP 1 version in parallel with any other SQL Server version. Aaron Bertrand has blogged about that new behavior.

In this blog posting I want to give a brief overview how to make your first steps with Extreme Transaction Processing (XTP) that was formerly known as Hekaton. If you want to have a good overview about XTP, I recommend Kalen Delaney’s whitepaper about it, and the research whitepaper titled High-Performance Concurrency Control Mechanisms for Main-Memory Databases (can be downloaded here) that was released by Microsoft Research.

All the versioning history that XTP maintains transparently for you in the background is stored in a FILESTREAM filegroup with your SQL Server database. So the first thing that you have to do, when you want to work with XTP, is to add a new FILESTREAM Filegroup to the corresponding database. That new file group must be also marked with the property MEMORY_OPTIMIZED_DATA

After you have created the new FILESTREAM file group, you have to add a new file to the file group.

After we have prepared our database for XTP, we can finally add our Memory Optimized Table – that’s the name how SQL Server refers to XTP tables:

From the above screenshot you expect a very cool table wizard, but the only thing that we are currently getting is a T-SQL script template – nothing more. That reminds me back to my Service Broker days, and we still have no wizard for Service Broker in SQL Server 2014 – 8 years are just gone without any improvements in this area. Hopefully Microsoft invests a little bit more into XTP… Here is the necessary T-SQL script to create a XTP table:

Every table in XTP needs a primary key constraint that must be a Non-Clustered Hash Index. Clustered Hash Indexes are currently not support. You also have to specify the bucket count with the BUCKET_COUNT clause. And finally you have to mark your table as MEMORY_OPTIMIZED. Congratulations, you have created your first Memory Optimized table – it wasn’t that hard!
Working with the new table is also very easy. Let’s insert a simple record:

But the real power of XTP is in combination with concurrent users, because there is now no Locking/Blocking/Latching anymore. Even Exclusive Locks (X) are just gone. Normally when you are running the above INSERT statement on a “normal” table, you have an IX lock on the table, and on the page, and on the record itself an X lock. But with XTP all those locks are just gone. See the following query:

The following screenshot shows the output from sys.dm_tran_locks. As you can see there is only a Schema Stability Lock (Sch-S) on the table itself, but the IX and X locks are just gone – very nice:

When you are running the above transactions without committing it immediately, you can also retrieve the data without blocking from a different session through a simple SELECT statement. SQL Server uses an Index Scan (NonClusteredHash) operator in the Execution Plan:

Before XTP this behavior was only possible by enabling Optimistic Concurrency within the database – Read Committed Snapshot Isolation or Snapshot Isolation that was introduced back in SQL Server 2005.
Let’s try to run an UPDATE statement:

When we look at the Update Execution Plan, there is nothing special here expect the Index Seek (NonClusteredHash) operator. So XTP is able to Scan and Seek Hash Indexes. Let’s try to make the UPDATE statement in an explicit transaction:

In this case, SQL Server gives us the following error message:

Accessing memory optimized tables using the READ COMMITTED isolation level is supported only for autocommit transactions. It is not supported for explicit or implicit transactions. Provide a supported isolation level for the memory optimized table using a table hint, such as WITH (SNAPSHOT).

So when you are using explicit transactions, we have to hint SQL Server. But WITH (SNAPSHOT) isn’t the same as the Transaction Isolation Level Snapshot, because when I’m changing the Isolation Level to Snapshot, and trying to rerun the transaction, I’m getting the following error message:

Msg 41332, Level 16, State 0, Line 3
Memory optimized tables and natively compiled stored procedures cannot be accessed or created when the session TRANSACTION ISOLATION LEVEL is set to SNAPSHOT.

So let’s let’s add a query hint to the query itself:

Now the transaction commits, and sys.dm_tran_locks again only shows the Sch-S lock. The next thing that I wanted to try is to run 2 UPDATE statements in parallel by not committing the 1st transaction. So let’s just run the following query in 2 different sessions, and make sure you are not committing both transaction. Without XTP, the 2nd transaction would block, because of the X lock that is held by the 1st session:

Unfortunately SQL Server gives us in the 2nd transaction the following error message:

Msg 41302, Level 16, State 110, Line 3
The current transaction attempted to update a record that has been updated since this transaction started. The transaction was aborted.
Msg 3998, Level 16, State 1, Line 1
Uncommittable transaction is detected at the end of the batch. The transaction is rolled back.
The statement has been terminated.

We were just running into an Update Conflict, and SQL Server has rolled back the 2nd transaction. I have not expected that, but I have to think a little bit more about it.
The next thing that XTP provides you in the CTP1 version are so-called Natively Compiled Stored Procedures:

Again, there is no wizard, just a simple T-SQL script template that you can use. Native Compilation means that SQL Server is compiling the whole stored procedure down to C/C++ code in the background – the performance will be awesome, because we are now executing directly native code within SQL Server. The following script shows how a simple XTP stored procedure looks like.

There are a lot of prerequisites that must be met:

  • The Stored Procedure must be created with SCHEMABINDING and EXECUTE AS
  • The Stored Procedure must be marked with NATIVE_COMPILATION
  • You have to specify the ATOMIC block, where you set the Transaction Isolation Level, and the language

When you have created the stored procedure, you can simply execute it:

Because you are now dealing with native code, there is no Execution Plan anymore! Have fun with performance tuning… An ALTER PROCEDURE is also not supported (doesn’t make sense, because of the native generated code), means you have to DROP and CREATE the stored procedure again, if you want to make changes.

This was a short walkthrough of XTP that I have encountered in the 1st hour after installing the CTP1 version of SQL Server 2014. More information and additional content will definitely come. You can download the whole T-SQL code that I have shown in this blog posting here.

Call to action: Download the CTP1 of SQL Server 2014, play around with XTP and all the other nice cool features. Please feel free to leave a comment about your experience and your opinions about Extreme Transactional Processing.

Thanks for reading


Do you like this post? Why not share it with the SQL Server community?