mybatis三种执行器性能比较

Mybatis内置的三种执行器

 

public enum ExecutorType {
SIMPLE, REUSE, BATCH
}

 



默认是SIMPLE。依次是:单次提交、复用、批量(JDBC本身就支持批量)

SIMPLE

[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Preparing: insert into blog values(?,?,?)
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Parameters: 80(Integer), allen(String), 2012(String)
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - <== Updates: 1
[main] DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6580cfdd]
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Preparing: insert into blog values(?,?,?)
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Parameters: 81(Integer), allen(String), 2012(String)
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - <== Updates: 1
[main] DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6580cfdd]

REUSE

[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Preparing: insert into blog values(?,?,?)
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Parameters: 0(Integer), allen(String), 2012(String)
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - <== Updates: 1
[main] DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6580cfdd]

BATCH

[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Preparing: insert into blog values(?,?,?)
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Parameters: 0(Integer), allen(String), 2012(String)
[main] DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6580cfdd]
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Preparing: insert into blog values(?,?,?)
[main] DEBUG [org.apache.ibatis.demo.BlogMapper.insertBlog] - ==> Parameters: 1(Integer), allen(String), 2012(String)
[main] DEBUG [org.apache.ibatis.transaction.jdbc.JdbcTransaction] - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@6580cfdd]

 

同样都是插入100,000条数据,对比一下三种执行器的执行时间

[SIMPLE]

 

 

 

 

[REUSE]

 

 

 

 

[BATCH]

 

 

 

 

 

 比较一下三者的源码实现

 

 

 

几种执行器的实现比较

[SimpleExecutor]

public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
  Statement stmt = null;
  try {
    Configuration configuration = ms.getConfiguration();
    StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
    stmt = prepareStatement(handler, ms.getStatementLog());
    return handler.update(stmt);
  } finally {
    closeStatement(stmt);
  }
}

 

[ReuseExecutor]

public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
  Configuration configuration = ms.getConfiguration();
  StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
  Statement stmt = prepareStatement(handler, ms.getStatementLog());
  return handler.update(stmt);
}

 

[BatchExecutor]

public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
  final Configuration configuration = ms.getConfiguration();
  final StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, RowBounds.DEFAULT, null, null);
  final BoundSql boundSql = handler.getBoundSql();
  final String sql = boundSql.getSql();
  final Statement stmt;
  if (sql.equals(currentSql) && ms.equals(currentStatement)) {
    int last = statementList.size() - 1;
    stmt = statementList.get(last);
    applyTransactionTimeout(stmt);
    handler.parameterize(stmt);// fix Issues 322
    BatchResult batchResult = batchResultList.get(last);
    batchResult.addParameterObject(parameterObject);
  } else {
    Connection connection = getConnection(ms.getStatementLog());
    stmt = handler.prepare(connection, transaction.getTimeout());
    handler.parameterize(stmt);    // fix Issues 322
    currentSql = sql;
    currentStatement = ms;
    statementList.add(stmt);
    batchResultList.add(new BatchResult(ms, sql, parameterObject));
  }
  handler.batch(stmt);
  return BATCH_UPDATE_RETURN_VALUE;
}

REUSE是Statement不会执行一次就关闭,BATCH是使用JDBC的addBatch()方法提高效率

 

 

posted @ 2020-12-18 23:55  Java民工陆小凤  阅读(744)  评论(0编辑  收藏  举报