Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete All / Bulk Insert

First off let me say I am running on SQL Server 2005 so I don't have access to MERGE.

I have a table with ~150k rows that I am updating daily from a text file. As rows fall out of the text file I need to delete them from the database and if they change or are new I need to update/insert accordingly.

After some testing I've found that performance wise it is exponentially faster to do a full delete and then bulk insert from the text file rather than read through the file line by line doing an update/insert. However I recently came across some posts discussing mimicking the MERGE functionality of SQL Server 2008 using a temp table and the output of the UPDATE statement.

I was interested in this because I am looking into how I can eliminate the time in my Delete/Bulk Insert method when the table has no rows. I still think that this method will be the fastest so I am looking for the best way to solve the empty table problem.

Thanks

like image 381
rpf3 Avatar asked Oct 26 '10 14:10

rpf3


People also ask

How do you bulk delete rows in SQL?

There are a few ways to delete multiple rows in a table. If you wanted to delete a number of rows within a range, you can use the AND operator with the BETWEEN operator. DELETE FROM table_name WHERE column_name BETWEEN value 1 AND value 2; Another way to delete multiple rows is to use the IN operator.

How delete all data from multiple tables in SQL?

DELETE is a query that deletes one or multiple records from a table in SQL Server. With the statement, it is also possible to delete all data or specific data based on a condition specified in the WHERE clause. It is recommended to be careful and attentive while using DELETE , as it removes data permanently.

What is bulk insert?

A Bulk insert is a process or method provided by a database management system to load multiple rows of data into a database table. Bulk insert may refer to: Transact-SQL BULK INSERT statement. PL/SQL BULK COLLECT and FORALL statements. MySQL LOAD DATA INFILE statement.


2 Answers

I think your fastest method would be to:

  1. Drop all foreign keys and indexes from your table.
  2. Truncate your table.
  3. Bulk insert your data.
  4. Recreate your foreign keys and indexes.
like image 131
Joe Stefanelli Avatar answered Sep 18 '22 01:09

Joe Stefanelli


Is the problem that Joe's solution is not fast enough, or that you can not have any activity against the target table while your process runs? If you just need to prevent users from running queries against your target table, you should contain your process within a transaction block. This way, when your TRUNCATE TABLE executes, it will create a table lock that will be held for the duration of the transaction, like so:

begin tran;

truncate table stage_table

bulk insert stage_table
from N'C:\datafile.txt'

commit tran;
like image 24
Mike Fal Avatar answered Sep 19 '22 01:09

Mike Fal