Skip to content

Avoid redundant SELECT in execute_insert#386

Open
pctablet505 wants to merge 2 commits into
omnilib:mainfrom
pctablet505:fix/execute-insert-redundant-query
Open

Avoid redundant SELECT in execute_insert#386
pctablet505 wants to merge 2 commits into
omnilib:mainfrom
pctablet505:fix/execute-insert-redundant-query

Conversation

@pctablet505

Copy link
Copy Markdown

Description

execute_insert runs an insert and then issues a separate SELECT last_insert_rowid() query to fetch the new row id. That second query is unnecessary: sqlite3's cursor already exposes the same value via cursor.lastrowid right after the insert executes. This drops the extra round trip.

The return type moves from a sqlite3.Row to a plain tuple, but result[0] (the common access pattern for reading the inserted id) keeps working the same way.

Fixes: #379

execute_insert previously ran an extra "SELECT last_insert_rowid()"
query after every insert. sqlite3's cursor already exposes this value
via cursor.lastrowid, so we can return it directly and skip the round
trip, cutting an insert's overhead noticeably.

Fixes omnilib#379
Cover the execute_insert helper directly: normal single-row insert
(asserting both the (rowid,) tuple shape and result[0] access),
sequential inserts, multi-row insert returning the final rowid, and
the no-insert cases where lastrowid retains the connection's last
insert rowid or reports 0 when no insert has occurred.
@pctablet505
pctablet505 marked this pull request as ready for review July 17, 2026 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

execute_insert does redundant query

1 participant