码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis三种执行器性能比较

时间:2020-12-23 12:46:19      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:高效   deb   comm   row   mysql   code   als   ret   out   

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()方法提高效率

 

 

mybatis三种执行器性能比较

标签:高效   deb   comm   row   mysql   code   als   ret   out   

原文地址:https://www.cnblogs.com/yibao/p/14157704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!