{"id":1433,"date":"2014-02-21T09:30:00","date_gmt":"2014-02-21T17:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/dataplatforminsider\/2014\/02\/21\/using-natively-compiled-stored-procedures-in-sql-server-2014\/"},"modified":"2024-01-22T22:49:01","modified_gmt":"2024-01-23T06:49:01","slug":"using-natively-compiled-stored-procedures-in-sql-server-2014","status":"publish","type":"post","link":"https:\/\/www.microsoft.com\/en-us\/sql-server\/blog\/2014\/02\/21\/using-natively-compiled-stored-procedures-in-sql-server-2014\/","title":{"rendered":"Using Natively Compiled Stored Procedures in SQL Server 2014"},"content":{"rendered":"
The new In-Memory OLTP feature in SQL Server 2014 greatly optimizes the performance of certain OLTP applications. By using the new memory-optimized tables you can speed up data access, in particular in concurrency situations, due to the lock- and latch-free architecture of the In-Memory OLTP Engine. This means that applications which suffer from a lot of contention between concurrent transactions can greatly benefit from just migrating your hot tables to memory-optimized.<\/p>\n
The other part of the equation are the new natively compiled stored procedures, which allow you to speed up query processing and business logic execution. These native procs are T-SQL stored procedures that are compiled to native code, in the form of DLLs, which are linked to the SQL Server process, for very efficient execution. The efficiency in natively compiled procs comes from savings in the execution path by baking the operations into machine code that can interact very efficiently with the In-Memory storage engine. For example, when scanning an index and identifying rows that match a certain predicate, we see that a native proc requires around 1\/4th<\/sup> the number of CPU instructions compared with traditional interpreted T-SQL queries and stored procedures.<\/p>\n In this post we walk through some of the considerations when developing and using natively compiled stored procedures in your application.<\/p>\n Natively compiled stored procedures are optimized for OLTP-style operations. Now, what do we mean by that? Some characteristics: a) single-threaded execution (MAXDOP=1); b)\u00a0point lookups and small range scans, no full table scans, in general operations that touch a relatively small number of rows; c) nested-loops joins and stream aggregation; d) short-running transactions, in the ideal case a transaction spans a single execution of a natively compiled stored procedure.<\/p>\n Some examples of OLTP-style operations:<\/p>\n Native procs are not optimized for reporting-style queries which require joins between and aggregation over large data sets.<\/p>\n The body of a natively compiled stored procedure must comprise exactly one\u00a0ATOMIC block. ATOMIC blocks are a new concept in SQL Server 2014 that can be used only with native procs. The basic thing an ATOMIC block gives you is that all statements within the block either succeed or fail, atomically. In the context of transactions this means that:<\/p>\n Because transactions are handled through the ATOMIC block, there is no need to bother with BEGIN TRANSACTION, ROLLBACK, or COMMIT inside natively compiled stored procedures. In fact, that syntax is not supported.<\/p>\n For more details about ATOMIC blocks and transaction and error handling, see the corresponding topic in Books Online<\/a>.<\/p>\n As with all transactions that touch memory-optimized tables, with natively compiled stored procedures you will need to consider retry logic to deal with potential failures such as write conflicts (error 41302) or dependency failures (error 41301). In most applications the failure rate will be low, but it is still necessary to deal with the failures by retrying the transaction. Two suggested ways of implementing retry logic are:<\/p>\n For more details on retry logic and the error conditions to consider, see the corresponding topic in Books Online<\/a>.<\/p>\n Like traditional interpreted T-SQL stored procedures, natively compiled stored procedures support table-valued parameters (TVPs), which allow you to pass a rowset into a stored procedure. For example, if you want to insert a sales order along with its line items, you can use a TVP to encapsulate the line items.<\/p>\n The syntax and mechanisms to define and use table-valued parameters with natively compiled procs are the same as for interpreted procs. The only thing you need to take care of, is that you use a memory-optimized table type for the TVP. You can use memory-optimized table types with parameters in both native and interpreted stored procedures.<\/p>\n For more details and an example of the use of TVPs in natively compiled stored procedures, see the Books Online topic on memory-optimized table variables<\/a>.<\/p>\n In general, you can view a stored procedure execution as consisting of three phases:<\/p>\n Figure 1: Phases of stored procedure execution<\/em><\/p>\n If your application is sensitive to latency, i.e. how long it takes for a single stored procedure to execute, you will also want to optimize how you call the stored procedure from the client, in order to limit the overhead from the call of the stored procedure. Optimizing clients calls of stored procedures is not specific to natively compiled stored procedures, but it does play a bigger role for natively compiled procs, as the client invocation is proportionately a larger part of the overall procedure execution time, due to the optimization in the processing of queries and DML operations.<\/p>\n To optimize stored procedure calls, we recommend:<\/p>\n For an example of both direct and prepared execution with the ODBC driver in SQL Native Client see Books Online here<\/a>.<\/p>\n SQL Server 2014 has some limitations on the features supported inside natively compiled stored procedures, which you should consider when using these stored procs, and if you want to get the most out of native procs.<\/p>\n Because of these limitations you will see it can be challenging to migrate stored procedures in your existing application to native. We suggest you look for patterns that fit the surface area for native procs and migrate those patterns to native. You do not always need to migrate an entire stored procedure: if the existing stored procedure has a substantial piece of logic that can be migrated to native, you can consider putting only that piece into a new native proc, and modify the existing proc to call the new one.\u00a0Note that migrating a single statement to a natively compiled stored procedure may not be beneficial due to the overhead of stored procedure invocation \u2013 you really want to have a larger subset of the proc that you move to native.<\/p>\n To understand which features in an existing interpreted T-SQL stored procedure are supported in natively compiled stored procedures, we recommend using the Native Compilation Advisor, which is part of Management Studio in SQL Server 2014. The Advisor will tell you which features used in the stored procedure are not supported in native, which will help in identifying the parts of the procedure that can be migrated to native, and will indicate the limitations you may need to work around.<\/p>\n Two following two screenshots show an example of how to use the Advisor with the stored procedure dbo.uspGetBillOfMaterials in AdventureWorks.<\/p>\nOLTP-Style Operations<\/h1>\n
\n
ATOMIC Blocks in Native Procs<\/h1>\n
\n
Retry logic for handling failures<\/h1>\n
\n
Table-Valued Parameters<\/h1>\n
Optimizing Client Invocation<\/h1>\n
<\/a><\/p>\n\n
\n
T-SQL Surface Area Limitations<\/h1>\n