all groups > sql server new users > april 2007 >
You're in the

sql server new users

group:

Inserting large amount of rows?


Inserting large amount of rows? Usenet User
4/9/2007 10:01:53 AM
sql server new users: Environment: SQL 2000 Standard SP4
Database recovery mode: Simple

I have a stored proc that once a day transfers a large amount of data
(2+ million rows usually) from one table to another as follows:

INSERT INTO table1
SELECT * FROM table2

DROP table2

This doesn't scale well obviously, and quite often the above operation
results in "could not obtain lock" error.

Could someone suggest, please, some less brutal way of copying rows,
like in batches, so example? None of the tables have primary keys or
identity columns, by the way.

Re: Inserting large amount of rows? SQL Menace
4/9/2007 10:32:46 AM
[quoted text, click to view]

What about BCP OUT and then BCP IN or BULK INSERT (this is what is
called a minimally logged operation)

[quoted text, click to view]
Can't wait to hear the Celko answer on this one ;-)
How are you going to do a batch if you don't have a unique identifier?


Denis the SQL Menace
http://sqlservercode.blogspot.com/

Re: Inserting large amount of rows? SQL Menace
4/9/2007 11:01:07 AM
On Apr 9, 1:42 pm, "Aaron Bertrand [SQL Server MVP]"
[quoted text, click to view]

I think he throws in "by definition" somewhere in that sentence ;-)

Denis the SQL Menace
http://sqlservercode.blogspot.com/
Re: Inserting large amount of rows? Steve Dassin
4/9/2007 12:37:16 PM
[quoted text, click to view]

You might be better off waiting for Godow.









:)

Re: Inserting large amount of rows? Steve Dassin
4/9/2007 12:48:10 PM
[quoted text, click to view]

Good catch, you caught me thinking of shoes:( :)

[quoted text, click to view]

Does that make you a 'bag' boy?

Re: Inserting large amount of rows? Awi Ktir
4/9/2007 1:09:20 PM
try to select from both tables into a third one.

select * into Table2 from
(select * from table1
UNION
select * from Table2)
Drop table1
Drop table2
sp_rename 'table3', 'table1', object

see if it makes difference.




[quoted text, click to view]

Re: Inserting large amount of rows? Aaron Bertrand [SQL Server MVP]
4/9/2007 1:42:23 PM
[quoted text, click to view]

He would say, "then you don't have any tables!"

Re: Inserting large amount of rows? Aaron Bertrand [SQL Server MVP]
4/9/2007 1:57:03 PM
Actually, he would still say the same thing, even if all the tables *did*
have identity columns as the primary key. :-)



[quoted text, click to view]

Re: Inserting large amount of rows? Aaron Bertrand [SQL Server MVP]
4/9/2007 3:42:11 PM
[quoted text, click to view]

Did you mean Godot?

Re: Inserting large amount of rows? Usenet User
4/9/2007 5:07:23 PM
On 9 Apr 2007 10:32:46 -0700, "SQL Menace" <denis.gobo@gmail.com>
[quoted text, click to view]

Ok, I agree. Now, suppose I have an identity column included in
table2. How would I go about doing the above task in batches?

Re: Inserting large amount of rows? Andrew J. Kelly
4/9/2007 9:25:01 PM
That by itself does not create a minimally logged operation. There are 5
conditions that must be met to get a minimally logged bulk copy as shown in
BooksOnLine under "minimally logged bulk copy".

--
Andrew J. Kelly SQL MVP

[quoted text, click to view]

Re: Inserting large amount of rows? Andrew J. Kelly
4/9/2007 9:31:34 PM
I seriously recommend you look at upgrading to SQL 2005 and use partitioning
to handle this. With partitioning you would not have to copy a single row
under the right conditions. But currently if you have an identity column you
could create a clustered index on it and do the inserts in smaller bacthes
of say 10K at a time.


SET NOCOUNT ON

DECLARE @Total INT, @Counter INT, @Loops INT
DECLARE @Begin INT, @End INT
SET @Begin = 1
SET @End = 10000
SET @Counter = 1

SELECT @Total = MAX(R_ID)
FROM Test3

SET @Total = 150000 -- For testing purposes

-- See how many loops we need to make by dividing the total rows by the
-- amount we will work on in each pass
SET @Loops = (@Total / 10000)


WHILE @Counter <= @Loops
BEGIN

SET @StartTime = GETDATE()

INSERT INTO [dbo].[TestInserts]
([Sub_ID],[LName],[MDate])
SELECT [Sub_ID],[LName],[MDate]
FROM TEST3 WITH (NOLOCK) -- or TABLOCK **
WHERE R_ID BETWEEN @Begin AND @End

SET @Begin = @Begin + 10000
SET @End = @End + 10000

SET @Counter = @Counter + 1
-- WAITFOR DELAY '00:00:01'

END


--
Andrew J. Kelly SQL MVP

[quoted text, click to view]

AddThis Social Bookmark Button