Over the few months i have been playing with SQL Server 2016 new features. Microsoft SQL Server 2016 introduced new alter column with ONLINE option. Let’s explore this.
- Create a table and insert data
CREATE TABLE dbo.test_online (column_1 INT ) ; GO INSERT INTO dbo.test_online(column_1) values(10) GO
- Find what page has been allocated for the above operation. I have used un documented function fn_PhysLocCracker to get the information
SELECT fnph.page_id FROM dbo.test_online OUTER APPLY sys.fn_PhysLocCracker(%%PhysLoc%%) AS fnph;
- Perform alter column without online option
ALTER TABLE dbo.test_online ALTER COLUMN column_1 DECIMAL (5, 2) --WITH (ONLINE = ON); GO
- Verify what page has been allocated for the above alter column
- You can see in the above screenshot that same page has been allocated for alter command.
- Now, let’s Perform alter column with online option. Repeat the above steps
ALTER TABLE dbo.test_online ALTER COLUMN column_1 DECIMAL (5, 2) WITH (ONLINE = ON); GO
- Now verify the allocated page for the above alter command
- You can see in the above screenshot that the new page has been allocated for alter column statement without changing the old page id. This means leaving the old page available during the operation.
Conclusion: Online operation on alter column is really useful feature for DBA’s to perform the alter table with minimal downtime during the operation especially with table with large data.
Hope you like the post!