I have a table with millions of rows and a column that allows NULL values. However no row currently has a NULL value for that column (I can verify this fairly quickly with a query). However when I execute the command
ALTER TABLE MyTable ALTER COLUMN MyColumn BIGINT NOT NULL;
the query takes forever relatively speaking. It actually takes between 10 and 20 minutes, more than twice as long as adding a check constraint. Is there a way to instantly update the table's metadata for that column, especially since I know that no row has a NULL value for that column?
2 Answers 2
@ypercube's answer does manage this partially as a metadata only change.
Adding the constraint with NOCHECK
means that no rows will need to be read to verify it, and if you are starting from a position where the column does not contain NULL
values (and if you know none will be added between checking and adding the constraint) then, as the constraint prevents NULL
values being created from future INSERT
or UPDATE
operations, this will work.
Adding the constraint can still have an impact on concurrent transactions however. The ALTER TABLE
will need to acquire a Sch-M
lock first. Whilst it is waiting for this all other table accesses will be blocked as described here.
Once the Sch-M
lock is acquired the operation should be pretty quick however.
One problem with this is that even if you know the column in fact has no NULL
s the constraint is not trusted by the query optimiser which means that the plans can be sub optimal.
CREATE TABLE T (X INT NULL)
INSERT INTO T
SELECT ROW_NUMBER() OVER (ORDER BY @@SPID)
FROM master..spt_values
ALTER TABLE T WITH NOCHECK
ADD CONSTRAINT X_NOT_NULL
CHECK (X IS NOT NULL) ;
SELECT *
FROM T
WHERE X NOT IN (SELECT X FROM T)
Plan
Compare this with the simpler
ALTER TABLE T ALTER COLUMN X INT NOT NULL
SELECT *
FROM T
WHERE X NOT IN (SELECT X FROM T)
Plan
One possible problem you might encounter with altering the column definition in this way is that it not only needs to read all the rows to verify that they meet the condition but also can end up actually performing logged updates to the rows.
A possible half way house might be to add the check constraint WITH CHECK
. This will be slower than WITH NOCHECK
as it needs to read all rows but it does allow the query optimiser to give the simpler plan in the query above and it should avoid the possible logged updates issue.
You could, instead of altering the column, add a table CHECK
constraint with the NOCHECK
option:
ALTER TABLE MyTable WITH NOCHECK
ADD CONSTRAINT MyColumn_NOT_NULL
CHECK (MyColumn IS NOT NULL) ;
-
1This would prevent future updates or inserts that make the column
NULL
but would not be able to be used by the query optimiser.Martin Smith– Martin Smith2013年08月28日 14:22:36 +00:00Commented Aug 28, 2013 at 14:22 -
@MartinSmith Oh yes, I just read the answer and comments in the similar question: How do you add a NOT NULL Column to a large table in SQL Server? Please, add an answer with the problems or a better solution and I'll remove mine.ypercubeᵀᴹ– ypercubeᵀᴹ2013年08月28日 14:42:35 +00:00Commented Aug 28, 2013 at 14:42
-
2I don't have a better solution. I upvoted this because it provides a partial solution. If all the OP wants to do is prevent invalid data it will work (and should be quicker than
ALTER COLUMN
as once theSch-M
lock is acquired this doesn't need to scan the rows at all). Just pointing out that it isn't quite the same (e.g. if used in aNOT IN
query the plan will be more complex)Martin Smith– Martin Smith2013年08月28日 14:51:43 +00:00Commented Aug 28, 2013 at 14:51