Fast BULK INSERT Data in Java

Source: Internet
Author: User
Tags bulk insert

As the project prepares to go online, a large amount of test data is prepared to test the performance of the new project, but it is less likely to use stored procedures and database transactions for data insertion.

The first use of the simplest Java for Loop statement keeps the Add object, but found that the efficiency is too low, to insert millions of of data estimates a day, and then found on the Internet, find a direct use of JDBC directly into the data, take down to try to find performance although better than the new object, But the efficiency is also very low.

Can only find, finally found a very high, that is, using the batch submission method of SQL execution, this 1 million data is about 30 seconds or so.



[Java] View Plain copy print? Public static void insertrelease ()  {            long begin = new date () getTime ();            String sql =  "insert into tb_big_data  (Count, create_time,  random)  VALUES  (?,  sysdate (),  ?) ";            try {                conn.setautocommit (false);                preparedstatement pst = conn.preparestatement ( SQL);               for  (int i  = 1; i <= 100; i++)  {                    for  (int k = 1; k <= 10000; k++)  {                         pst.setlong (1, k * i);                        pst.setlong (2, k *  i);                        pst.addbatch ();                    }                    pst.executebatch ();                    conn.commit ();                }                pst.close ();                Conn.close ();           } catch  (sqlexception  e)  {               e.printstacktrace ();           }            long end = new date () getTime ();            system.out.println ("cast : "  +  (end - begin)  /  1000 +  "s");       }  
Original address: http://blog.csdn.net/frinder/article/details/38830723

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.