this post was submitted on 26 Jul 2023
1 points (100.0% liked)

Lemmy Code / App Technical

92 readers
1 users here now

The code and application behind Lemmy. Beta testing new releases, API coding, custom changes, adding new features, developers

See also: !lemmyperformance@lemmy.ml community, it's not always clear which one to put a topic into. "lemmycode" I'm trying to be more into actual code change proposals.

!lemmydev@lemm.ee

founded 2 years ago
MODERATORS
 

This is taking longer than other aggregate updates, and I think the join can be eliminated:

CREATE FUNCTION public.community_aggregates_comment_count() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
begin
  IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
update community_aggregates ca
set comments = comments + 1 from comment c, post p
where p.id = c.post_id
  and p.id = NEW.post_id
  and ca.community_id = p.community_id;
ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
update community_aggregates ca
set comments = comments - 1 from comment c, post p
where p.id = c.post_id
  and p.id = OLD.post_id
  and ca.community_id = p.community_id;

END IF;
return null;
end $$;

pg_stat_statements shows it as:

update community_aggregates ca set comments = comments + $15 from comment c, post p where p.id = c.post_id and p.id = NEW.post_id and ca.community_id = p.community_id

TRIGGER:

CREATE TRIGGER community_aggregates_comment_count AFTER INSERT OR DELETE OR UPDATE OF removed, deleted ON public.comment FOR EACH ROW EXECUTE FUNCTION public.community_aggregates_comment_count();

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here