SQL Server WAIT_AT_LOW_PRIORITY option to create indexes

By:   |   Updated: 2023-05-10   |   Comments (5)   |   Related: > SQL Server 2022


Problem

With each release of SQL Server, there are usually new features or enhancements to existing features. An enhancement in SQL Server 2022 allows for creating or altering indexes to wait for resources prior to building or updating an index. This helps limit resource contention that may occur when maintaining indexes.

Solution

SQL Server 2022 added the "Wait at Low Priority" option that allows index creation to wait for resources to become available before executing. This feature reduces the impact on long-running queries that have a higher execution priority. When working with a big table, index creation can take several hours. That is why having some options to prioritize the operations is useful. This tip will explain the different options this new argument uses during index creation. This feature is also available in Azure SQL and Azure SQL Managed instances. Also, this option will only work with Enterprise and Developer editions.

Syntax

Let's take a look at the syntax to understand how it works:

CREATE CLUSTERED INDEX INDEX_NAME
ON TABLENAME (COLUMNNAME)
WITH (ONLINE = ON 
        (WAIT_AT_LOW_PRIORITY 
           (MAX_DURATION = X MINUTES, ABORT_AFTER_WAIT = BLOCKERS)
        )
     );
GO
  • INDEX_NAME - is the name of the index
  • TABLENAME - is the name of the table where the index will be created
  • COLUMNNAME - is the column or columns where the index will be applied
  • ONLINE = ON - this needs to be used for this feature (NOTE: this is an Enterprise or Developer edition only feature)
  • WAIT_AT_LOW_PRIORITY - is the new setting
    • MAX_DURATION - is used to specify the maximum time in minutes to wait until an action
    • ABORT_AFTER_WAIT - is used to abort the operation if it exceeds the wait time
      • NONE - is to wait for the lock with regular priority
      • SELF - exits the online index operation
      • BLOCKERS - kills transactions blocking the index rebuild

Create Test Table

First, let's create a table named dbo.salesOrderDetailTest for testing from another table in the AdventureWorks database.

SELECT * INTO dbo.salesOrderDetailTest FROM [Sales].[SalesOrderDetail]

CREATE INDEX with WAIT_AT_LOW_PRIORITY

Add an index with the WAIT_AT_LOW_PRIORITY option.

CREATE CLUSTERED INDEX cindex 
ON [dbo].[salesOrderDetailTest] ([SalesOrderDetailID]) 
WITH (ONLINE = ON 
        (WAIT_AT_LOW_PRIORITY 
           (MAX_DURATION = 50 MINUTES, ABORT_AFTER_WAIT = SELF)
        )
     );
GO

This will try to create the index and wait for 50 minutes if there is a process running with a higher priority. If it is still waiting after 50 minutes the create index operation will be cancelled.

The next example is similar, but it will kill sessions that are blocking after the 50 minutes and then create the index.

CREATE CLUSTERED INDEX cindex 
ON [dbo].[salesOrderDetailTest] ([SalesOrderDetailID]) 
WITH (ONLINE = ON 
        (WAIT_AT_LOW_PRIORITY 
           (MAX_DURATION = 50 MINUTES, ABORT_AFTER_WAIT = KILL)
        )
     );
GO

If everything is OK, the index will be created as shown below.

Check the index created in SSMS

ALTER INDEX with WAIT_AT_LOW_PRIORITY

If you need to modify the properties of an index or reorganize or rebuild an index, you can use ALTER INDEX as follows.

ALTER INDEX cindex 
ON [dbo].[salesOrderDetailTest]
REBUILD
WITH (ONLINE = ON 
        (WAIT_AT_LOW_PRIORITY 
           (MAX_DURATION = 30 MINUTES, ABORT_AFTER_WAIT = SELF)
        )
     );
GO
Next Steps

To learn more about SQL Server indexes, please refer to these links:



sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Daniel Calbimonte Daniel Calbimonte is a Microsoft SQL Server MVP, Microsoft Certified Trainer and 6-time Microsoft Certified IT Professional. Daniel started his career in 2001 and has worked with SQL Server 6.0 to 2022. Daniel is a DBA as well as specializes in Business Intelligence (SSIS, SSAS, SSRS) technologies.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2023-05-10

Comments For This Article




Thursday, March 28, 2024 - 5:30:11 AM - ste hoban Back To Top (92134)
also, this feature can cause an issue on a really active table. When it 'switches in' the new index at the very end of the process, it waits for current activity on the table to cease, thereby 'holding' any new queries, which depending on how busy your systems are may be an issue!

Thursday, May 11, 2023 - 4:14:29 PM - Twan Back To Top (91186)
From MSLearn:
WAIT_AT_LOW_PRIORITY with online index operations
Applies to: This syntax for CREATE INDEX currently applies to SQL Server 2022 (16.x), Azure SQL Database, and Azure SQL Managed Instance only. For ALTER INDEX, this syntax applies to SQL Server (Starting with SQL Server 2014 (12.x)) and Azure SQL Database.

Thursday, May 11, 2023 - 10:38:44 AM - Daniel Calbimonte Back To Top (91183)
The WAIT_AT_LOW_PRIORITY can work with the online index operation now.

Thursday, May 11, 2023 - 2:57:36 AM - Twan Back To Top (91181)
I believe there is some confusion. Wait_at_low_priority was introduced for shrink operations in 2022, for index operations it was introduced in Hekaton, wasn't it?

Wednesday, May 10, 2023 - 10:35:50 AM - Steve Back To Top (91177)
I thought this feature was introduced a while ago.














get free sql tips
agree to terms