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;








5















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.










share|improve this question









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.

























    5















    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.










    share|improve this question









    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.





















      5












      5








      5








      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.










      share|improve this question









      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






      share|improve this question









      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.










      share|improve this question









      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.








      share|improve this question




      share|improve this question








      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.






















          2 Answers
          2






          active

          oldest

          votes


















          5














          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






          share|improve this answer

























          • 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


















          5














          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.






          share|improve this answer

























            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.









            draft saved

            draft discarded


















            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









            5














            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






            share|improve this answer

























            • 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















            5














            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






            share|improve this answer

























            • 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













            5












            5








            5







            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






            share|improve this answer















            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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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

















            • 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













            5














            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.






            share|improve this answer





























              5














              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.






              share|improve this answer



























                5












                5








                5







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 6 hours ago

























                answered 6 hours ago









                Joe ObbishJoe Obbish

                23.1k43498




                23.1k43498




















                    Ben Amada is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                    François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480