Tuesday, March 27, 2012
adding a new column to a large table
Windows 2000 Server
I need to add a new column to a table that will likely have millions of rows
at most customer sites. The table currently has one FK and 2 unclustered
indexes. The new column is an int column. When I run the script to add the
column on our test server (table has about 5 million rows) it takes about
1/2 hour. Unfortunately, we can't have that kind of down time on that
table.
Is there a typical method for doing this more efficiently? Would it help to
drop the indexes before and recreate them after or would that end up taking
the same amount of time anyway? What about creating a temp table, copying
that data into the new table and renaming it?
Are you trying to add this column in the middle of the other columns, or at
the end of the other columns? If the middle, try adding to the end and see
if that sppeds things up, Im betting thats whats going on.
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>
|||Here's the script:
ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
DF_Images_Version DEFAULT 1
I'd guess that adds to the end, no?
"ChrisR" <noemail@.bla.com> wrote in message
news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
> Are you trying to add this column in the middle of the other columns, or
> at the end of the other columns? If the middle, try adding to the end and
> see if that sppeds things up, Im betting thats whats going on.
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
>
|||Yes it does. I just realized you are adding these to customer sires. Are you
logging onto the boxes locally, or over the WAN?
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
> Here's the script:
> ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
> DF_Images_Version DEFAULT 1
> I'd guess that adds to the end, no?
> "ChrisR" <noemail@.bla.com> wrote in message
> news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
>
|||We haven't run the script on an actual customer site yet, only on our test
server which is a close resemblance. When we upgrade the customers, we will
likely remote in and run the script on the actual server.
(thanks for taking the time to help)
"ChrisR" <noemail@.bla.com> wrote in message
news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
> Yes it does. I just realized you are adding these to customer sires. Are
> you logging onto the boxes locally, or over the WAN?
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
>
|||I just realized...what about the default value? It has to apply that
default value to all the existing rows. Maybe that's what's taking a long
time. I wonder if disabling some level of logging during this update would
speed that up.
"ChrisR" <noemail@.bla.com> wrote in message
news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
> Yes it does. I just realized you are adding these to customer sires. Are
> you logging onto the boxes locally, or over the WAN?
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
>
|||Yes, the defualt value will take a bit. But it doesnt seems to me like Ive
ever experienced that severe of slowness, which is what led to my other
questions. Creating with NULL will be much faster of course... but still
thats a long time. I guess try just to add with NULL for timing tests.
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:eLFnFbhZFHA.2520@.TK2MSFTNGP09.phx.gbl...
>I just realized...what about the default value? It has to apply that
>default value to all the existing rows. Maybe that's what's taking a long
>time. I wonder if disabling some level of logging during this update would
>speed that up.
> "ChrisR" <noemail@.bla.com> wrote in message
> news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
>
|||On Tue, 31 May 2005 10:50:16 -0600, Joel Lyons wrote:
>Here's the script:
>ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
>DF_Images_Version DEFAULT 1
Hi Joel,
You're right (in your other post) - adding the default is what takes
time.
Consider breaking it in smaller steps:
1. Add a column, keep it nullable - should be fast.
ALTER TABLE dbo.Images
ADD Version tinyint
go
2. Fill the default value. This will take time, but if you use smallish
batches to process the data, locks will be released in between, so your
other applications will be able to proceed. If you have full recovery
model, then make sure that you back up the transaction log in between
(unless you don't mind it growing to accomodate all the changes).
SET ROWCOUNT 50000-- experiment to get the optimal setting
DECLARE @.rc int
SET @.rc = 1
WHILE @.rc > 0
BEGIN
UPDATE dbo.Images
SET Version = 1
WHERE Version IS NULL
SET @.rc = @.@.ROWCOUNT
-- BACKUP LOG MyDatabase TO MyDatabaseLog
END
SET ROWCOUNT 0-- never forget to reset ROWCOUNT!!
3. When all rows have been filled, set the NOT NULL constraint and the
DEFAULT:
ALTER TABLE dbo.Images
ALTER COLUMN Version tinyint NOT NULL
ALTER TABLE dbo.Images
ADD DEFAULT 1 FOR Version
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||When you allow nulls and do not provide a default value, it will be very
very quick, because the new rows are not materialized until the page is
re-written... It works sort of like a virtual column... when you provide a
default value, the values must ACTUALLY be written, so the time it would
take is relative to the IO speed.
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>
|||Very interesting info - thanks all!
I'll try some of these ideas and let you know.
-Joel
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>
adding a new column to a large table
Windows 2000 Server
I need to add a new column to a table that will likely have millions of rows
at most customer sites. The table currently has one FK and 2 unclustered
indexes. The new column is an int column. When I run the script to add the
column on our test server (table has about 5 million rows) it takes about
1/2 hour. Unfortunately, we can't have that kind of down time on that
table.
Is there a typical method for doing this more efficiently? Would it help to
drop the indexes before and recreate them after or would that end up taking
the same amount of time anyway? What about creating a temp table, copying
that data into the new table and renaming it?Are you trying to add this column in the middle of the other columns, or at
the end of the other columns? If the middle, try adding to the end and see
if that sppeds things up, Im betting thats whats going on.
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>|||Here's the script:
ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
DF_Images_Version DEFAULT 1
I'd guess that adds to the end, no?
"ChrisR" <noemail@.bla.com> wrote in message
news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
> Are you trying to add this column in the middle of the other columns, or
> at the end of the other columns? If the middle, try adding to the end and
> see if that sppeds things up, Im betting thats whats going on.
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
>|||Yes it does. I just realized you are adding these to customer sires. Are you
logging onto the boxes locally, or over the WAN?
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
> Here's the script:
> ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
> DF_Images_Version DEFAULT 1
> I'd guess that adds to the end, no?
> "ChrisR" <noemail@.bla.com> wrote in message
> news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
>|||We haven't run the script on an actual customer site yet, only on our test
server which is a close resemblance. When we upgrade the customers, we will
likely remote in and run the script on the actual server.
(thanks for taking the time to help)
"ChrisR" <noemail@.bla.com> wrote in message
news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
> Yes it does. I just realized you are adding these to customer sires. Are
> you logging onto the boxes locally, or over the WAN?
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
>|||I just realized...what about the default value? It has to apply that
default value to all the existing rows. Maybe that's what's taking a long
time. I wonder if disabling some level of logging during this update would
speed that up.
"ChrisR" <noemail@.bla.com> wrote in message
news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
> Yes it does. I just realized you are adding these to customer sires. Are
> you logging onto the boxes locally, or over the WAN?
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
>|||Yes, the defualt value will take a bit. But it doesnt seems to me like Ive
ever experienced that severe of slowness, which is what led to my other
questions. Creating with NULL will be much faster of course... but still
thats a long time. I guess try just to add with NULL for timing tests.
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:eLFnFbhZFHA.2520@.TK2MSFTNGP09.phx.gbl...
>I just realized...what about the default value? It has to apply that
>default value to all the existing rows. Maybe that's what's taking a long
>time. I wonder if disabling some level of logging during this update would
>speed that up.
> "ChrisR" <noemail@.bla.com> wrote in message
> news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
>|||On Tue, 31 May 2005 10:50:16 -0600, Joel Lyons wrote:
>Here's the script:
>ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
>DF_Images_Version DEFAULT 1
Hi Joel,
You're right (in your other post) - adding the default is what takes
time.
Consider breaking it in smaller steps:
1. Add a column, keep it nullable - should be fast.
ALTER TABLE dbo.Images
ADD Version tinyint
go
2. Fill the default value. This will take time, but if you use smallish
batches to process the data, locks will be released in between, so your
other applications will be able to proceed. If you have full recovery
model, then make sure that you back up the transaction log in between
(unless you don't mind it growing to accomodate all the changes).
SET ROWCOUNT 50000 -- experiment to get the optimal setting
DECLARE @.rc int
SET @.rc = 1
WHILE @.rc > 0
BEGIN
UPDATE dbo.Images
SET Version = 1
WHERE Version IS NULL
SET @.rc = @.@.ROWCOUNT
-- BACKUP LOG MyDatabase TO MyDatabaseLog
END
SET ROWCOUNT 0 -- never forget to reset ROWCOUNT!!
3. When all rows have been filled, set the NOT NULL constraint and the
DEFAULT:
ALTER TABLE dbo.Images
ALTER COLUMN Version tinyint NOT NULL
ALTER TABLE dbo.Images
ADD DEFAULT 1 FOR Version
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||When you allow nulls and do not provide a default value, it will be very
very quick, because the new rows are not materialized until the page is
re-written... It works sort of like a virtual column... when you provide a
default value, the values must ACTUALLY be written, so the time it would
take is relative to the IO speed.
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>|||Very interesting info - thanks all!
I'll try some of these ideas and let you know.
-Joel
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>
adding a new column to a large table
Windows 2000 Server
I need to add a new column to a table that will likely have millions of rows
at most customer sites. The table currently has one FK and 2 unclustered
indexes. The new column is an int column. When I run the script to add the
column on our test server (table has about 5 million rows) it takes about
1/2 hour. Unfortunately, we can't have that kind of down time on that
table.
Is there a typical method for doing this more efficiently? Would it help to
drop the indexes before and recreate them after or would that end up taking
the same amount of time anyway? What about creating a temp table, copying
that data into the new table and renaming it?Are you trying to add this column in the middle of the other columns, or at
the end of the other columns? If the middle, try adding to the end and see
if that sppeds things up, Im betting thats whats going on.
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>|||Here's the script:
ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
DF_Images_Version DEFAULT 1
I'd guess that adds to the end, no?
"ChrisR" <noemail@.bla.com> wrote in message
news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
> Are you trying to add this column in the middle of the other columns, or
> at the end of the other columns? If the middle, try adding to the end and
> see if that sppeds things up, Im betting thats whats going on.
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
>> SQL Server 2000
>> Windows 2000 Server
>> I need to add a new column to a table that will likely have millions of
>> rows at most customer sites. The table currently has one FK and 2
>> unclustered indexes. The new column is an int column. When I run the
>> script to add the column on our test server (table has about 5 million
>> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
>> down time on that table.
>> Is there a typical method for doing this more efficiently? Would it help
>> to drop the indexes before and recreate them after or would that end up
>> taking the same amount of time anyway? What about creating a temp table,
>> copying that data into the new table and renaming it?
>>
>|||Yes it does. I just realized you are adding these to customer sires. Are you
logging onto the boxes locally, or over the WAN?
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
> Here's the script:
> ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
> DF_Images_Version DEFAULT 1
> I'd guess that adds to the end, no?
> "ChrisR" <noemail@.bla.com> wrote in message
> news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
>> Are you trying to add this column in the middle of the other columns, or
>> at the end of the other columns? If the middle, try adding to the end and
>> see if that sppeds things up, Im betting thats whats going on.
>>
>> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
>> news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
>> SQL Server 2000
>> Windows 2000 Server
>> I need to add a new column to a table that will likely have millions of
>> rows at most customer sites. The table currently has one FK and 2
>> unclustered indexes. The new column is an int column. When I run the
>> script to add the column on our test server (table has about 5 million
>> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind
>> of down time on that table.
>> Is there a typical method for doing this more efficiently? Would it
>> help to drop the indexes before and recreate them after or would that
>> end up taking the same amount of time anyway? What about creating a
>> temp table, copying that data into the new table and renaming it?
>>
>>
>|||We haven't run the script on an actual customer site yet, only on our test
server which is a close resemblance. When we upgrade the customers, we will
likely remote in and run the script on the actual server.
(thanks for taking the time to help)
"ChrisR" <noemail@.bla.com> wrote in message
news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
> Yes it does. I just realized you are adding these to customer sires. Are
> you logging onto the boxes locally, or over the WAN?
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
>> Here's the script:
>> ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
>> DF_Images_Version DEFAULT 1
>> I'd guess that adds to the end, no?
>> "ChrisR" <noemail@.bla.com> wrote in message
>> news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
>> Are you trying to add this column in the middle of the other columns, or
>> at the end of the other columns? If the middle, try adding to the end
>> and see if that sppeds things up, Im betting thats whats going on.
>>
>> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
>> news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
>> SQL Server 2000
>> Windows 2000 Server
>> I need to add a new column to a table that will likely have millions of
>> rows at most customer sites. The table currently has one FK and 2
>> unclustered indexes. The new column is an int column. When I run the
>> script to add the column on our test server (table has about 5 million
>> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind
>> of down time on that table.
>> Is there a typical method for doing this more efficiently? Would it
>> help to drop the indexes before and recreate them after or would that
>> end up taking the same amount of time anyway? What about creating a
>> temp table, copying that data into the new table and renaming it?
>>
>>
>>
>|||I just realized...what about the default value? It has to apply that
default value to all the existing rows. Maybe that's what's taking a long
time. I wonder if disabling some level of logging during this update would
speed that up.
"ChrisR" <noemail@.bla.com> wrote in message
news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
> Yes it does. I just realized you are adding these to customer sires. Are
> you logging onto the boxes locally, or over the WAN?
>
> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
> news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
>> Here's the script:
>> ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
>> DF_Images_Version DEFAULT 1
>> I'd guess that adds to the end, no?
>> "ChrisR" <noemail@.bla.com> wrote in message
>> news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
>> Are you trying to add this column in the middle of the other columns, or
>> at the end of the other columns? If the middle, try adding to the end
>> and see if that sppeds things up, Im betting thats whats going on.
>>
>> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
>> news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
>> SQL Server 2000
>> Windows 2000 Server
>> I need to add a new column to a table that will likely have millions of
>> rows at most customer sites. The table currently has one FK and 2
>> unclustered indexes. The new column is an int column. When I run the
>> script to add the column on our test server (table has about 5 million
>> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind
>> of down time on that table.
>> Is there a typical method for doing this more efficiently? Would it
>> help to drop the indexes before and recreate them after or would that
>> end up taking the same amount of time anyway? What about creating a
>> temp table, copying that data into the new table and renaming it?
>>
>>
>>
>|||Yes, the defualt value will take a bit. But it doesnt seems to me like Ive
ever experienced that severe of slowness, which is what led to my other
questions. Creating with NULL will be much faster of course... but still
thats a long time. I guess try just to add with NULL for timing tests.
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:eLFnFbhZFHA.2520@.TK2MSFTNGP09.phx.gbl...
>I just realized...what about the default value? It has to apply that
>default value to all the existing rows. Maybe that's what's taking a long
>time. I wonder if disabling some level of logging during this update would
>speed that up.
> "ChrisR" <noemail@.bla.com> wrote in message
> news:uOYoAMgZFHA.580@.TK2MSFTNGP15.phx.gbl...
>> Yes it does. I just realized you are adding these to customer sires. Are
>> you logging onto the boxes locally, or over the WAN?
>>
>> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
>> news:utoFrFgZFHA.3876@.TK2MSFTNGP12.phx.gbl...
>> Here's the script:
>> ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
>> DF_Images_Version DEFAULT 1
>> I'd guess that adds to the end, no?
>> "ChrisR" <noemail@.bla.com> wrote in message
>> news:%23caxC6fZFHA.3364@.TK2MSFTNGP12.phx.gbl...
>> Are you trying to add this column in the middle of the other columns,
>> or at the end of the other columns? If the middle, try adding to the
>> end and see if that sppeds things up, Im betting thats whats going on.
>>
>> "Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
>> news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
>> SQL Server 2000
>> Windows 2000 Server
>> I need to add a new column to a table that will likely have millions
>> of rows at most customer sites. The table currently has one FK and 2
>> unclustered indexes. The new column is an int column. When I run the
>> script to add the column on our test server (table has about 5 million
>> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind
>> of down time on that table.
>> Is there a typical method for doing this more efficiently? Would it
>> help to drop the indexes before and recreate them after or would that
>> end up taking the same amount of time anyway? What about creating a
>> temp table, copying that data into the new table and renaming it?
>>
>>
>>
>>
>|||On Tue, 31 May 2005 10:50:16 -0600, Joel Lyons wrote:
>Here's the script:
>ALTER TABLE [dbo].[Images] ADD Version tinyint NOT NULL CONSTRAINT
>DF_Images_Version DEFAULT 1
Hi Joel,
You're right (in your other post) - adding the default is what takes
time.
Consider breaking it in smaller steps:
1. Add a column, keep it nullable - should be fast.
ALTER TABLE dbo.Images
ADD Version tinyint
go
2. Fill the default value. This will take time, but if you use smallish
batches to process the data, locks will be released in between, so your
other applications will be able to proceed. If you have full recovery
model, then make sure that you back up the transaction log in between
(unless you don't mind it growing to accomodate all the changes).
SET ROWCOUNT 50000 -- experiment to get the optimal setting
DECLARE @.rc int
SET @.rc = 1
WHILE @.rc > 0
BEGIN
UPDATE dbo.Images
SET Version = 1
WHERE Version IS NULL
SET @.rc = @.@.ROWCOUNT
-- BACKUP LOG MyDatabase TO MyDatabaseLog
END
SET ROWCOUNT 0 -- never forget to reset ROWCOUNT!!
3. When all rows have been filled, set the NOT NULL constraint and the
DEFAULT:
ALTER TABLE dbo.Images
ALTER COLUMN Version tinyint NOT NULL
ALTER TABLE dbo.Images
ADD DEFAULT 1 FOR Version
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||When you allow nulls and do not provide a default value, it will be very
very quick, because the new rows are not materialized until the page is
re-written... It works sort of like a virtual column... when you provide a
default value, the values must ACTUALLY be written, so the time it would
take is relative to the IO speed.
--
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>|||Very interesting info - thanks all!
I'll try some of these ideas and let you know.
-Joel
"Joel Lyons" <NOSPAMJOELL@.NOVARAD.NET> wrote in message
news:uyRcotfZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of
> rows at most customer sites. The table currently has one FK and 2
> unclustered indexes. The new column is an int column. When I run the
> script to add the column on our test server (table has about 5 million
> rows) it takes about 1/2 hour. Unfortunately, we can't have that kind of
> down time on that table.
> Is there a typical method for doing this more efficiently? Would it help
> to drop the indexes before and recreate them after or would that end up
> taking the same amount of time anyway? What about creating a temp table,
> copying that data into the new table and renaming it?
>|||Joel,
Are you adding the new column executing a script in QA or doing it manually
in EM?
Do it in QA, using "alter table".
AMB
"Joel Lyons" wrote:
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of rows
> at most customer sites. The table currently has one FK and 2 unclustered
> indexes. The new column is an int column. When I run the script to add the
> column on our test server (table has about 5 million rows) it takes about
> 1/2 hour. Unfortunately, we can't have that kind of down time on that
> table.
> Is there a typical method for doing this more efficiently? Would it help to
> drop the indexes before and recreate them after or would that end up taking
> the same amount of time anyway? What about creating a temp table, copying
> that data into the new table and renaming it?
>
>|||Could you please show the script you use to add a column ?
Regards.
"Joel Lyons" wrote:
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of rows
> at most customer sites. The table currently has one FK and 2 unclustered
> indexes. The new column is an int column. When I run the script to add the
> column on our test server (table has about 5 million rows) it takes about
> 1/2 hour. Unfortunately, we can't have that kind of down time on that
> table.
> Is there a typical method for doing this more efficiently? Would it help to
> drop the indexes before and recreate them after or would that end up taking
> the same amount of time anyway? What about creating a temp table, copying
> that data into the new table and renaming it?
>
>|||Hi Joel,
DEFAULT constraint in your script is not an issue,
default value will only apply on new records, not existent records in your
table.
The real issue is NOT NULL - this will require prety huge I/O.
What you can do, run your script to add a new column without NOT NULL.
This will let you use a new column and populate it.
Then when you will able to schedule a time, make it NOT NULL.
Regards.
"Joel Lyons" wrote:
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of rows
> at most customer sites. The table currently has one FK and 2 unclustered
> indexes. The new column is an int column. When I run the script to add the
> column on our test server (table has about 5 million rows) it takes about
> 1/2 hour. Unfortunately, we can't have that kind of down time on that
> table.
> Is there a typical method for doing this more efficiently? Would it help to
> drop the indexes before and recreate them after or would that end up taking
> the same amount of time anyway? What about creating a temp table, copying
> that data into the new table and renaming it?
>
>|||Hi Joel,
DEFAULT constraint in your script is not an issue,
default value will only apply on new records, not existent records in your
table.
The real issue is NOT NULL - this will require prety huge I/O.
What you can do, run your script to add a new column without NOT NULL.
This will let you use a new column and populate it.
Then when you will able to schedule a time, make it NOT NULL.
Regards.
"Joel Lyons" wrote:
> SQL Server 2000
> Windows 2000 Server
> I need to add a new column to a table that will likely have millions of rows
> at most customer sites. The table currently has one FK and 2 unclustered
> indexes. The new column is an int column. When I run the script to add the
> column on our test server (table has about 5 million rows) it takes about
> 1/2 hour. Unfortunately, we can't have that kind of down time on that
> table.
> Is there a typical method for doing this more efficiently? Would it help to
> drop the indexes before and recreate them after or would that end up taking
> the same amount of time anyway? What about creating a temp table, copying
> that data into the new table and renaming it?
>
>
Adding a new column is DBNULL
VS 2005
I am developing the database application. This is a live database and is being used by the customer. I am to release a new version and I had to add new columns to fit the requirements DateStarted (DateTime), and TotalHours(Int) into a database table.There are already over a 1000 rows in this table.
When the customer wants to look at a record in this table and insert the value into the text boxes (Front-end), when it gets to the DateStarted or TotalHours it comes up with a error message:"
The value for column 'DateStarted' in table 'IncidentTask' is DBNull."The method for inserting is:
Code Snippet
Me.dtDateStarted.Value = Me.DsIncidentsControl.IncidentTask(0).DateStarted
What are the possible solutions to this problem? Would it mean checking for a DBNULL before displaying in the textboxes? Or updating the new column rows with an date:
Code Snippet
UPDATE IncidentTask SET DateStarted = '1/1/2005' WHERE (DateStarted IS NULL)
Many thanks for any suggestions,
Steve
You can either have the application check for null values and display an empty string when dbnull,
OR,
UPDATE the table and put a 'dummy' date in all null rows.
Sunday, February 12, 2012
AD Groups
added their Active Directory group to the SQL logins but can not connect to
SQL 2000. For a test they added the individual domain user to an SQL login
and they can connect. For some reason they can not use their AD group to
connect to SQL 2000. Then they added local admin privledge to the AD group
and they can now connect. Do you have to have local admin privledge on an A
D
group to connet to SQL 2000? How can they remove that privledge and still
connect with thier AD group SQL login'
Thanks
RyanHi,
please refer following links :
www.sqlservercentral.com/forums/shw...and-SQL-2K.html
www.flatmtn.com/computer/Linux-LDAP.html
www.windowsitpro.com/SQLServer/Arti...841/pg/2/2.html
www.openldap.org/lists/openldap-sof...0/msg00304.html
http://forums.devshed.com/archive/t...s-ms-sql-server
:-)
Regards
Andy Davis
Activecrypt Team
---SQL Server Encryption Software
http://www.activecrypt.com
"Ryan" wrote:
> Our customer is using Windows Authentication to connect to SQL 2000. They
> added their Active Directory group to the SQL logins but can not connect t
o
> SQL 2000. For a test they added the individual domain user to an SQL logi
n
> and they can connect. For some reason they can not use their AD group to
> connect to SQL 2000. Then they added local admin privledge to the AD grou
p
> and they can now connect. Do you have to have local admin privledge on an
AD
> group to connet to SQL 2000? How can they remove that privledge and still
> connect with thier AD group SQL login'
> Thanks
> Ryan