![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
guys, I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will take those date ranges to INSERT records to a table in another database, then delete the records, but it will take really long time to finish this action (up to 1 or 2 hours). My question is if there is some other way I should do to speed up the action, I am thinking about use bcp to copy those records to datafile and then use bcp to insert it into SQL Server table. Is this the right way to do it or should I consider other solution (then, what is the solution.) Thanks a lot! |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will take those date ranges to INSERT records to a table in another database, then delete the records, but it will take really long time to finish this action (up to 1 or 2 hours). |
#5
| |||
| |||
|
|
Yes, BCP will be a good option for fast data transfer. All of the BULK operations (BULK INSERT, SELECT INTO, BCP) are minimally logged when a non FULL recovery model is set. Another issue could be the purging of the archived records from your main table. If you have it as a single DELETE and it takes long time to complete, then you can break it into smaller DELETE chunks. If you have SQL Server 2005 Enterprise Edition, an interesting alternative is to use partitioned tables. Specifically range partitions based on date ranges (in your case could be weekly) can help with archiving. Take a look at the following article (in particular the section about Range Partitions):http://msdn2.microsoft.com/en-us/library/ms345146.aspx HTH, Plamen Ratchevhttp://www.SQLStudio.com |
#6
| |||
| |||
|
|
Lee (lee.jenkins... (AT) gmail (DOT) com) writes: I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will take those date ranges to INSERT records to a table in another database, then delete the records, but it will take really long time to finish this action (up to 1 or 2 hours). It shouldn't take 1-2 hours to move 100.000 rows. It sounds like the process is not well implemented, or that there are indexes missing. Yes, you can gain speed by using BCP, but you also add complexity to the solution that I can't really see should be needed with the volumes you indicate? Would it be possible for you to post the definition of the tables, including indexes and the stored procedure? -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#7
| |||
| |||
|
|
Erland, Thanks a lot for the reply, also forgot to say thanks to Brad, Here is the table: CREATE TABLE [dbo].[tbl_record]( [record_id] [int] IDENTITY(1,1) NOT NULL, [record_CC_id] [int] NOT NULL, [record_content] [varchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_tbl_record_record_content] DEFAULT (''), [record_date] [datetime] NOT NULL CONSTRAINT [DF_tbl_record_record_date] DEFAULT (getdate()), [record_ip] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_tbl_record_record_ip] DEFAULT (''), [record_active] [bit] NOT NULL CONSTRAINT [DF_tbl_record_record_archive] DEFAULT (1), CONSTRAINT [PK_tbl_record] PRIMARY KEY CLUSTERED ( [record_id] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, FILLFACTOR = 90) ON [PRIMARY] ) ON [PRIMARY] And The stored procedure is here: ALTER PROCEDURE [dbo].[ArchiveRecords] ( @ddate datetime ) AS BEGIN TRAN SET IDENTITY_INSERT record_archive.dbo.tbl_record_archive ON; INSERT INTO record_archive.dbo.tbl_record_archive ( record_id, record_CC_id, record_content, record_date, record_ip, record_active ) SELECT record_id, record_CC_id, record_content, record_date, record_ip, record_active FROM tbl_record WHERE record_date <= @ddate; DELETE FROM tbl_record WHERE record_date <= @ddate; SET IDENTITY_INSERT record_archive.dbo.tbl_record_archive OFF; IF @@ERROR = 0 BEGIN COMMIT TRAN END ELSE BEGIN ROLLBACK TRAN END On Apr 23, 2:31 pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote: Lee (lee.jenkins... (AT) gmail (DOT) com) writes: I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will take those date ranges to INSERT records to a table in another database, then delete the records, but it will take really long time to finish this action (up to 1 or 2 hours). It shouldn't take 1-2 hours to move 100.000 rows. It sounds like the process is not well implemented, or that there are indexes missing. Yes, you can gain speed by using BCP, but you also add complexity to the solution that I can't really see should be needed with the volumes you indicate? Would it be possible for you to post the definition of the tables, including indexes and the stored procedure? -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx- Hide quoted text - - Show quoted text - |
#8
| |||
| |||
|
|
On Apr 23, 2:23 pm, Lee <lee.jenkins... (AT) gmail (DOT) com> wrote: guys, I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will take those date ranges to INSERT records to a table in another database, then delete the records, but it will take really long time to finish this action (up to 1 or 2 hours). My question is if there is some other way I should do to speed up the action, I am thinking about use bcp to copy those records to datafile and then use bcp to insert it into SQL Server table. Is this the right way to do it or should I consider other solution (then, what is the solution.) Thanks a lot! Use a Select Into statement and make sure the destination db is set to a simple recovery model.- Hide quoted text - - Show quoted text - |
#9
| |||
| |||
|
|
Should I remove the clusterd index on the record_id field and create nonclustered index on this field and create a clustered index on record_date field since in my query, I always select a range of data by date. |
![]() |
| Thread Tools | |
| Display Modes | |
| |