{"id":342226,"date":"2022-12-06T15:02:11","date_gmt":"2022-12-06T15:02:11","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=342226"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2022-12-06T15:02:11","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=342226","title":{"rendered":"<span>\u041f\u0430\u043a\u0435\u0442\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f JDBC \u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 batchUpdate<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c \u043d\u0430 Java, \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0430\u043a\u0435\u0442\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b batchUpdate \u0438 \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u043d\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0441 update. \u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<p>\u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438:<\/p>\n<p>\u0421 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438:<\/p>\n<h3>12.4\u00a0JDBC batch operations<\/h3>\n<p><strong>Most JDBC drivers provide improved performance if you batch multiple calls to the same prepared statement. By grouping updates into batches you limit the number of round trips to the database. This section will cover batch processing using both the JdbcTemplate and the SimpleJdbcTemplate.<\/strong><\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u0434\u0430\u043d\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u044b JDBC \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0442 \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u043f\u0430\u043a\u0435\u0442\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430.\u00a0\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043f\u0430\u043a\u0435\u0442\u044b, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043b\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0437\u043d\u0430\u0442\u044c \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b.<\/p>\n<p>\u041a\u043e\u043c\u0443 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u0441\u0430\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0441\u0447\u0435\u0442\u0430 &#8212; Welcom \u0441\u0440\u0430\u0437\u0443 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u0442\u0430\u0442\u044c\u0438, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0430\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0410 \u043f\u043e\u043a\u0430 \u0437\u0430 \u0440\u0430\u0431\u043e\u0442\u0443.<\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 <a href=\"https:\/\/github.com\/mista1984gmail\/example-batch-update\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a><\/p>\n<p>\u042f \u0441\u043e\u0437\u0434\u0430\u043b spring-boot \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0430\u0439\u0442\u0430 <a href=\"https:\/\/start.spring.io\/\" rel=\"noopener noreferrer nofollow\">https:\/\/start.spring.io\/<\/a>. \u0421\u0442\u0430\u0440\u0442\u0435\u0440\u044b:<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-data-jpa&lt;\/artifactId> &lt;\/dependency> &lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-web&lt;\/artifactId> &lt;\/dependency><\/code><\/pre>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c postgres. \u041f\u043e\u044d\u0442\u043e\u043c\u0443:<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>org.postgresql&lt;\/groupId> &lt;artifactId>postgresql&lt;\/artifactId> &lt;scope>runtime&lt;\/scope> &lt;\/dependency><\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443:<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>com.github.javafaker&lt;\/groupId> &lt;artifactId>javafaker&lt;\/artifactId> &lt;version>1.0.2&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u0410 \u0434\u043b\u044f \u0437\u0430\u043c\u0435\u0440\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>org.apache.commons&lt;\/groupId> &lt;artifactId>commons-lang3&lt;\/artifactId> &lt;version>3.9&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u041a\u0430\u0436\u0434\u044b\u0439 \u0448\u0430\u0433 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0435\u043c\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u042f \u043a\u0430\u043a, \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b, \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c postgres. \u041e\u043d \u0443 \u043c\u0435\u043d\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e pgAdmin4 \u0441\u043e\u0437\u0434\u0430\u044e \u043d\u043e\u0432\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 test_db.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8e0\/faa\/861\/8e0faa86197a9aba9d67cb1d79974777.png\" width=\"1895\" height=\"795\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8e0\/faa\/861\/8e0faa86197a9aba9d67cb1d79974777.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432 IDEA \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e \u0434\u0430\u043d\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u0438 \u0441\u043e\u0437\u0434\u0430\u044e \u0432 \u043d\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 users. \u0421\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<pre><code class=\"sql\">CREATE TABLE users( user_id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, user_name varchar(100) NOT NULL, address varchar(255) NOT NULL, email varchar(100), telephone varchar(13) )<\/code><\/pre>\n<p>\u0418 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0432 application.properties \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e \u043a \u043d\u0430\u0448\u0435\u0439 \u0411\u0414.<\/p>\n<pre><code class=\"java\">spring.datasource.url=jdbc:postgresql:\/\/localhost:5432\/test_db spring.datasource.username=\/\/ \u0442\u0443\u0442 \u0412\u0430\u0448 username \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 spring.datasource.password=\/\/ \u0442\u0443\u0442 \u0412\u0430\u0448 \u043f\u0430\u0440\u043e\u043b\u044c \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u043c  spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.database=postgresql<\/code><\/pre>\n<p>\u0421 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438. \u0421\u0435\u0439\u0447\u0430\u0441 \u0437\u0430\u0439\u043c\u0435\u043c\u0441\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"java\">public class User {     private int user_id;     private String user_name;     private String address;     private String email;     private String telephone;       public User(String user_name, String address, String email, String telephone) {         this.user_name = user_name;         this.address = address;         this.email = email;         this.telephone = telephone;     }      public int getUser_id() {         return user_id;     }      public void setUser_id(int user_id) {         this.user_id = user_id;     }      public String getUser_name() {         return user_name;     }      public void setUser_name(String user_name) {         this.user_name = user_name;     }      public String getAddress() {         return address;     }      public void setAddress(String address) {         this.address = address;     }      public String getEmail() {         return email;     }      public void setEmail(String email) {         this.email = email;     }      public String getTelephone() {         return telephone;     }      public void setTelephone(String telephone) {         this.telephone = telephone;     }   }  <\/code><\/pre>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e \u043d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 UserService \u0434\u0432\u0443\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 void batchUpdateTest() \u0438 void multipleUpdateTest() &#8212; \u043f\u0435\u0440\u0432\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 batchUpdate, \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 &#8212; \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c update.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441 UserServiceImpl, \u0437\u0430\u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 UserService \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 batchUpdateTest():<\/p>\n<pre><code class=\"java\"> public void batchUpdateTest() {         List&lt;User> users= create100_000Users();         StopWatch stopWatch = new StopWatch();         stopWatch.start();         jdbcTemplate.batchUpdate(\"INSERT INTO Users (user_name, address, email, telephone) VALUES(?, ?, ?, ?)\", new BatchPreparedStatementSetter() {             @Override             public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {                 preparedStatement.setString(1, users.get(i).getUser_name());                 preparedStatement.setString(2, users.get(i).getAddress());                 preparedStatement.setString(3, users.get(i).getEmail());                 preparedStatement.setString(4, users.get(i).getTelephone());             }              @Override             public int getBatchSize() {                 return users.size();             }         });         stopWatch.stop();         System.out.println(\"Time has passed with batch update, ms: \" + stopWatch.getTime());     }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 multipleUpdateTest():<\/p>\n<pre><code class=\"java\">public void multipleUpdateTest() {         List&lt;User> users= create100_000Users();         StopWatch stopWatch = new StopWatch();         stopWatch.start();         for (User user: users) {             jdbcTemplate.update(\"INSERT INTO Users (user_name, address, email, telephone) VALUES(?, ?, ?, ?)\",                     user.getUser_name(),                     user.getAddress(),                     user.getEmail(),                     user.getTelephone());         }         stopWatch.stop();         System.out.println(\"Time has passed with update, ms: \" + stopWatch.getTime());     }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0435 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 10 000 Uses-\u043e\u0432:<\/p>\n<pre><code class=\"java\">public static List&lt;User> create100_000Users() {         List&lt;User> users = new ArrayList&lt;>();         Faker faker = new Faker();         for (int i = 0; i &lt; 10_000; i++) {             String firstName = faker.name().firstName();             String lastName = faker.name().lastName();             String sufixTel = String.valueOf(i);             String telephone = \"+000290000000\";             telephone = telephone.substring(0, telephone.length()-sufixTel.length()) + sufixTel;             User user = new User(                     firstName + \" \" + lastName,                     faker.address().fullAddress(),                     firstName.toLowerCase() + lastName.toLowerCase() + i + \"@gmail.com\",                     telephone);             users.add(user);         }         return users;     }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0432\u0441\u0435 \u044d\u0442\u043e \u0442\u044d\u0441\u0442\u0438\u0442\u044c.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"api\/v1\") public class UserController {     private final UserService userService;      @Autowired     public UserController(UserService userService) {         this.userService = userService;     }      @GetMapping(\"\/withBatchUpdate\")     @ResponseStatus(HttpStatus.OK)     public void withBatchUpdate() {         userService.batchUpdateTest();     }      @GetMapping(\"\/withUpdate\")     @ResponseStatus(HttpStatus.OK)     public void withoutBatchUpdate() {         userService.multipleUpdateTest();     } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u0431\u0443\u0434\u0443 \u043f\u043e\u0441\u044b\u043b\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e postmana.<\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/331\/9d0\/4ec\/3319d04ec2e953e29cc7480342cc931b.png\" width=\"1865\" height=\"948\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/331\/9d0\/4ec\/3319d04ec2e953e29cc7480342cc931b.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0434\u0435\u043c \u0432 postman \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 http:\/\/localhost:8080\/api\/v1\/withBatchUpdate \u0438 \u0436\u0434\u0435\u043c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7c0\/bd5\/9c1\/7c0bd59c1ce9eed7232b408d68bea157.png\" width=\"1618\" height=\"745\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7c0\/bd5\/9c1\/7c0bd59c1ce9eed7232b408d68bea157.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442: 196 \u043c\u0441.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/0bb\/212\/f27\/0bb212f27381e2fafaaf021b8fc49ca6.png\" width=\"1471\" height=\"434\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0bb\/212\/f27\/0bb212f27381e2fafaaf021b8fc49ca6.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0434\u0435\u043c \u0441\u043d\u043e\u0432\u0430 \u0432 postman \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 http:\/\/localhost:8080\/api\/v1\/withUpdate \u0438 \u0436\u0434\u0435\u043c.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2cd\/654\/3ef\/2cd6543ef43917aa945ac1d8ab8e75b6.png\" width=\"360\" height=\"35\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2cd\/654\/3ef\/2cd6543ef43917aa945ac1d8ab8e75b6.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442: 1932 \u043c\u0441.<\/p>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u0438\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0435\u0449\u0435 \u043f\u043e 2 \u0440\u0430\u0437\u0430 \u0438 \u0432\u043d\u0435\u0441\u0435\u043c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443.<\/p>\n<p>\u0420\u0430\u0441\u0447\u0435\u0442\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0438, \u0447\u0442\u043e \u043f\u0440\u0438 10 000 \u0432\u0441\u0442\u0430\u0432\u043e\u043a \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434 batchUpdate() \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0432 10,6 \u0440\u0430\u0437\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e update().<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/255\/c04\/438\/255c0443807d872c43020535882f1931.png\" width=\"402\" height=\"129\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/255\/c04\/438\/255c0443807d872c43020535882f1931.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0412\u0441\u0435\u043c \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. \u041f\u043e\u043a\u0430.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/703828\/\"> https:\/\/habr.com\/ru\/post\/703828\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c \u043d\u0430 Java, \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0430\u043a\u0435\u0442\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b batchUpdate \u0438 \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u043d\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0441 update. \u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<p>\u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438:<\/p>\n<p>\u0421 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438:<\/p>\n<h3>12.4\u00a0JDBC batch operations<\/h3>\n<p><strong>Most JDBC drivers provide improved performance if you batch multiple calls to the same prepared statement. By grouping updates into batches you limit the number of round trips to the database. This section will cover batch processing using both the JdbcTemplate and the SimpleJdbcTemplate.<\/strong><\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u044f\u0441\u044c \u043d\u0430 \u0434\u0430\u043d\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u044b JDBC \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0442 \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u043f\u0430\u043a\u0435\u0442\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430.\u00a0\u0413\u0440\u0443\u043f\u043f\u0438\u0440\u0443\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043f\u0430\u043a\u0435\u0442\u044b, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043b\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0443\u0437\u043d\u0430\u0442\u044c \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b.<\/p>\n<p>\u041a\u043e\u043c\u0443 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u0441\u0430\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0430\u0441\u0447\u0435\u0442\u0430 &#8212; Welcom \u0441\u0440\u0430\u0437\u0443 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u0442\u0430\u0442\u044c\u0438, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0430\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0410 \u043f\u043e\u043a\u0430 \u0437\u0430 \u0440\u0430\u0431\u043e\u0442\u0443.<\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 <a href=\"https:\/\/github.com\/mista1984gmail\/example-batch-update\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a><\/p>\n<p>\u042f \u0441\u043e\u0437\u0434\u0430\u043b spring-boot \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0430\u0439\u0442\u0430 <a href=\"https:\/\/start.spring.io\/\" rel=\"noopener noreferrer nofollow\">https:\/\/start.spring.io\/<\/a>. \u0421\u0442\u0430\u0440\u0442\u0435\u0440\u044b:<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-data-jpa&lt;\/artifactId> &lt;\/dependency> &lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-web&lt;\/artifactId> &lt;\/dependency><\/code><\/pre>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c postgres. \u041f\u043e\u044d\u0442\u043e\u043c\u0443:<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>org.postgresql&lt;\/groupId> &lt;artifactId>postgresql&lt;\/artifactId> &lt;scope>runtime&lt;\/scope> &lt;\/dependency><\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443:<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>com.github.javafaker&lt;\/groupId> &lt;artifactId>javafaker&lt;\/artifactId> &lt;version>1.0.2&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u0410 \u0434\u043b\u044f \u0437\u0430\u043c\u0435\u0440\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u0435\u0442\u043e\u0434\u0430:<\/p>\n<pre><code class=\"java\">&lt;dependency> &lt;groupId>org.apache.commons&lt;\/groupId> &lt;artifactId>commons-lang3&lt;\/artifactId> &lt;version>3.9&lt;\/version> &lt;\/dependency><\/code><\/pre>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445. \u041a\u0430\u0436\u0434\u044b\u0439 \u0448\u0430\u0433 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u044f \u043d\u0435 \u0431\u0443\u0434\u0443 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c, \u0442\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u043e \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0435\u043c\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438. \u042f \u043a\u0430\u043a, \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b, \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c postgres. \u041e\u043d \u0443 \u043c\u0435\u043d\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e pgAdmin4 \u0441\u043e\u0437\u0434\u0430\u044e \u043d\u043e\u0432\u0443\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 test_db.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432 IDEA \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e \u0434\u0430\u043d\u043d\u0443\u044e \u0431\u0430\u0437\u0443 \u0438 \u0441\u043e\u0437\u0434\u0430\u044e \u0432 \u043d\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 users. \u0421\u043a\u0440\u0438\u043f\u0442 \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<pre><code class=\"sql\">CREATE TABLE users( user_id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, user_name varchar(100) NOT NULL, address varchar(255) NOT NULL, email varchar(100), telephone varchar(13) )<\/code><\/pre>\n<p>\u0418 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0432 application.properties \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044e \u043a \u043d\u0430\u0448\u0435\u0439 \u0411\u0414.<\/p>\n<pre><code class=\"java\">spring.datasource.url=jdbc:postgresql:\/\/localhost:5432\/test_db spring.datasource.username=\/\/ \u0442\u0443\u0442 \u0412\u0430\u0448 username \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 spring.datasource.password=\/\/ \u0442\u0443\u0442 \u0412\u0430\u0448 \u043f\u0430\u0440\u043e\u043b\u044c \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u043c  spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.database=postgresql<\/code><\/pre>\n<p>\u0421 \u0431\u0430\u0437\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0438. \u0421\u0435\u0439\u0447\u0430\u0441 \u0437\u0430\u0439\u043c\u0435\u043c\u0441\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<pre><code class=\"java\">public class User {     private int user_id;     private String user_name;     private String address;     private String email;     private String telephone;       public User(String user_name, String address, String email, String telephone) {         this.user_name = user_name;         this.address = address;         this.email = email;         this.telephone = telephone;     }      public int getUser_id() {         return user_id;     }      public void setUser_id(int user_id) {         this.user_id = user_id;     }      public String getUser_name() {         return user_name;     }      public void setUser_name(String user_name) {         this.user_name = user_name;     }      public String getAddress() {         return address;     }      public void setAddress(String address) {         this.address = address;     }      public String getEmail() {         return email;     }      public void setEmail(String email) {         this.email = email;     }      public String getTelephone() {         return telephone;     }      public void setTelephone(String telephone) {         this.telephone = telephone;     }   }  <\/code><\/pre>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e \u043d\u0430\u043a\u043e\u043d\u0435\u0446-\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 UserService \u0434\u0432\u0443\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 void batchUpdateTest() \u0438 void multipleUpdateTest() &#8212; \u043f\u0435\u0440\u0432\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 batchUpdate, \u0430 \u0432\u0442\u043e\u0440\u043e\u0439 &#8212; \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c update.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0435\u0440\u0432\u0438\u0441 UserServiceImpl, \u0437\u0430\u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 UserService \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0434\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430.<\/p>\n<p>\u041c\u0435\u0442\u043e\u0434 batchUpdateTest():<\/p>\n<pre><code class=\"java\"> public void batchUpdateTest() {         List&lt;User> users= create100_000Users();         StopWatch stopWatch = new StopWatch();         stopWatch.start();         jdbcTemplate.batchUpdate(\"INSERT INTO Users (user_name, address, email, telephone) VALUES(?, ?, ?, ?)\", new BatchPreparedStatementSetter() {             @Override             public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {                 preparedStatement.setString(1, users.get(i).getUser_name());                 preparedStatement.setString(2, users.get(i).getAddress());                 preparedStatement.setString(3, users.get(i).getEmail());                 preparedStatement.setString(4, users.get(i).getTelephone());             }              @Override             public int getBatchSize() {                 return users.size();             }         });         stopWatch.stop();         System.out.println(\"Time has passed with batch update, ms: \" + stopWatch.getTime());     }<\/code><\/pre>\n<p>\u041c\u0435\u0442\u043e\u0434 multipleUpdateTest():<\/p>\n<pre><code class=\"java\">public void multipleUpdateTest() {         List&lt;User> users= create100_000Users();         StopWatch stopWatch = new StopWatch();         stopWatch.start();         for (User user: users) {             jdbcTemplate.update(\"INSERT INTO Users (user_name, address, email, telephone) VALUES(?, ?, ?, ?)\",                     user.getUser_name(),                     user.getAddress(),                     user.getEmail(),                     user.getTelephone());         }         stopWatch.stop();         System.out.println(\"Time has passed with update, ms: \" + stopWatch.getTime());     }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0435 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 10 000 Uses-\u043e\u0432:<\/p>\n<pre><code class=\"java\">public static List&lt;User> create100_000Users() {         List&lt;User> users = new ArrayList&lt;>();         Faker faker = new Faker();         for (int i = 0; i &lt; 10_000; i++) {             String firstName = faker.name().firstName();             String lastName = faker.name().lastName();             String sufixTel = String.valueOf(i);             String telephone = \"+000290000000\";             telephone = telephone.substring(0, telephone.length()-sufixTel.length()) + sufixTel;             User user = new User(                     firstName + \" \" + lastName,                     faker.address().fullAddress(),                     firstName.toLowerCase() + lastName.toLowerCase() + i + \"@gmail.com\",                     telephone);             users.add(user);         }         return users;     }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0432\u0441\u0435 \u044d\u0442\u043e \u0442\u044d\u0441\u0442\u0438\u0442\u044c.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"api\/v1\") public class UserController {     private final UserService userService;      @Autowired     public UserController(UserService userService) {         this.userService = userService;     }      @GetMapping(\"\/withBatchUpdate\")     @ResponseStatus(HttpStatus.OK)     public void withBatchUpdate() {         userService.batchUpdateTest();     }      @GetMapping(\"\/withUpdate\")     @ResponseStatus(HttpStatus.OK)     public void withoutBatchUpdate() {         userService.multipleUpdateTest();     } }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u0431\u0443\u0434\u0443 \u043f\u043e\u0441\u044b\u043b\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e postmana.<\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0434\u0435\u043c \u0432 postman \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 http:\/\/localhost:8080\/api\/v1\/withBatchUpdate \u0438 \u0436\u0434\u0435\u043c.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442: 196 \u043c\u0441.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0434\u0435\u043c \u0441\u043d\u043e\u0432\u0430 \u0432 postman \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 http:\/\/localhost:8080\/api\/v1\/withUpdate \u0438 \u0436\u0434\u0435\u043c.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442: 1932 \u043c\u0441.<\/p>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u0438\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0435\u0449\u0435 \u043f\u043e 2 \u0440\u0430\u0437\u0430 \u0438 \u0432\u043d\u0435\u0441\u0435\u043c \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443.<\/p>\n<p>\u0420\u0430\u0441\u0447\u0435\u0442\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u043b\u0438, \u0447\u0442\u043e \u043f\u0440\u0438 10 000 \u0432\u0441\u0442\u0430\u0432\u043e\u043a \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434 batchUpdate() \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b \u0432 10,6 \u0440\u0430\u0437\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e update().<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0412\u0441\u0435\u043c \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043a \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435. \u041f\u043e\u043a\u0430.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/703828\/\"> https:\/\/habr.com\/ru\/post\/703828\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-342226","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/342226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342226"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/342226\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}