Oracle Insert和bulk Insert测速对比实例

数据库 Oracle
经过长时间接触Oracle Insert和bulk Insert,我对比了一下他们的执行效率。在这里和大家分享一下,希望你看完本文后有不少收获。

经过长时间接触Oracle Insert和bulk Insert,我对比了一下他们的执行效率。在这里和大家分享一下,希望你看完本文后有不少收获。

测试java的insert 同使用9i以后的bulk Insert 的速度,结果显示通过bulk Insert 速度相当的快.

100000条记录

insert ,---------------93秒

bulk Insert -------------0.441秒

环境:

oracle 10.2.0.3 Windows 2000Server

java

代码:

  1. SQL> desc a  
  2. Name Type Nullable Default Comments   
  3. ---- ------------ -------- ------- --------   
  4. ID INTEGER Y   
  5. NAME VARCHAR2(20) Y   

bulk Insert 使用的类型及过程

  1. create or replace type i_table is table of number(10);  
  2. create or replace type v_table is table of varchar2(10);  
  3. create or replace procedure pro_forall_insert(v_1 i_table,v_2 v_table)  
  4. as  
  5. c integer;  
  6. begin  
  7. forall i in 1.. v_1.count   
  8. insert into a values(v_1(i),v_2(i));  
  9. end;  
  10.  

测试的java代码:

  1. import java.io.*;  
  2. import java.sql.*;  
  3. import java.util.*;  
  4. import javax.naming.Context;  
  5. import javax.naming.InitialContext;  
  6. import javax.naming.*;  
  7. import oracle.jdbc.OracleTypes;  
  8. import oracle.sql.*;  
  9. import oracle.sql.ARRAY;  
  10. import oracle.sql.ArrayDescriptor;  
  11. import oracle.sql.STRUCT;  
  12. import oracle.sql.StructDescriptor;  
  13. import java.sql.Connection;  
  14. import java.sql.DriverManager;  
  15. import oracle.jdbc.OracleCallableStatement;  
  16.  
  17. public class testOracle {  
  18. public testOracle() {  
  19. Connection oraCon = null;  
  20. PreparedStatement ps = null;  
  21. Statement st = null;  
  22. ResultSet rs = null;  
  23. try {  
  24. try {  
  25. Class.forName("oracle.jdbc.driver.OracleDriver");  
  26. } catch (ClassNotFoundException ex) {}  
  27. oraCon = DriverManager.getConnection("jdbc:oracle:thin:@192.168.15.234:1521:ora10g", "imcs","imcs");  
  28. oraCon.setAutoCommit(false);  
  29. } catch (SQLException ex) {  
  30. ex.printStackTrace();  
  31. }  
  32. CallableStatement cstmt = null;  
  33. oracle.sql.ArrayDescriptor a = null;  
  34. oracle.sql.ArrayDescriptor b = null;  
  35. if (1 == 1 )  
  36. {  
  37. Object[] s1 = new Object[100000];  
  38. Object[] s2 = new Object[100000];  
  39. for (int i = 0; i < 100000; i++) {  
  40. s1[i] = new Integer(1);  
  41. s2[i] = new String("aaa").concat(String.valueOf(i));  
  42. }  
  43. try {  
  44. a = oracle.sql.ArrayDescriptor.createDescriptor("I_TABLE", oraCon);  
  45. b = oracle.sql.ArrayDescriptor.createDescriptor("V_TABLE", oraCon);  
  46. ARRAY a_test = new ARRAY(a, oraCon, s1);  
  47. ARRAY b_test = new ARRAY(b, oraCon, s2);  
  48. cstmt = oraCon.prepareCall("{ call pro_forall_insert(?,?) }");  
  49. cstmt.setObject(1, a_test);  
  50. cstmt.setObject(2, b_test);  
  51. long aaaa = System.currentTimeMillis();  
  52. System.out.println(System.currentTimeMillis());  
  53. cstmt.execute();  
  54. oraCon.commit();  
  55. System.out.println(System.currentTimeMillis()-aaaa);  
  56.  catch (Exception e) {  
  57. e.printStackTrace();  
  58. }  
  59. }  
  60. else  
  61. {  
  62. try  
  63. {  
  64. PreparedStatement oraPs = null;  
  65. String oraInsertSql =  
  66. "insert into a values(?,?)";  
  67. oraPs = oraCon.prepareStatement(oraInsertSql);  
  68. long aaaa = System.currentTimeMillis();  
  69. System.out.println(System.currentTimeMillis());  
  70. for (int i = 0; i < 100000; i++)  
  71. {  
  72. oraPs.setInt(1,i);  
  73. oraPs.setString(2, new String("aaa").concat(String.valueOf(i)));  
  74. oraPs.executeUpdate();   
  75. }  
  76. oraCon.commit();  
  77. System.out.println(System.currentTimeMillis()-aaaa);  
  78. }  
  79. catch (SQLException ex)  
  80. {  
  81. System.out.print("dddddd");  
  82. System.out.print(ex.getMessage());  
  83. }  
  84. }  
  85. try {  
  86. jbInit();  
  87. } catch (Exception ex) {  
  88. ex.printStackTrace();  
  89. }  
  90. }  
  91. public static void main(String args[]) {  
  92. testOracle a = new testOracle();  
  93. }  
  94. private void jbInit() throws Exception {  
  95. }  
  96. };  
  97.  

【编辑推荐】

  1. 修改Oracle存 储过程所需代码
  2. 对Oracle存储过程的总结
  3. 实现Oracle存 储过程的实际应用的代码 
  4. 深入高性能的 Oracle动态SQL开发 
  5. Oracle SQL的 优化规则解析 
责任编辑:佚名
相关推荐

2011-08-11 10:16:15

2010-04-13 14:00:00

Oracle inse

2010-11-18 17:24:27

Oracle旋转ins

2011-08-24 15:48:38

INSERT中文man

2010-05-10 18:30:31

Oracle多表创建

2023-03-27 08:17:48

2010-09-03 15:27:02

SQLSELECT语句

2022-06-27 07:56:36

Mybatis源码Spring

2010-05-20 08:47:21

MySQL数据库

2010-09-25 14:02:18

SQL INSERT

2018-08-23 09:10:01

数据库MySQLInnoDB

2018-08-27 07:29:34

InnoDBinsertselect

2010-05-27 14:47:14

MySQL INSER

2010-10-08 14:32:47

MySQL使用INSE

2011-07-28 09:08:14

MongoDB性能测试

2010-09-07 13:50:41

SQL语句

2022-05-16 08:03:12

MySQL数据库

2010-09-07 16:31:17

SQL语句insert

2021-11-11 13:05:25

MySQLINSERT

2021-11-15 07:47:39

InnoDB缓存池磁盘
点赞
收藏

51CTO技术栈公众号