2016-01-25 123 views
24

在Slick 3.0中執行批量insertOrUpdate的正確方法是什麼?Slick 3.0批量插入或更新(upsert)

我使用MySQL在適當的查詢是

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) 
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); 

MySQL bulk INSERT or UPDATE

這裏是我當前的代碼,這是非常緩慢:-(

// FIXME -- this is slow but will stop repeats, an insertOrUpdate 
// functions for a list would be much better 
val rowsInserted = rows.map { 
    row => await(run(TableQuery[FooTable].insertOrUpdate(row))) 
}.sum 

什麼我要找相當於

def insertOrUpdate(values: Iterable[U]): DriverAction[MultiInsertResult, NoStream, Effect.Write] 

回答

28

有幾種方法,你可以使這個代碼快(每一個應該比前面的人快,但它會逐漸減少慣用-光滑):

  • 運行您DBIO事件一次全部,而不是等待每一個提交你運行下之前:

    val toBeInserted = rows.map { row => TableQuery[FooTable].insertOrUpdate(row) } 
    val inOneGo = DBIO.sequence(toBeInserted) 
    val dbioFuture = run(inOneGo) 
    // Optionally, you can add a `.transactionally` 
    // and/or `.withPinnedSession` here to pin all of these upserts 
    // to the same transaction/connection 
    // which *may* get you a little more speed: 
    // val dbioFuture = run(inOneGo.transactionally) 
    val rowsInserted = await(dbioFuture).sum 
    
  • 下降到JDBC級別和運行UPSERT所有一氣呵成(idea via this answer):

    val SQL = """INSERT INTO table (a,b,c) VALUES (?, ?, ?) 
    ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);""" 
    
    SimpleDBIO[List[Int]] { session => 
        val statement = session.connection.prepareStatement(SQL) 
        rows.map { row => 
        statement.setInt(1, row.a) 
        statement.setInt(2, row.b) 
        statement.setInt(3, row.c) 
        statement.addBatch() 
        } 
        statement.executeBatch() 
    } 
    
+0

酷。特別感謝第二種技術。我不知道 – user1902291

+0

只需重新檢查:第一個解決方案不是批量插入,是嗎?它看起來像是在並行bot中進行所有插入操作而不是批處理,不是嗎? – ignasi35

+0

正確@ ignasi35 –

0

正如您在Slick examples中所看到的,您可以使用++=函數使用JDBC批插入功能插入。每個實例:

val foos = TableQuery[FooTable] 
val rows: Seq[Foo] = ... 
foos ++= rows // here slick will use batch insert 

也可以在「大小」你批量的「分組」的行順序:

val batchSize = 1000 
rows.grouped(batchSize).foreach { group => foos ++= group } 
+9

謝謝,但我不要認爲++ = insertOrUpdate。我相信這只是插入,並在我的情況下將拋出一個完整性異常,如果有重複行 – user1902291

0

使用sqlu

這演示作品

case ("insertOnDuplicateKey",answers:List[Answer])=>{ 
    def buildInsert(r: Answer): DBIO[Int] = 
    sqlu"insert into answer (aid,bid,sbid,qid,ups,author,uid,nick,pub_time,content,good,hot,id,reply,pic,spider_time) values (${r.aid},${r.bid},${r.sbid},${r.qid},${r.ups},${r.author},${r.uid},${r.nick},${r.pub_time},${r.content},${r.good},${r.hot},${r.id},${r.reply},${r.pic},${r.spider_time}) ON DUPLICATE KEY UPDATE `aid`=values(aid),`bid`=values(bid),`sbid`=values(sbid),`qid`=values(qid),`ups`=values(ups),`author`=values(author),`uid`=values(uid),`nick`=values(nick),`pub_time`=values(pub_time),`content`=values(content),`good`=values(good),`hot`=values(hot),`id`=values(id),`reply`=values(reply),`pic`=values(pic),`spider_time`=values(spider_time)" 
    val inserts: Seq[DBIO[Int]] = answers.map(buildInsert) 
    val combined: DBIO[Seq[Int]] = DBIO.sequence(inserts) 
    DEST_DB.run(combined).onComplete(data=>{ 
    println("insertOnDuplicateKey data result",data.get.mkString) 
    if (data.isSuccess){ 
     println(data.get) 
     val lastid=answers.last.id 
     Sync.lastActor !("upsert",tablename,lastid) 
    }else{ 
     //retry 
     self !("insertOnDuplicateKey",answers) 
    } 
    }) 
} 

,我嘗試在單個SQL使用sqlu但錯誤也許sqlu不供給線插補

這個演示不運作

case ("insertOnDuplicateKeyError",answers:List[Answer])=>{ 
    def buildSql(execpre:String,values: String,execafter:String): DBIO[Int] = sqlu"$execpre $values $execafter" 
    val execpre="insert into answer (aid,bid,sbid,qid,ups,author,uid,nick,pub_time,content,good,hot,id,reply,pic,spider_time) values " 
    val execafter=" ON DUPLICATE KEY UPDATE `aid`=values(aid),`bid`=values(bid),`sbid`=values(sbid),`qid`=values(qid),`ups`=values(ups),`author`=values(author),`uid`=values(uid),`nick`=values(nick),`pub_time`=values(pub_time),`content`=values(content),`good`=values(good),`hot`=values(hot),`id`=values(id),`reply`=values(reply),`pic`=values(pic),`spider_time`=values(spider_time)" 
    val valuesstr=answers.map(row=>("("+List(row.aid,row.bid,row.sbid,row.qid,row.ups,"'"+row.author+"'","'"+row.uid+"'","'"+row.nick+"'","'"+row.pub_time+"'","'"+row.content+"'",row.good,row.hot,row.id,row.reply,row.pic,"'"+row.spider_time+"'").mkString(",")+")")).mkString(",\n") 
    val insertOrUpdateAction=DBIO.seq(
    buildSql(execpre,valuesstr,execafter) 
) 
    DEST_DB.run(insertOrUpdateAction).onComplete(data=>{ 
    if (data.isSuccess){ 
     println("insertOnDuplicateKey data result",data) 
     //retry 
     val lastid=answers.last.id 
     Sync.lastActor !("upsert",tablename,lastid) 
    }else{ 
     self !("insertOnDuplicateKey2",answers) 
    } 
    }) 
} 

與一個MySQL同步工具階光滑 https://github.com/cclient/ScalaMysqlSync