Spring JdbcTemplate The problem of slow batch update

Source: Internet
Author: User
Tags mongodb mysql in postgresql redis

Because many INSERT statements (5w) are executed at a time, it is common to write native insert statements through MySQL in a similar format:











Insert into TABLEAAA (F1,F2) VALUES (F11V,F21V), (f12v,f22v) ...


Execution speed is no problem, about 5, 6 seconds. In the use of JdbcTemplate, is to use the BatchUpdate method to write batch execution statements:








String sql = "INSERT into code (ID,CODE,STATUS,TIME) VALUES (?,?,?,?)";
Jdbctemplate.batchupdate (SQL, codes, Codes.size (), new parameterizedpreparedstatementsetter<couponcode> () {
    @Override public
    void Setvalues (PreparedStatement preparedstatement, Couponcode code) throws SQLException {
        preparedstatement.setint (1, Code.getcid ());
        Preparedstatement.setstring (2, Code.getcode ());
        Preparedstatement.setint (3, Code.getstatus ());
        Preparedstatement.setdate (4, New Date (New Java.util.Date (). GetTime ()));
    }
);
When executing the statement, it takes more than 10 seconds to complete the execution, which is much worse than the native SQL statement. So consult others, and finally found that the database connection statement is less enabled rewritebatchedstatements parameters. JdbcTemplate default in the BatchUpdate method, is an execution statement of one article. This can be seen in his driver code:













So on the connection string, add



jdbc:mysql://ip:port/database?useunicode=true&characterencoding=utf-8&autoreconnect=true& Rewritebatchedstatements=true



Plus, reboot Tomcat and find that the batch execution is fast.


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

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.