You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In SQL Server 2019, a new index option was added called [OPTIMIZE_FOR_SEQUENTIAL_KEY](https://docs.microsoft.com/sql/t-sql/statements/create-index-transact-sql#sequential-keys) that is intended to address an issue known as [last page insert contention](https://support.microsoft.com/kb/4460004). Most of the solutions to this problem that have been suggested in the past involve making changes to either the application or the structure of the contentious index, which can be costly and sometimes involve performance trade-offs. Rather than making major structural changes, OPTIMIZE_FOR_SEQUENTIAL_KEY addresses some of the SQL Server scheduling issues that can lead to severely reduced throughput when last page insert contention occurs. Using the OPTMIZE_FOR_SEQUENTIAL_KEY index option can help maintain consistent throughput in high-concurrency environments when the following conditions are true:
6
+
7
+
- The index has a sequential key
8
+
- The number of concurrent insert threads to the index far exceeds the number of schedulers (in other words logical cores)
9
+
- The index has a high rate of new page allocations (page splits), which is most often due to a large row size
10
+
11
+
This sample illustrates how OPTIMIZE_FOR_SEQUENTIAL_KEY can be used to improve throughput on workloads that are suffering from severe last page insert contention bottlenecks.
12
+
13
+
### Contents
14
+
15
+
[About this sample](#about-this-sample)<br/>
16
+
[Before you begin](#before-you-begin)<br/>
17
+
[Run this sample](#run-this-sample)<br/>
18
+
[Disclaimers](#disclaimers)<br/>
19
+
[Related links](#related-links)<br/>
20
+
21
+
22
+
<aname=about-this-sample></a>
23
+
24
+
## About this sample
25
+
26
+
-**Applies to:** SQL Server 2019 (or higher)
27
+
-**Workload:** High-concurrency OLTP
28
+
-**Programming Language:** T-SQL
29
+
-**Authors:** Pam Lahoud
30
+
-**Update history:** Created August 15, 2019
31
+
32
+
<aname=before-you-begin></a>
33
+
34
+
## Before you begin
35
+
36
+
To run this sample, you need the following prerequisites.
37
+
38
+
1. SQL Server 2019 (or higher)
39
+
2. A server (physical or virtual) with multiple cores
40
+
3. The [AdventureWorks2016_EXT](https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorks2016_EXT.bak) sample database
41
+
42
+
[!NOTE]
43
+
> This sample was designed for a server with 8 logical cores. If you run the sample on a server with more cores, you may need to increase the number of concurrent threads in order to observe the improvement.
44
+
45
+
46
+
<aname=run-this-sample></a>
47
+
48
+
## Run this sample
49
+
50
+
1. Copy the files from the root folder to a folder on the SQL Server.
51
+
52
+
2. Download [AdventureWorks2016_EXT.bak](https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorks2016_EXT.bak) and restore it to your SQL Server 2019 instance.
53
+
54
+
3. From SQL Server Management Studio or Azure Data Studio, run the Setup.sql script.
55
+
56
+
4. Modify the SequentialInserts_Optimized.bat and SequentialInserts_Unoptimized.bat files and change the -S parameter to point to the server where the setup script was run. For example, `-S.\SQL2019` points to an instance named SQL2019 on the local server.
57
+
58
+
5. Open the SQL2019_LatchWaits.htm file to open a Performance Monitor session in your default browser.
59
+
60
+
6. Right-click anywhere in the browser window to clear the existing data from the session.
61
+
62
+
7. Click the play button to start the Performance Monitor session.
63
+
64
+
8. From a Command Prompt, browse to the folder that contains the demo files and run SequentialInserts_Unoptimized.bat, then return to the Performance Monitor window. You should see a high number of Page Latch waits as well as high average wait times. Note the time it takes for the script to complete.
65
+
66
+
9. Run the SequentialInserts_Optimized.bat script from the same Command Prompt window and again return to the Performance Monitor window. This time you should see much lower number and duration of Page Latch waits, along with higher Batch requests/sec. Note the time it takes for the script to complete, it should be significantly faster than the Unoptimized script.
67
+
68
+
10.**OPTIONAL** - Modify the `-n256` parameter in the Optimized and Unoptimized scripts to see the effect on performance. Generally, the larger the number of concurrent sessions, the greater the improvement will be with OPTIMIZE_FOR_SEQUENTIAL_KEY.
69
+
70
+
71
+
72
+
<aname=disclaimers></a>
73
+
74
+
## Disclaimers
75
+
The code included in this sample is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this quick start sample.
76
+
77
+
<aname=related-links></a>
78
+
79
+
## Related Links
80
+
81
+
For more information, see these articles:
82
+
83
+
[CREATE INDEX - Sequential Keys](https://docs.microsoft.com/sql/t-sql/statements/create-index-transact-sql#sequential-keys)
84
+
85
+
[Behind the Scenes on OPTIMIZE_FOR_SEQUENTIAL_KEY](https://techcommunity.microsoft.com/t5/SQL-Server/Behind-the-Scenes-on-OPTIMIZE-FOR-SEQUENTIAL-KEY/ba-p/806888)
0 commit comments