This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.
Today, I worked in a very interested case. Our customer is trying to run an ALTER TABLE <TableName> to add two columns but this process is taking too much time, even, when the documentation reported "Starting with SQL Server 2012 (11.x) Enterprise Edition, adding a NOT NULL column with a default value is an online operation when the default value is a runtime constant. This means that the operation is completed almost instantaneously despite the number of rows in the table, because the existing rows in the table aren't updated during the operation" https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql?view=azuresqldb-current#adding-not-null-columns-as-an-online-operation
So, let's try to analyze what is happening behind the scenes and explain why is taking too much time.
We have the following table:
We added around 7M of rows in this table adding random values.
Right now, let's try to run a query that reads the data, for example, Select Name from Table 1 and other session trying to run the query: ALTER table Table1 ADD mynewdata char(2)
Meanwhile the query Select Name from Table1 (Session:74) is still running the ALTER TABLE (Session:86) will be waiting for session 74 completion.
As you could see in the following picture the session 74 has Sch-S Granted and the session 86 is waiting for 74.
Reviewing this URL: sys.dm_tran_locks (Transact-SQL) - SQL Server | Microsoft Learn
LCK_M_SCH_S and LCK_M_SCH_M wait types are wait states for schema stability (Sch-S) and schema modification (Sch-M) locks. As Sch-M requires exclusive access to the table all request would be blocked. As Sch-S vs. Sch-M: Sch-S locks are incompatible with Sch-M locks. So, Sch-M means that you are not immediately granted and you have to wait for other Sch-S have been released.
Sch-S locks are used to avoid table alterations when tables are in use. SQL Server acquires during the query compilation and execution of SELECT queries. In the majority of cases, you could address this situation by changing deployment or database maintenance strategies in the system.
Enjoy!