65f1ce8679dec4ec864e4f226503f4bb9e871485.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package test.main.DB;
  2. import java.sql.*;
  3. public class TestBatch{
  4. /**
  5. * JDBC批量Insert优化(上)
  6. *
  7. * @author leizhimin 2009-7-29 10:03:10
  8. */
  9. public static TestBatchDbConnection myBroker = null;
  10. static {
  11. myBroker = new TestBatchDbConnection();
  12. }
  13. /**
  14. * 初始化测试环境
  15. *
  16. * @throws SQLException
  17. * 异常时抛出
  18. * @throws ClassNotFoundException
  19. */
  20. public static void init() throws SQLException, ClassNotFoundException {
  21. Connection conn = myBroker.getConnection();
  22. Statement stmt = conn.createStatement();
  23. stmt.addBatch("IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tuser]') AND type in (N'U'))DROP TABLE [dbo].[tuser]");
  24. stmt.addBatch("CREATE TABLE tuser (\n" + " id varchar(20) DEFAULT NULL,\n"
  25. + " name varchar(12) DEFAULT NULL,\n" + " remark varchar(24) DEFAULT NULL,\n"
  26. + " createtime datetime DEFAULT NULL,\n" + " updatetime datetime DEFAULT NULL"
  27. + ")");
  28. stmt.executeBatch();
  29. myBroker.freeConnection(conn);
  30. }
  31. /**
  32. * 1000条静态SQL插入
  33. *
  34. * @throws Exception
  35. * 异常时抛出
  36. */
  37. public static void testInsert() throws Exception {
  38. init(); // 初始化环境
  39. Long start = System.currentTimeMillis();
  40. for (int i = 0; i < 1000; i++) {
  41. String sql = "\n" + "insert into tuser \n" + "\t(name, \n" + "\tremark, \n"
  42. + "\tcreatetime, \n" + "\tupdatetime\n" + "\t)\n" + "\tvalues\n" + "\t('"
  43. + (i + 1) + "', \n" + "\t'" + (i + 2) + "',getDate(), \n" + "getDate()\n" + ")";
  44. // System.out.println(sql);
  45. Connection conn = myBroker.getConnection();
  46. Statement stmt = conn.createStatement();
  47. stmt.execute(sql);
  48. myBroker.freeConnection(conn);
  49. }
  50. Long end = System.currentTimeMillis();
  51. System.out.println("单条执行1000条Insert操作,共耗时:" + (end - start) / 1000f + "秒!");
  52. }
  53. /**
  54. * 批处理执行静态SQL测试
  55. *
  56. * @param m
  57. * 批次
  58. * @param n
  59. * 每批数量
  60. * @throws Exception
  61. * 异常时抛出
  62. */
  63. public static void testInsertBatch(int m, int n) throws Exception {
  64. init(); // 初始化环境
  65. Long start = System.currentTimeMillis();
  66. for (int i = 0; i < m; i++) {
  67. // 从池中获取连接
  68. Connection conn = myBroker.getConnection();
  69. Statement stmt = conn.createStatement();
  70. for (int k = 0; k < n; k++) {
  71. String sql = "\n" + "insert into tuser \n" + "\t(name, \n" + "\tremark, \n"
  72. + "\tcreatetime, \n" + "\tupdatetime\n" + "\t)\n" + "\tvalues\n" + "\t('"
  73. + (k + 1) + "', \n" + "\t'" + (k + 2) + "', \n" + "getDate(), \n"
  74. + "getDate()\n" + ")";
  75. // 加入批处理
  76. stmt.addBatch(sql);
  77. }
  78. stmt.executeBatch(); // 执行批处理
  79. // stmt.clearBatch(); //清理批处理
  80. stmt.close();
  81. myBroker.freeConnection(conn); // 连接归池
  82. }
  83. Long end = System.currentTimeMillis();
  84. System.out.println("批量执行" + m + "*" + n + "=" + m * n + "条Insert操作,共耗时:" + (end - start)
  85. / 1000f + "秒!");
  86. }
  87. /**
  88. * 1000条预定义SQL插入
  89. *
  90. * @throws Exception
  91. * 异常时抛出
  92. */
  93. public static void testInsert2() throws Exception { // 单条执行1000条Insert操作,共耗时:40.422秒!
  94. init(); // 初始化环境
  95. Long start = System.currentTimeMillis();
  96. String sql = "" + "insert into tuser\n" + " (name, remark, createtime, updatetime)\n"
  97. + "values\n" + " (?, ?, ?, ?)";
  98. for (int i = 0; i < 1000; i++) {
  99. Connection conn = myBroker.getConnection();
  100. PreparedStatement pstmt = conn.prepareStatement(sql);
  101. pstmt.setString(1, (i + 1) + "");
  102. pstmt.setString(2, (i + 2) + "");
  103. pstmt.setDate(3, new Date(System.currentTimeMillis()));
  104. pstmt.setDate(4, new Date(System.currentTimeMillis()));
  105. pstmt.executeUpdate();
  106. pstmt.close();
  107. myBroker.freeConnection(conn);
  108. }
  109. Long end = System.currentTimeMillis();
  110. System.out.println("单条执行1000条Insert操作,共耗时:" + (end - start) / 1000f + "秒!");
  111. }
  112. /**
  113. * 批处理执行预处理SQL测试
  114. *
  115. * @param m
  116. * 批次
  117. * @param n
  118. * 每批数量
  119. * @throws Exception
  120. * 异常时抛出
  121. */
  122. public static void testInsertBatch2(int m, int n) throws Exception {
  123. init(); // 初始化环境
  124. Long start = System.currentTimeMillis();
  125. String sql = "" + "insert into tuser\n" + " (name, remark, createtime, updatetime) values "
  126. + " (?, ?, ?, ?)";
  127. for (int i = 0; i < m; i++) {
  128. // 从池中获取连接
  129. Connection conn = myBroker.getConnection();
  130. PreparedStatement pstmt = conn.prepareStatement(sql);
  131. for (int k = 0; k < n; k++) {
  132. pstmt.setString(1, (k + 1) + "");
  133. pstmt.setString(2, (k + 2) + "");
  134. pstmt.setDate(3, new Date(System.currentTimeMillis()));
  135. pstmt.setDate(4, new Date(System.currentTimeMillis()));
  136. // 加入批处理
  137. pstmt.addBatch();
  138. }
  139. pstmt.executeBatch(); // 执行批处理
  140. // pstmt.clearBatch(); //清理批处理
  141. pstmt.close();
  142. myBroker.freeConnection(conn); // 连接归池
  143. }
  144. Long end = System.currentTimeMillis();
  145. System.out.println("批量执行" + m + "*" + n + "=" + m * n + "条Insert操作,共耗时:" + (end - start)
  146. / 1000f + "秒!");
  147. }
  148. public static void main(String[] args) throws Exception {
  149. init();
  150. Long start = System.currentTimeMillis();
  151. System.out.println("--------A组测试----------");
  152. testInsert();
  153. testInsertBatch(100, 1000);
  154. testInsertBatch(250, 400);
  155. testInsertBatch(400, 250);
  156. testInsertBatch(500, 200);
  157. testInsertBatch(1000, 100);
  158. testInsertBatch(2000, 50);
  159. testInsertBatch(2500, 40);
  160. testInsertBatch(5000, 20);
  161. Long end1 = System.currentTimeMillis();
  162. System.out.println("B组测试过程结束,全部测试耗时:" + (end1 - start) / 1000f + "秒!");
  163. System.out.println("--------B组测试----------");
  164. testInsert2();
  165. testInsertBatch2(100, 1000);
  166. testInsertBatch2(250, 400);
  167. testInsertBatch2(400, 250);
  168. testInsertBatch2(500, 200);
  169. testInsertBatch2(1000, 100);
  170. testInsertBatch2(2000, 50);
  171. testInsertBatch2(2500, 40);
  172. testInsertBatch2(5000, 20);
  173. Long end2 = System.currentTimeMillis();
  174. System.out.println("B组测试过程结束,全部测试耗时:" + (end2 - end1) / 1000f + "秒!");
  175. }
  176. }