Repository navigation
Description
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 measuresinsights_brand_dailyholds 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_dailyholds competitor sightings. It gains whatcompetitor_aggregatesandai_visibility_aggregatesread 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
*_dailyread functions ap_topic_idargument:insights_aggregates_daily,ai_visibility_aggregates_daily,competitor_aggregates_daily,share_of_voice_aggregates_daily,visibility_rate_trend_daily,visible_prompt_stats_dailyandtracked_prompt_count_daily. - When
p_topic_idis set, the function reads the prompt-grain tables joined throughpromptson the currenttopic_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, letdailyWindow()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,
getTopicDetailno 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_dailygrows by about one row per result on the largest brand, roughly 4k a day.
Activity
arambu1a commentedon Oct 2, 2026
Hi @gkhngyk I'll work on this
arambu1a commentedon Oct 2, 2026
Opened #867 for this.
perf(insights): serve topic-filtered reads from prompt-grain rollups
perf(insights): serve topic-filtered reads from prompt-grain rollups (a…