simonw / sqlite-history

Track changes to SQLite tables using triggers
Apache License 2.0
108 stars 2 forks source link

Alternatives to a sequential version and optimising for UPDATE speed #9

Open metamoof opened 1 year ago

metamoof commented 1 year ago

I came across this from your blog post where you basically stated that you optimised for space.

This is a valid choice, but one that should be clearly stated on the README, as the tradeoff is that all those CASE statements will be a drag for every UPDATE, especially the more columns a table gets.

An alternative way to optimise this would be to INSERT the whole changed line into the table in all cases, and then run a maintenance script that would replace the duplicate values with NULL at a suitable window, before any vacuuming. This would make the table bloat and shrink, but would make writing to the table quicker.

Also, you’re adding a SELECT operation to every UPDATE and DELETE. I’m unaware of the specific internals of SQLite, but I’m pretty certain that this addition to the operation is not going to be O(1) and will significantly slow down updates and deletes in large tables.

That being said, this SELECT is all to do with the _version column. You may wish to consider:

If you finally do decide to keep the _version column, at least consider creating an index over Id and _version to make the search quicker.