且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-27 09:41:52

有几种方法可以使此代码更快(每个应该都比前面的代码要快,但是它会逐渐变得更快).习惯用法少一些):

There are several ways that you can make this code faster (each one should be faster than the preceding ones, but it gets progressively less idiomatic-slick):

  • 如果在slick-pg 0.16.1+上运行insertOrUpdateAll而不是insertOrUpdate

await(run(TableQuery[FooTable].insertOrUpdateAll rows)).sum

  • 一次运行所有DBIO事件,而不是等每个事件都提交之后再运行下一个:

  • Run your DBIO events all at once, rather than waiting for each one to commit before you run the next:

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