当前位置: 首页 > news >正文

搜狗网站收录广州网站建设推广服务

搜狗网站收录,广州网站建设推广服务,程序员做音乐网站,上海官方最新消息我们可以看到 Statement 和 PreparedStatement 为我们提供的批次执行 sql 操作 JDBC 引入上述 batch 功能的主要目的#xff0c;是加快对客户端SQL的执行和响应速度#xff0c;并进而提高数据库整体并发度#xff0c;而 jdbc batch 能够提高对客户端SQL的执行和响应速度,其… 我们可以看到 Statement 和 PreparedStatement 为我们提供的批次执行 sql 操作 JDBC 引入上述 batch 功能的主要目的是加快对客户端SQL的执行和响应速度并进而提高数据库整体并发度而 jdbc batch 能够提高对客户端SQL的执行和响应速度,其主要原理有 减少了JDBC客户端和数据库服务器之间网络传输的开销使用 batch 功能前每提交一个SQL都需要一次网络IO开销且提交后需要等待服务端返回结果后才能提交下一个SQL而使用 batch 功能后客户端的多个SQL是一起提交给服务器的只涉及到一次网络IO开销single database round trip其示意图如下 当batch底层使用的是静态SQL并参数化执行时(JAVA中一般是使用类java.sql.PreparedStatement 来参数化执行静态SQL)数据库服务器可以只做一次解析利用对参数化机制的支持数据库服务器仅需要对 PreparedStatement 做一次解析sql parse即可传入不同参数执行该 batch 中所有的 SQL; 网上有个帖子详细对比了不同场景下不同数据库的插入和更新性能的差异可以看出ORACLE/PG/MYSQL 使用 batch 功能后性能都有了3-5被的提高 现在大家用的都是 PreparedStatement 了 对于 select update insert delete 来说、select 是用不上这个批次方法的 我们看到 executeBatch 返回的是 int[] 类型 不同 db 可能返回的值或者逻辑是不一样的 关于 rewriteBatchedStatements 参数 该参数是 mysql 专属的 https://stackoverflow.com/questions/2993251/jdbc-batch-insert-performance 里面有说到关于如何提高 mysql 批量 insert 的效率 useServerPrepStmts 是作用于 mysql 服务端的 rewriteBatchedStatements 是作用于mysql 客户端的、也就是我们的应用程序 那么它发挥了什么的作用 该参数的意思就是重写用户的 sql 已提高批量 update 的速率。 可以发现是将批量执⾏的⼀组 sql Insert 语句,改写为一条 batched 语句 insert into tableA (colA,colB) values (colA-value1,colB-value1),(colA-value2,colB-value2),(colA-value3,colB-value3), 并通过一次请求发送给数据库服务器的也就是说此时 mysql 使用了批量插入功能 对于批量 对于增删改 batchInsert(10 records) 会被改写为 insert into t (…) values (…), (…), (…)” 并一次性提交 如果不能被改写为 “multi-values”, 则会改写为多个;分割的sql语句并一次性提交语句 “INSERT INTO TABLE(col1) VALUES (?) ON DUPLICATE KEY UPDATE col2?” 与变量 [1,2] 和 [2,3]会被改写为 “INSERT INTO TABLE(col1) VALUES (1) ON DUPLICATE KEY UPDATE col22;INSERT INTO TABLE(col1) VALUES (3) ON DUPLICATE KEY UPDATE col24” 并一次性提交batchDelete(10 records) 会被改写为 delete from t where id 1; delete from t where id 2; delete from t where id 3;….并一次性提交batchUpdate(10 records) 会被改写为 “update t set… where id 1; update t set… where id 2; update t set… where id 3…” 并一次性提交 对于其他数据库比如 oracle 来说、默认是开启了这种批量优化的。比如现在 insert (1,1)(2,2)(3,3)… 到数据 源码 Override protected long[] executeBatchInternal() throws SQLException {synchronized (checkClosed().getConnectionMutex()) {if (this.connection.isReadOnly()) {throw new SQLException(Messages.getString(PreparedStatement.25) Messages.getString(PreparedStatement.26),MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT);}if (this.query.getBatchedArgs() null || this.query.getBatchedArgs().size() 0) {return new long[0];}// we timeout the entire batch, not individual statementsint batchTimeout getTimeoutInMillis();setTimeoutInMillis(0);resetCancelledState();try {statementBegins();clearWarnings();if (!this.batchHasPlainStatements this.rewriteBatchedStatements.getValue()) {if (getQueryInfo().isRewritableWithMultiValuesClause()) {return executeBatchWithMultiValuesClause(batchTimeout);}if (!this.batchHasPlainStatements this.query.getBatchedArgs() ! null this.query.getBatchedArgs().size() 3 /* cost of option setting rt-wise */) {return executePreparedBatchAsMultiStatement(batchTimeout);}}return executeBatchSerially(batchTimeout);} finally {this.query.getStatementExecuting().set(false);clearBatch();}} }看到 executeBatchSerially 方法 for (batchCommandIndex 0; batchCommandIndex nbrCommands; batchCommandIndex) {((PreparedQuery?) this.query).setBatchCommandIndex(batchCommandIndex);Object arg this.query.getBatchedArgs().get(batchCommandIndex);try {if (arg instanceof String) {updateCounts[batchCommandIndex] executeUpdateInternal((String) arg, true, this.retrieveGeneratedKeys);// limit one generated key per OnDuplicateKey statementgetBatchedGeneratedKeys(this.results.getFirstCharOfQuery() I containsOnDuplicateKeyInString((String) arg) ? 1 : 0);} else {QueryBindings? queryBindings (QueryBindings?) arg;updateCounts[batchCommandIndex] executeUpdateInternal(queryBindings, true);// limit one generated key per OnDuplicateKey statementgetBatchedGeneratedKeys(containsOnDuplicateKeyUpdateInSQL() ? 1 : 0);}} catch (SQLException ex) {updateCounts[batchCommandIndex] EXECUTE_FAILED;if (this.continueBatchOnError !(ex instanceof MySQLTimeoutException) !(ex instanceof MySQLStatementCancelledException) !hasDeadlockOrTimeoutRolledBackTx(ex)) {sqlEx ex;} else {long[] newUpdateCounts new long[batchCommandIndex];System.arraycopy(updateCounts, 0, newUpdateCounts, 0, batchCommandIndex);throw SQLError.createBatchUpdateException(ex, newUpdateCounts, this.exceptionInterceptor);}} }确实是一条条插入到数据库中的 但是我们认真看看、调用这个函数之前、还有其他出口 if (!this.batchHasPlainStatements this.rewriteBatchedStatements.getValue()) {if (getQueryInfo().isRewritableWithMultiValuesClause()) {return executeBatchWithMultiValuesClause(batchTimeout);}if (!this.batchHasPlainStatements this.query.getBatchedArgs() ! null this.query.getBatchedArgs().size() 3 /* cost of option setting rt-wise */) {return executePreparedBatchAsMultiStatement(batchTimeout);}}this.rewriteBatchedStatements.getValue() 这个我们说过的参数 batchHasPlainStatements 默认就是 false 不用管 executeBatchWithMultiValuesClause 做的就是 insert 的时候多 values executePreparedBatchAsMultiStatement 做的就是 ; 分割的多个 sql 语句、比如 delete 、update 或者是 insert 无法多个 valuse 的 Mysql 这里还涉及到另外的一个参数 max_allowed_packet 代表一次的网络包最大是多少 看到在 mysql 8.0 这边的大小是 32M 左右 Spring JdbcTemplate 直接 JDBC //获取要设置的Arp基准的List后,插入Arp基准表中 public boolean insertArpStandardList(ListArpTable list) {Connection conn null;PreparedStatement ps null;ResultSet rs null;//MySql的JDBC连接的url中要加rewriteBatchedStatements参数并保证5.1.13以上版本的驱动才能实现高性能的批量插入。//优化插入性能用JDBC的addBatch方法但是注意在连接字符串加上面写的参数。//例如 String connectionUrljdbc:mysql://192.168.1.100:3306/test?rewriteBatchedStatementstrue ;String sql insert into arp_standard(guid, devicebrand, devicename, deviceip, ipaddress, macaddress, createtime) values(?,?,?,?,?,?,?);try{conn DBConnection.getConnection();ps conn.prepareStatement(sql);//优化插入第一步设置手动提交 conn.setAutoCommit(false); int len list.size();for(int i0; ilen; i) {ps.setString(1, list.get(i).getGuid());ps.setString(2, list.get(i).getDeviceBrand());ps.setString(3, list.get(i).getDeviceName());ps.setString(4, list.get(i).getDeviceIp());ps.setString(5, list.get(i).getIpAddress());ps.setString(6, list.get(i).getMacAddress());ps.setString(7, list.get(i).getCreateTime());//if(ps.executeUpdate() ! 1) r false; 优化后不用传统的插入方法了。//优化插入第二步插入代码打包等一定量后再一起插入。ps.addBatch(); //if(ps.executeUpdate() ! 1)result false;//每200次提交一次 if((i!0 i%2000) || ilen-1){//可以设置不同的大小如501002005001000等等 ps.executeBatch(); //优化插入第三步提交批量插入数据库中。conn.commit(); ps.clearBatch();//提交后Batch清空。}}} catch (Exception e) {System.out.println(MibTaskPack-getArpInfoList() error: e.getMessage());return false; //出错才报false} finally {DBConnection.closeConection(conn, ps, rs);}return true;}https://mdnice.com/writing/fc6d8a16525d447bbcae4c5be34215a0 https://stackoverflow.com/questions/26307760/mysql-and-jdbc-with-rewritebatchedstatements-true https://letcoding.com/2024/01/03/%E5%A5%BD%E5%A5%BD%E7%A0%81%E4%BB%A3%E7%A0%81%E5%90%96/JAVA/MyBatis/JDBC%E8%BF%9E%E6%8E%A5%E5%8F%82%E6%95%B0%E3%80%90rewriteBatchedStatements%E3%80%91%E8%AF%A6%E8%A7%A3/ https://www.cnblogs.com/lizm166/p/7890168.html
http://www.dnsts.com.cn/news/133807.html

相关文章:

  • 动漫网站建设方案项目书目录网站开发充值功能
  • 沈阳沈阳建设工程信息网站东莞营业厅
  • 2_ 如何写一份详细的网站开发方案新手做自己的网站
  • 网络公司除了做网站自学黑客编程入门
  • 网站广告弹窗代码甘肃省城市建设档案馆网站
  • 做ptt网站wordpress xiu
  • 小公司怎么做免费网站在wordpress加入文件管理器
  • 滁州项目建设公示在哪个网站电商型网站开发多少钱
  • 东营做网站优化多少钱体育新闻最新消息
  • 重庆网站模板建站公司建网站程序怎么办
  • 加强网站的建设网站用哪些系统做的比较好用
  • 郑州网站建设知识分享忘记php网站后台密码
  • 住房和城市建设厅网站域名和网站关联
  • 在那个网站做直播好赚钱吗安徽网站建设的基本步骤
  • 免费建站工具桂林象鼻山景区简介
  • 汕头市住房和城乡建设局网站代刷网站搭建教程
  • 电子商务网站建设答辩记录怎么做图片网站
  • 股票场外期权网站开发wordpress 地图导航代码
  • 建设中小企业网站山东网站备案公司吗
  • 网站文字优化方案黑龙江省公共资源
  • 旅游网站平台网站建设意义必要性
  • 做网站建设的怎么寻找客户太原最新情况
  • 建设银行信用卡网站登录wordpress 下载别人的主题
  • 网站建设和运维网站配色主题
  • 文化网站模版郑州小程序制作流程及费用
  • 国外网站建设视频教学漳州 外贸网站建设 SEO
  • 专业建站提供商开发者模式影响手机吗
  • 手机如何创造网站简易做海报网站
  • 西安建设门户网站企业地址管理系统
  • 中国黄冈网网站主机的选择与优化