2014-10-29 124 views
1

我试图插入一个表中大量的100(我听说它是​​使用mySQL的最佳大小),我使用scala 2.10.4与sbt 0.13.6和JDBC框架我现在用的就是scalikejdbc与Hikaricp,我的连接设置是这样的:在scalikejdbc中的批量插入在远程计算机上的速度很慢

val dataSource: DataSource = { 
    val ds = new HikariDataSource() 
    ds.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); 
    ds.addDataSourceProperty("url", "jdbc:mysql://" + org.Server.GlobalSettings.DB.mySQLIP + ":3306?rewriteBatchedStatements=true") 
    ds.addDataSourceProperty("autoCommit", "false") 
    ds.addDataSourceProperty("user", "someUser") 
    ds.addDataSourceProperty("password", "not my password") 
    ds 
} 

ConnectionPool.add('review, new DataSourceConnectionPool(dataSource)) 

插入代码:

try { 
    implicit val session = AutoSession 
    val paramList: scala.collection.mutable.ListBuffer[Seq[(Symbol, Any)]] = scala.collection.mutable.ListBuffer[Seq[(Symbol, Any)]]() 
    . 
    . 
    . 
    for(rev<reviews){ 
    paramList += Seq[(Symbol, Any)](
      'review_id -> rev.review_idx, 
      'text -> rev.text, 
      'category_id -> rev.category_id, 
      'aspect_id -> aspectId, 
      'not_aspect -> noAspect /*0*/ , 
      'certainty_aspect -> rev.certainty_aspect, 
      'sentiment -> rev.sentiment, 
      'sentiment_grade -> rev.certainty_sentiment, 
      'stars -> rev.stars 
     ) 
    } 
    . 
    . 
    . 
    try { 
    if (paramList != null && paramList.length > 0) { 
     val result = NamedDB('review) localTx { implicit session => 
     sql"""INSERT INTO `MasterFlow`.`classifier_results` 
     (
      `review_id`, 
      `text`, 
      `category_id`, 
      `aspect_id`, 
      `not_aspect`, 
      `certainty_aspect`, 
      `sentiment`, 
      `sentiment_grade`, 
      `stars`) 
     VALUES 
       ({review_id}, {text}, {category_id}, {aspect_id}, 
       {not_aspect}, {certainty_aspect}, {sentiment}, {sentiment_grade}, {stars}) 
     """ 
      .batchByName(paramList.toIndexedSeq: _*)/*.__resultOfEnsuring*/ 
      .apply() 
     } 

每次我插入了一批花了15秒,我的日志:

29/10/2014 14:03:36 - DEBUG[Hikari Housekeeping Timer (pool HikariPool-0)] HikariPool - Before cleanup pool stats HikariPool-0 (total=10, inUse=1, avail=9, waiting=0) 
29/10/2014 14:03:36 - DEBUG[Hikari Housekeeping Timer (pool HikariPool-0)] HikariPool - After cleanup pool stats HikariPool-0 (total=10, inUse=1, avail=9, waiting=0) 
29/10/2014 14:03:46 - DEBUG[default-akka.actor.default-dispatcher-3] StatementExecutor$$anon$1 - SQL execution completed 

    [SQL Execution] 
    INSERT INTO `MasterFlow`.`classifier_results` (`review_id`, `text`, `category_id`, `aspect_id`, `not_aspect`, `certainty_aspect`, `sentiment`, `sentiment_grade`, `stars`) VALUES (...can't show this....); 
    INSERT INTO `MasterFlow`.`classifier_results` (`review_id`, `text`, `category_id`, `aspect_id`, `not_aspect`, `certainty_aspect`, `sentiment`, `sentiment_grade`, `stars`) VALUES (...can't show this....); 
. 
. 
. 
    INSERT INTO `MasterFlow`.`classifier_results` (`review_id`, `text`, `category_id`, `aspect_id`, `not_aspect`, `certainty_aspect`, `sentiment`, `sentiment_grade`, `stars`) VALUES (...can't show this....); 
    ... (total: 100 times); (15466 ms) 

    [Stack Trace] 
    ... 
    logic.DB.ClassifierJsonToDB$$anonfun$1.apply(ClassifierJsonToDB.scala:119) 
    logic.DB.ClassifierJsonToDB$$anonfun$1.apply(ClassifierJsonToDB.scala:96) 
    scalikejdbc.DBConnection$$anonfun$_localTx$1$1.apply(DBConnection.scala:252) 
    scala.util.control.Exception$Catch.apply(Exception.scala:102) 
    scalikejdbc.DBConnection$class._localTx$1(DBConnection.scala:250) 
    scalikejdbc.DBConnection$$anonfun$localTx$1.apply(DBConnection.scala:257) 
    scalikejdbc.DBConnection$$anonfun$localTx$1.apply(DBConnection.scala:257) 
    scalikejdbc.LoanPattern$class.using(LoanPattern.scala:33) 
    scalikejdbc.NamedDB.using(NamedDB.scala:32) 
    scalikejdbc.DBConnection$class.localTx(DBConnection.scala:257) 
    scalikejdbc.NamedDB.localTx(NamedDB.scala:32) 
    logic.DB.ClassifierJsonToDB$.insertBulk(ClassifierJsonToDB.scala:96) 
    logic.DB.ClassifierJsonToDB$$anonfun$bulkInsert$1.apply(ClassifierJsonToDB.scala:176) 
    logic.DB.ClassifierJsonToDB$$anonfun$bulkInsert$1.apply(ClassifierJsonToDB.scala:167) 
    scala.collection.Iterator$class.foreach(Iterator.scala:727) 
    ... 

当我在托管mySQL数据库的服务器上运行它时,它运行得很快,我能做些什么来使它在远程计算机上运行得更快?

回答

0

我想这不是ScalikeJDBC或HikariCP的问题。你应该调查你的机器和MySQL服务器之间的网络环境。

+1

也许尝试在HikariCP中关闭自动提交并让scalikejdbc处理提交。 – brettw 2014-10-30 12:15:49

1

如果有需要的话,我遇到了类似的问题,可以通过在jdbc url中设置rewriteBatchedStatements为true(“jdbc:mysql:// host:3306”)来批量插入10000条记录到ScalikeJdbc的MySQL中/ DB?rewriteBatchedStatements =真正的“)。它将批量插入时间从40秒减少到1秒!

+0

我使用了它(请参阅我的问题中的第一个代码块),但谢谢任何方式。 – user1120007 2015-07-26 07:08:14

相关问题