Skip to content

Microsoft.Data.Sqlite: Persist prepared statements across commands #14044

Description

@bricelam

We can cache/pool these on the opened connection to remove the overhead of re-compiling SQL for every new command.

If we add a connection pool (#13837) they can even be persisted across connections.

Activity

  1. self-assigned this
    on Nov 29, 2018
  2. added this to the Backlog milestone on Dec 3, 2018
  3. modified the milestones: Backlog, 3.0.0 on Jan 30, 2019
  4. bricelam commented on Mar 4, 2019

    @bricelam
    ContributorAuthor

    It looks like prepared statements aren't intended to be long-lived in SQLite. We'd need ericsink/SQLitePCL.raw#257 to fully avoid depleting the look-aside memory pool. Until then, we can keep the number of cached prepared statements relatively low.

    We could also consider having a threshold before we cache (e.g. we only cache a prepared statement on its second execution). Explicitly calling SqliteCommand.Prepare() would cause us to cache it immediately.

  5. roji commented on Mar 12, 2019

    @roji
    Member

    Noticed this a bit late.

    FWIW Npgsql's automatic preparation cache also has an upper limit per physical connection (100 by default IIRC), since prepared statements represent physical server-side resources. And similar to what you say, explicitly-prepared commands (with Prepare()) don't count and are immediately prepared.

    (In fact explicitly prepared commands remain prepared forever unless explicitly unprepared - they don't participate at all in the caching logic. It seemed to make sense at the time at least).

  6. 14 remaining items

  7. modified the milestones: Backlog, 7.0.0 on Oct 22, 2021
  8. lukasf commented on Feb 12, 2023

    @lukasf

    I noticed that statements that were prepared within a transaction cannot be used outside that transaction. And likely, statements prepared without transaction (or in a different transaction) cannot be used in a transaction. An InvalidOperationException is thrown otherwise. This effectively prevents usage of a prepared statement cache.

    What is the reasoning behind that? Prepared statements in sqlite can be used across transactions without any limitations. Only on very very old versions of sqlite (more than 10 years old), this was not allowed. But this was resolved very long ago. So I do not understand why Microsoft.Data.Sqlite prevents this, when it is all fine from sqlite side.

    I have been using prepared statements across transactions in an application for years, and I am surprised that I run into problems when moving to Microsoft.Data.Sqlite.

  9. removed their assignment
    on Jul 8, 2023
  10. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions