Increase speed altering column on large table to NON NULLRunning out of Transaction Log space during Alter TableQuickly change NULL column to NOT NULLAltering a table to add a New Column vs Create table with New ColumnAdding nullable column to table costs more than 10 minutesSQL Server - Export large table without primary keyAlter All Data Types To Different Data TypeWill altering an nvarchar(max) column to nvarchar(50) lock the table?Does ALTER TABLE - ADD COLUMN lock the table?Architecture design for a large number of db records upgradeWhat is the fastest way to convert many nullable ints 1:1 to a binary string?
How can one's career as a reviewer be ended?
Should I refuse being named as co-author of a bad quality paper?
Is using 'echo' to display attacker-controlled data on the terminal dangerous?
Is it possible to have a wealthy country without a middle class?
What is this airplane?
Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?
Which is the better way to call a method that is only available to one class that implements an interface but not the other one?
What would be the way to say "just saying" in German? (Not the literal translation)
How can I remove material from this wood beam?
Why am I Seeing A Weird "Notch" on the Data Line For Some Logical 1s?
How come the nude protesters were not arrested?
Are there any normal animals in Pokemon universe?
Why not invest in precious metals?
Specific use case of to_address
How to make insert mode mapping count as multiple undos?
Has there been a multiethnic Star Trek character?
If I leave the US through an airport, do I have to return through the same airport?
How to safely destroy (a large quantity of) valid checks?
Ability To Change Root User Password (Vulnerability?)
Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?
Why was this person allowed to become Grand Maester?
Why did Intel abandon unified CPU cache?
How creative should the DM let an artificer be in terms of what they can build?
How can I deal with uncomfortable silence from my partner?
Increase speed altering column on large table to NON NULL
Running out of Transaction Log space during Alter TableQuickly change NULL column to NOT NULLAltering a table to add a New Column vs Create table with New ColumnAdding nullable column to table costs more than 10 minutesSQL Server - Export large table without primary keyAlter All Data Types To Different Data TypeWill altering an nvarchar(max) column to nvarchar(50) lock the table?Does ALTER TABLE - ADD COLUMN lock the table?Architecture design for a large number of db records upgradeWhat is the fastest way to convert many nullable ints 1:1 to a binary string?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I recently added a NULL-able bit column to a table which has close to 500 million rows. There's not a default on the column, however all inserts are specifying a value of 0 or 1, and I ran a one-time routine to assign 0 or 1 to all existing rows (updating the rows in small batches). Every row should now have a 0 or 1 in that column.
I want to make the bit column non-nullable however when I tried doing so via ALTER TABLE t1 ALTER COLUMN c1 bit not null, it started running for 3 minutes and I stopped it because it was blocking all reads to the table and I suspected it was going to take a long time to complete. It's possible it wouldn't take too long but I couldn't risk too much unavailability. The rollback itself took 6 minutes.
Do you have any suggestions on how I can make the column non-nullable without it taking potentially hours to complete? Additionally is there any way to estimate how long the ALTER TABLE ALTER COLUMN statement I started and then cancelled would take to complete?
I am using SQL Server 2017 Web Edition.
sql-server sql-server-2017 alter-table null
New contributor
Ben Amada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I recently added a NULL-able bit column to a table which has close to 500 million rows. There's not a default on the column, however all inserts are specifying a value of 0 or 1, and I ran a one-time routine to assign 0 or 1 to all existing rows (updating the rows in small batches). Every row should now have a 0 or 1 in that column.
I want to make the bit column non-nullable however when I tried doing so via ALTER TABLE t1 ALTER COLUMN c1 bit not null, it started running for 3 minutes and I stopped it because it was blocking all reads to the table and I suspected it was going to take a long time to complete. It's possible it wouldn't take too long but I couldn't risk too much unavailability. The rollback itself took 6 minutes.
Do you have any suggestions on how I can make the column non-nullable without it taking potentially hours to complete? Additionally is there any way to estimate how long the ALTER TABLE ALTER COLUMN statement I started and then cancelled would take to complete?
I am using SQL Server 2017 Web Edition.
sql-server sql-server-2017 alter-table null
New contributor
Ben Amada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I recently added a NULL-able bit column to a table which has close to 500 million rows. There's not a default on the column, however all inserts are specifying a value of 0 or 1, and I ran a one-time routine to assign 0 or 1 to all existing rows (updating the rows in small batches). Every row should now have a 0 or 1 in that column.
I want to make the bit column non-nullable however when I tried doing so via ALTER TABLE t1 ALTER COLUMN c1 bit not null, it started running for 3 minutes and I stopped it because it was blocking all reads to the table and I suspected it was going to take a long time to complete. It's possible it wouldn't take too long but I couldn't risk too much unavailability. The rollback itself took 6 minutes.
Do you have any suggestions on how I can make the column non-nullable without it taking potentially hours to complete? Additionally is there any way to estimate how long the ALTER TABLE ALTER COLUMN statement I started and then cancelled would take to complete?
I am using SQL Server 2017 Web Edition.
sql-server sql-server-2017 alter-table null
New contributor
Ben Amada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I recently added a NULL-able bit column to a table which has close to 500 million rows. There's not a default on the column, however all inserts are specifying a value of 0 or 1, and I ran a one-time routine to assign 0 or 1 to all existing rows (updating the rows in small batches). Every row should now have a 0 or 1 in that column.
I want to make the bit column non-nullable however when I tried doing so via ALTER TABLE t1 ALTER COLUMN c1 bit not null, it started running for 3 minutes and I stopped it because it was blocking all reads to the table and I suspected it was going to take a long time to complete. It's possible it wouldn't take too long but I couldn't risk too much unavailability. The rollback itself took 6 minutes.
Do you have any suggestions on how I can make the column non-nullable without it taking potentially hours to complete? Additionally is there any way to estimate how long the ALTER TABLE ALTER COLUMN statement I started and then cancelled would take to complete?
I am using SQL Server 2017 Web Edition.
sql-server sql-server-2017 alter-table null
sql-server sql-server-2017 alter-table null
New contributor
Ben Amada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ben Amada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 5 hours ago
Comment Converter
1,4491326
1,4491326
New contributor
Ben Amada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 9 hours ago
Ben AmadaBen Amada
1264
1264
New contributor
Ben Amada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ben Amada is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If you are on Enterprise Edition (EE) a better strategy might have been to add it as NOT NULL with a default of 0 or 1 (whichever is most common).
This is a metadata-only change in EE. Then update the ones needing to be flipped. This means fewer updates and no need to change the column nullability when finished. - martin-smith
Unfortunately I'm on the Web edition. That's quite a handy EE feature, I wasn't aware it existed.
– Ben Amada
7 hours ago
add a comment |
Instead of changing the column definition you could add a CHECK CONSTRAINT that doesn't allow NULLs for that column. The table will still need to be scanned but it won't need to modify every single data page, so it should be a much faster operation. Regrettably, a Sch-M lock will still be held during the operation. One trick is to try to get as much of the table into the buffer pool as possible before attempting to add the constraint. That may reduce the amount of time the Sch-M lock is held.
You can then remove the constraint and change the column definition during your next maintenance window.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Ben Amada is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f240055%2fincrease-speed-altering-column-on-large-table-to-non-null%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you are on Enterprise Edition (EE) a better strategy might have been to add it as NOT NULL with a default of 0 or 1 (whichever is most common).
This is a metadata-only change in EE. Then update the ones needing to be flipped. This means fewer updates and no need to change the column nullability when finished. - martin-smith
Unfortunately I'm on the Web edition. That's quite a handy EE feature, I wasn't aware it existed.
– Ben Amada
7 hours ago
add a comment |
If you are on Enterprise Edition (EE) a better strategy might have been to add it as NOT NULL with a default of 0 or 1 (whichever is most common).
This is a metadata-only change in EE. Then update the ones needing to be flipped. This means fewer updates and no need to change the column nullability when finished. - martin-smith
Unfortunately I'm on the Web edition. That's quite a handy EE feature, I wasn't aware it existed.
– Ben Amada
7 hours ago
add a comment |
If you are on Enterprise Edition (EE) a better strategy might have been to add it as NOT NULL with a default of 0 or 1 (whichever is most common).
This is a metadata-only change in EE. Then update the ones needing to be flipped. This means fewer updates and no need to change the column nullability when finished. - martin-smith
If you are on Enterprise Edition (EE) a better strategy might have been to add it as NOT NULL with a default of 0 or 1 (whichever is most common).
This is a metadata-only change in EE. Then update the ones needing to be flipped. This means fewer updates and no need to change the column nullability when finished. - martin-smith
edited 6 hours ago
community wiki
2 revs, 2 users 91%
Comment Converter
Unfortunately I'm on the Web edition. That's quite a handy EE feature, I wasn't aware it existed.
– Ben Amada
7 hours ago
add a comment |
Unfortunately I'm on the Web edition. That's quite a handy EE feature, I wasn't aware it existed.
– Ben Amada
7 hours ago
Unfortunately I'm on the Web edition. That's quite a handy EE feature, I wasn't aware it existed.
– Ben Amada
7 hours ago
Unfortunately I'm on the Web edition. That's quite a handy EE feature, I wasn't aware it existed.
– Ben Amada
7 hours ago
add a comment |
Instead of changing the column definition you could add a CHECK CONSTRAINT that doesn't allow NULLs for that column. The table will still need to be scanned but it won't need to modify every single data page, so it should be a much faster operation. Regrettably, a Sch-M lock will still be held during the operation. One trick is to try to get as much of the table into the buffer pool as possible before attempting to add the constraint. That may reduce the amount of time the Sch-M lock is held.
You can then remove the constraint and change the column definition during your next maintenance window.
add a comment |
Instead of changing the column definition you could add a CHECK CONSTRAINT that doesn't allow NULLs for that column. The table will still need to be scanned but it won't need to modify every single data page, so it should be a much faster operation. Regrettably, a Sch-M lock will still be held during the operation. One trick is to try to get as much of the table into the buffer pool as possible before attempting to add the constraint. That may reduce the amount of time the Sch-M lock is held.
You can then remove the constraint and change the column definition during your next maintenance window.
add a comment |
Instead of changing the column definition you could add a CHECK CONSTRAINT that doesn't allow NULLs for that column. The table will still need to be scanned but it won't need to modify every single data page, so it should be a much faster operation. Regrettably, a Sch-M lock will still be held during the operation. One trick is to try to get as much of the table into the buffer pool as possible before attempting to add the constraint. That may reduce the amount of time the Sch-M lock is held.
You can then remove the constraint and change the column definition during your next maintenance window.
Instead of changing the column definition you could add a CHECK CONSTRAINT that doesn't allow NULLs for that column. The table will still need to be scanned but it won't need to modify every single data page, so it should be a much faster operation. Regrettably, a Sch-M lock will still be held during the operation. One trick is to try to get as much of the table into the buffer pool as possible before attempting to add the constraint. That may reduce the amount of time the Sch-M lock is held.
You can then remove the constraint and change the column definition during your next maintenance window.
edited 6 hours ago
answered 6 hours ago
Joe ObbishJoe Obbish
23.1k43498
23.1k43498
add a comment |
add a comment |
Ben Amada is a new contributor. Be nice, and check out our Code of Conduct.
Ben Amada is a new contributor. Be nice, and check out our Code of Conduct.
Ben Amada is a new contributor. Be nice, and check out our Code of Conduct.
Ben Amada is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f240055%2fincrease-speed-altering-column-on-large-table-to-non-null%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown