Ability to batch multiple counter cache updates for same row
lightyrs opened this issue Β· 4 comments
π @magnusvk
I've been using this great gem for awhile at joinsubtext.com, however, have recently been running into deadlocking issues as we scale. This idea is a little different than the other open deadlocking issue #263.
I have multiple counter caches defined on a model like this:
class Message < ApplicationRecord
belongs_to :broadcast
counter_culture :broadcast, column_name: proc { |model| model.draft? ? nil : "messages_count" }
counter_culture :broadcast, column_name: proc { |model| model.delivered? ? "delivered_messages_count" : nil }
end
So when I look at the postgres logs and see something like below, I wonder if it would be possible to combine these updates into a single UPDATE
.
Process 26355: UPDATE "broadcasts" SET "messages_count" = COALESCE("messages_count", 0) + 1 WHERE "broadcasts"."id" = $1
Process 29749: UPDATE "broadcasts" SET "delivered_messages_count" = COALESCE("delivered_messages_count", 0) + 1 WHERE "broadcasts"."id" = $1
Thanks for your time.
ππ»βsmall world!
This is a great idea! These can and should be consolidated. However taking a quick peek here it looks like that's a pretty big refactor from the current code. I'd be open to a pull request if you have time to work on this! :)
@magnusvk Thanks for the quick reply. I will look into an implementation and submit a PR if I come up with something decent.
Closing for inactivity.