Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way for inserting very large number of records into a Table in SQL

The problem is, we have a huge number of records (more than a million) to be inserted into a single table from a Java application. The records are created by the Java code, it's not a move from another table, so INSERT/SELECT won't help.

Currently, my bottleneck is the INSERT statements. I'm using PreparedStatement to speed-up the process, but I can't get more than 50 recods per second on a normal server. The table is not complicated at all, and there are no indexes defined on it.

The process takes too long, and the time it takes will make problems.

What can I do to get the maximum speed (INSERT per second) possible?

Database: MS SQL 2008. Application: Java-based, using Microsoft JDBC driver.

like image 835
Iravanchi Avatar asked May 04 '10 14:05

Iravanchi


People also ask

How can I speed up bulk insert in SQL?

Below are some good ways to improve BULK INSERT operations : Using TABLOCK as query hint. Dropping Indexes during Bulk Load operation and then once it is completed then recreating them. Changing the Recovery model of database to be BULK_LOGGED during the load operation.

What is the best and fast way to insert 2 million rows of data into SQL Server?

You can try with SqlBulkCopy class. Lets you efficiently bulk load a SQL Server table with data from another source.

How insert large data into table in SQL?

The basic syntax for bulk importing data is: INSERT ... SELECT * FROM OPENROWSET(BULK...) When used in an INSERT statement, OPENROWSET(BULK...)

How can I insert more than 1000 rows in SQL Server?

A table can store upto 1000 rows in one insert statement. If a user want to insert multiple rows at a time, the following syntax has to written. If a user wants to insert more than 1000 rows, multiple insert statements, bulk insert or derived table must be used.


5 Answers

Batch the inserts. That is, only send 1000 rows at a time, rather then one row at a time, so you hugely reduce round trips/server calls

Performing Batch Operations on MSDN for the JDBC driver. This is the easiest method without reengineering to use genuine bulk methods.

Each insert must be parsed and compiled and executed. A batch will mean a lot less parsing/compiling because a 1000 (for example) inserts will be compiled in one go

There are better ways, but this works if you are limited to generated INSERTs

like image 92
gbn Avatar answered Oct 22 '22 20:10

gbn


Use BULK INSERT - it is designed for exactly what you are asking and significantly increases the speed of inserts.

Also, (just in case you really do have no indexes) you may also want to consider adding an indexes - some indexes (most an index one on the primary key) may improve the performance of inserts.

The actual rate at which you should be able to insert records will depend on the exact data, the table structure and also on the hardware / configuration of the SQL server itself, so I can't really give you any numbers.

like image 34
Justin Avatar answered Oct 22 '22 22:10

Justin


Have you looked into bulk operations bulk operations?

like image 29
FrustratedWithFormsDesigner Avatar answered Oct 22 '22 22:10

FrustratedWithFormsDesigner


Have you considered to use batch updates?

like image 21
Manolo Santos Avatar answered Oct 22 '22 22:10

Manolo Santos


Is there any integrity constraint or trigger on the table ? If so, droping it before inserts will help, but you have to be sure that you can afford the consequences.

like image 43
Rostislav Matl Avatar answered Oct 22 '22 21:10

Rostislav Matl