admin

Slick 3.0批量插入或更新(upsert)

sql

在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);

MySQL批量插入或更新

这是我当前的代码,它很慢:-(

// 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]

阅读 190

收藏
2021-05-10

共1个答案

admin

您可以通过多种方法使此代码更快(每个代码都 应该 比前面的代码更快,但是它的习惯用法越来越少):

  • 运行insertOrUpdateAll而不是insertOrUpdateif on slick-pg 0.16.1+

    await(run(TableQuery[FooTable].insertOrUpdateAll rows)).sum
    
  • 一次运行所有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(通过此答案的想法):

    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()
    }

2021-05-10