Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADP-2565] Improve performance of insertMany with prepared statement #4699

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/delta-table/src/Database/Table/SQLite/Simple/Exec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,22 @@ selectWhere expr = queryNamed . Stmt.selectWhere expr
insertOne :: IsTableSql t => Row t -> proxy t -> SqlM ()
insertOne row proxy = executeOne (Stmt.insertOne proxy) row

-- | Insert many rows into a database table.
--
-- As an optimization, we use a single prepared SQL statement.
insertMany :: IsTableSql t => [Row t] -> proxy t -> SqlM ()
insertMany rows proxy = for_ rows (`insertOne` proxy)
insertMany rows proxy =
ReaderT $ \conn -> do
-- The 'query' string contains '?' which will be substituted
-- for the values in the row, but no named params.
let (query, _noBindings) = renderStmt $ Stmt.insertOne proxy
Sqlite.withStatement conn query $ \stmt ->
for_ rows $ \row ->
-- From <https://www.sqlite.org/cintro.html>:
-- Each call to sqlite3_bind() overrides
-- prior bindings on the same parameter.
Sqlite.withBind stmt row (Sqlite.nextRow stmt)
:: IO (Maybe [Bool]) -- dummy type, we expect 'Nothing'

updateWhere
:: IsTableSql t
Expand Down
Loading