Skip to content

perf(insights): serve topic-filtered reads from prompt-grain rollups #862

Closed

Description

@gkhngyk

Problem

Every Insights read that filters by topic still scans raw prompt_results. The daily rollups (00066) only have a brand-level grain, so dailyWindow() sends any call with a topicId to the raw RPCs. On large topics those raw scans are now the slowest queries in the app.

On the largest topic (91 prompts, ~27k results) the topic detail page's aggregates take 5–15s each when run together:

RPC (topic-filtered) Alone Run together with the others
competitor_aggregates, all time 7.2s 11–15s
ai_visibility_aggregates, all time 4.8s 9.5–13.8s
visibility_rate_trend, all time — ~13s

Most of the cost is unpacking every answer's competitor_mentions jsonb. The topic scan itself is cheap.

The page failed outright under the authenticated role's 8s timeout. #861 made it load by running these reads on the service-role client (120s, #860), after checking access on the caller's client. That keeps the page up, but the first load still takes 10–15s, and it grows with the topic.

Proposal

Serve topic-filtered reads from prompt-grain rollups, so their cost scales with days in the window rather than with accumulated results.

1. Prompt-grain measures in the rollups

Extend the rollup tables that already carry prompt_id, and fill the new columns in refresh_insights_daily. The mention_count column added in 00098 is the pattern to follow.

  • insights_prompt_daily, one row per (day, prompt, engine, region), gains the additive brand measures insights_brand_daily holds at brand grain:
    • answer, mention and citation counts;
    • total mentions and total citations;
    • visibility sums;
    • positive-sentiment count;
    • position sum and count.
  • insights_competitor_prompt_daily holds competitor sightings. It gains what competitor_aggregates and ai_visibility_aggregates read per competitor: citation count, visibility sum, and position sum and count.
    • It stays sparse, holding sightings only. The comparison already divides competitor figures by the brand's answer count, so it does not need a dense row for every competitor in every answer.

2. Topic support in the daily read functions

  • Give the *_daily read functions a p_topic_id argument: insights_aggregates_daily, ai_visibility_aggregates_daily, competitor_aggregates_daily, share_of_voice_aggregates_daily, visibility_rate_trend_daily, visible_prompt_stats_daily and tracked_prompt_count_daily.
  • When p_topic_id is set, the function reads the prompt-grain tables joined through prompts on the current topic_id. A prompt moved to another topic then still takes its history along, which is the reason topic filters stayed on the raw path in the first place.
  • In web/src/lib/actions/tracking.ts, let dailyWindow() return the window for topic-filtered calls too.

3. Backfill and verify

  • Rebuild the rollups for the full retained history, as was done for 00098.
  • Before switching reads over, compare old and new outputs for several topics across brands of different sizes. The numbers should match, as they did for 00098.

4. Remove the stopgap

  • Once topic reads come from the rollups, getTopicDetail no longer needs the service-role client. Remove it so the page runs on the caller's own client again.

Expected result

The topic detail page, and topic filters on Insights and Competitors, respond in well under a second regardless of topic size.

Notes

  • Today's data is folded in when a run stamps, as with the other rollups. Topic views would therefore follow the same end-of-run timing as Insights.
  • Keep the per-row width small. insights_prompt_daily grows by about one row per result on the largest brand, roughly 4k a day.

Activity

  1. added
    enhancement
    ai visibility
    topics
    on Sep 30, 2026
  2. arambu1a commented on Oct 2, 2026

    @arambu1a
    Contributor

    Hi @gkhngyk I'll work on this

  3. arambu1a commented on Oct 2, 2026

    @arambu1a
    Contributor

    Opened #867 for this.

  4. added a commit that references this issue on Oct 15, 2026
    c73dbbd
  5. added a commit that references this issue on Oct 17, 2026
    a0f1dee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai visibilityenhancementtopics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions