Request for SQL experts: Lemmy PR to combine action tables
Request for SQL experts: Lemmy PR to combine action tables
Combine action tables by dullbananas · Pull Request #4459 · LemmyNet/lemmy
Lemmy currently uses distinct tables like post_like: (post_id, person_id, score)
and post_saved
. Unfortunately this causes performance issues when we have to join many of these tables to create views.
One suggestion in this PR, is to combine these into a single post_action
table, with a lot of optional columns depending on the action. This solution scares me a little, because I'm afraid we might lose data integrity, and many of our constraints with so many optional columns.
Is there a better way of doing this in SQL?
Have you considered keeping the data writes to their current tables and deploying an indexed materialized view over those tables for more efficient reads? You can normalize your data and eat your denormalized cake too!
Let me know if you have any technical questions about how to.
We stopped using materialized views in lemmy a few years ago, because of caching / stale data issues.
That's fair - though you can use trigger based refreshes, there will always be some kind of inconsistency window. That was my only real silver bullet suggestion. Otherwise, you'll just need to modify the underlying structure.
From what I've read of the change, it doesn't look too dangerous. My only concern would be around concurrent writes where someone upvotes and saves a post in rapid succession. The logic for both actions needs to support a
post_action
record existing or not existing and potentially starting to exist mid-write. There will be some ugly edge cases with that approach, but it should be doable if carefully done.Sorry if that isn't super helpful, but thanks for your work maintaining the platform. It's appreciated!