Tristan St-Cyr
Tristan St-Cyr

Reputation: 143

What's the most efficient way to bulk-copy to SQL Server from Java?

I have data that is streamed from disk and processed in memory by a Java application and that finally needs to be copied into SQL Server. The data can be fairly large (hence the streaming) and can require up to several 100,000 rows to be inserted. The fastest solution seems to be using SQL Server's bulk-copy feature. However, I haven't found any way for Java programs to do this easily or nearly fast enough.

Here are some ways that I've already investigated:

I'm looking for the fastest solution. Memory is not an issue.

Thanks!

Upvotes: 8

Views: 5000

Answers (3)

Christian d'Heureuse
Christian d'Heureuse

Reputation: 5630

Since version 4.2 of the Microsoft JDBC driver for SQL Server, there is a class named com.microsoft.sqlserver.jdbc.SQLServerBulkCopy which does the same as the SqlBulkCopy class of .NET.

Upvotes: 1

SAsInSumit
SAsInSumit

Reputation: 21

The best option for me was to use the commercial SQL Server JDBC driver from DataDirect with standard JDBC calls addBatch/executeBatch that run across Linux and Windows - https://blogs.datadirect.com/2012/05/how-to-bulk-insert-jdbc-batches-into-microsoft-sql-server-oracle-sybase.html

I've seen load times improve from 7 hours to under 30 minutes.

Upvotes: 0

Horcrux7
Horcrux7

Reputation: 24457

  • For the .NET answer i would recommended IKVM. Then your Java Code will be .NET code and you can call any .NET code.
  • The BULK INSERT required also that the bulk file is accessible from SQL Server. This is only a local option. The performance from Batch Update can be vary between different JDBC drivers.
  • For native calls I would recommended to use JNA (Java native access). Then you does not need to write any C code.

Upvotes: 1

Related Questions