ORACLE 兩種插入方式對比

  • 原始批量插入方式
<insert id="batchInsert">
  <foreach open="begin" close=";end;" collection="list" item="item" separator=";">
    insert into VOTES (
    <include refid="VotesResult"></include>
    ) values(
    <trim suffixOverrides=",">
      #{item.id},
      #{item.productCode},
      #{item.productName},
      #{item.cityName},
      #{item.statStartTime},
      #{item.statEndTime},
      #{item.interfaceType},
      #{item.creator},
      sysdate,
      #{item.modifier},
      sysdate,
      0,
      #{item.recStatus},
    </trim>
    )
  </foreach>
</insert>
複製代碼
  • 優化批量插入
<insert id="batchInsert">
    insert into VOTES (
    <include refid="VotesResult"></include>
    )
    <foreach collection="list" item="item" index="index" separator="union all">
      (
      SELECT
       #{item.id},
      #{item.productCode},
      #{item.productName},
      #{item.cityName},
      #{item.statStartTime},
      #{item.statEndTime},
      #{item.interfaceType},
      #{item.creator},
      sysdate,
      #{item.modifier},
      sysdate,
      0,
      #{item.recStatus},
      FROM DUAL
      )
    </foreach>
  </insert>
複製代碼
條數 原始插入耗時(ms) 優化插入耗時(ms)------Druid 1.0.6 優化插入耗時(ms)------Druid 1.1.10
500 508ms 156ms 162ms
2000 1162ms 366ms 452ms
5000 2711ms 1039ms 1007ms
10000 5279ms 1626ms 1716ms

注意,使用 druid 版本低於 1.0.22 時,語句中含有 union 關鍵字 Druid 會出現解析異常:java

com.alibaba.druid.sql.parser.ParserException: TODO : UNION union at com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser.parseStatementList(OracleStatementParser.java:489)
at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:145)
at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:140)
at com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils.parameterize(ParameterizedOutputVisitorUtils.java:53)
at com.alibaba.druid.filter.stat.StatFilter.mergeSql(StatFilter.java:145)
at com.alibaba.druid.filter.stat.StatFilter.createSqlStat(StatFilter.java:630)
at com.alibaba.druid.filter.stat.StatFilter.statementPrepareAfter(StatFilter.java:305)
at com.alibaba.druid.filter.FilterEventAdapter.connection_prepareStatement(FilterEventAdapter.java:124)
at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:448)
at com.alibaba.druid.filter.FilterAdapter.connection_prepareStatement(FilterAdapter.java:928)
at com.alibaba.druid.filter.FilterEventAdapter.connection_prepareStatement(FilterEventAdapter.java:122)
at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:448)
at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.prepareStatement(ConnectionProxyImpl.java:342)
at com.alibaba.druid.pool.DruidPooledConnection.prepareStatement(DruidPooledConnection.java:331)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:79)
at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:58)
at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:49)
...
複製代碼

不影響運行,主要緣由是 Druid 版本問題,詳見 github.com/alibaba/dru…git

相關文章
相關標籤/搜索