opu*_*111 26 mysql sql scala slick typesafe
在Slick 3.0中执行批量insertOrUpdate的正确方法是什么?
我正在使用适当查询的MySQL
INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码非常慢:-(
// 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
Run Code Online (Sandbox Code Playgroud)
我正在寻找的是相当于
def insertOrUpdate(values: Iterable[U]): DriverAction[MultiInsertResult, NoStream, Effect.Write]
Run Code Online (Sandbox Code Playgroud)
Sea*_*ira 40
有几种方法可以使这段代码更快(每一种都应该比前面的代码更快,但它逐渐减少了惯用的代码):
在slick-pg 0.16.1+上运行insertOrUpdateAll
而不是insertOrUpdate
if
await(run(TableQuery[FooTable].insertOrUpdateAll rows)).sum
Run Code Online (Sandbox Code Playgroud)一次运行您的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
Run Code Online (Sandbox Code Playgroud)下拉到JDBC级别并一次性运行upsert(通过这个答案的想法):
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()
}
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
12407 次 |
最近记录: |