canonical / sqlair

Friendly type mapping for SQL databases
Apache License 2.0
16 stars 8 forks source link

feat: add a cache for driver prepared statements #149

Closed Aflynn50 closed 5 days ago

Aflynn50 commented 2 months ago

When the same SQLair query is executed multiple times against the database it is recompiled by the database every time. This is inefficient.

It is possible to instead prepare a SQL query against the database driver and cache it in SQLair to allow for reuse of the query.

With this change, when a SQLair statement is executed directly against a database it is first prepared and then executed, the prepared statement is put in the cache for reuse. If the statement is executed in a transaction, the SQLair statement is looked up in the cache but if it is not found it is not prepared on the driver. This is due to the issues in #117.

Aflynn50 commented 5 days ago

Not too surprising that the times go up slightly. Because of the issue with transactions that meant we couldn't cache their statements Juju does not benefit at all from this caching.