tusken icon indicating copy to clipboard operation
tusken copied to clipboard

Prepared statements

Open aleclarson opened this issue 3 years ago • 0 comments

https://www.postgresql.org/docs/current/sql-prepare.html

Prepared statements potentially have the largest performance advantage when a single session is being used to execute a large number of similar statements. The performance difference will be particularly significant if the statements are complex to plan or rewrite, e.g., if the query involves a join of many tables or requires the application of several rules. If the statement is relatively simple to plan and rewrite but relatively expensive to execute, the performance advantage of prepared statements will be less noticeable.

The API would look like this:

const preparedQuery =
  db.select(...)
    .innerJoin(...)
    .innerJoin(...)
    .prepare('unique_name')

// Release from memory
preparedQuery.dealloc()

Note that two separate queries cannot share the same name.

Ideas 🤔

  • We could generate a hash that allows automatic naming of prepared statements based on the query.
  • We could inject a LRU cache if enabled within tusken.config.ts

aleclarson avatar Sep 12 '22 00:09 aleclarson