playframework / anorm

The Anorm database library
https://playframework.github.io/anorm/
Apache License 2.0
240 stars 75 forks source link

Support batch queries #574

Closed gaeljw closed 1 year ago

gaeljw commented 1 year ago

Anorm Version (2.5.x / etc)

2.7.0

Expected Behavior

When using BatchSql for inserting multiple rows, I would expect Anorm to actually generate a single INSERT statement with several values rather than N INSERT statements.

In terms of performance, this can make a significative difference.

Actual Behavior

Anorm generates multiple INSERT statements.

Workaround

A good workaround is described at https://stackoverflow.com/questions/24573242/batch-insert-with-table-that-has-many-columns-using-anorm

This is relatively easy to implement but this is kinda ugly.

cchantep commented 1 year ago

Anorm is using addBatch and executeBatch() and so is compliant with JDBC semantics. If you want to use INSERT INTO tbl (n rows ...) as specific to some database, it's out of the JDBC batch features for me.

gaeljw commented 1 year ago

Ah I see, I didn't know this was specific to some databases and/or drivers. I agree this is not Anorm responsibility to implement this then. I will look a bit deeper at what my driver does and if there's a setting maybe (look's like some have a rewriteBatchedStatements option).

Thanks @cchantep