2011年11月14日 星期一

Copy function of PostgreSQL

One of my projects need to insert million records into database (postgreSQL). The performance becomes the key factor. Using the traditional insert statement will take a few days!

We are lucky that postgreSQL provides a very useful function for bulk insert.

import org.postgresql.copy.CopyManager; import org.postgresql.core.BaseConnection; import java.sql.Connection; StringBuilder csvContent = new StringBuilder(); //create your csv content here, or read from a file. Connection conn = //get your connection from datasource CopyManager copyManager = new CopyManager((BaseConnection) conn); long affected = copyManager.copyIn("COPY table FROM STDIN WITH csv header", new ByteArrayInputStream(csvContent.toString().getBytes())); LOG.debug("Copy from csv completed. Insert " + affected + " records.");

沒有留言:

張貼留言