Batch processing for MySQL

Source: Internet
Author: User

MySQL turns off batching by default, so we store 10,000 of data in the database by default (batch is not open), with the following core code:

 PackageCn.itcast.demo5;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.SQLException;Importorg.junit.Test;Importcn.itcast.demo3.JdbcUtils; Public classDemo5 {@Test Public voidFun5 ()throwsSQLException {/** PSTMT: * > Add parameters to the batch * > Execute batch! */Connection con=jdbcutils.getconnection (); String SQL= "INSERT into T_stu VALUES (?,?,?,?)"; PreparedStatement pstmt=con.preparestatement (SQL); //crazy to add parameters         for(inti = 0; I < 10000; i++) {Pstmt.setint (1, i+1); Pstmt.setstring (2, "stu_" +i); Pstmt.setint (3, i); Pstmt.setstring (4, i%2==0? " Male ":" Female "); Pstmt.addbatch ();//Add Batch! This set of parameters is saved to the collection.         }        LongStart =System.currenttimemillis (); Pstmt.executebatch ();//Execute the batch!         LongEnd =System.currenttimemillis (); System.out.println (End-start);    }}

The above program execution end time consuming 412764MS

This is the batch that opens MySQL, open in the way:

Url=jdbc:mysql://localhost:3306/exam the MySQL parameter

Change to Url=jdbc:mysql://localhost:3306/exam?rewritebatchedstatements=true

Execute the program again, time consuming 301MS, speed 1000 times times faster than!

Batch processing for MySQL

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.