Repository navigation
fix(prompts): open the Topics tab's 24 hours and long windows in time - #1338
Merged
Merged
Conversation
…an index Both window reads behind the Prompts table summed answer figures that idx_prompt_results_brand_created_cover doesn't carry, so every answer was a heap read (each row holds the answer text); on prod the page's read hit the 8 s timeout. Migration 00232 adds idx_prompt_results_brand_created_metrics (the cover index's key and columns plus visibility_score, mention_count, citation_count, mention_position and mentioned_entity_count; build it concurrently on prod first) and makes prompt_visibility_summaries plpgsql planned per call, security definer with the organization check, same rows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-speed # Conflicts: # supabase/schema.sql
Measured on production on 2026-10-22, the Topics tab's default 24 hours failed every time at the 8 s statement timeout, and 90 days took 5.9 s. Migration 00236: - topics_overview_window's 24-hour branch reads both windows' counts from idx_prompt_results_brand_created_metrics (ansvisor#1326) alone, and reads competitor_mentions for the window's answers only. On the dev DB it returns the same rows as 00170 over 7 windows. - Covering indexes on insights_prompt_daily and insights_competitor_prompt_daily (brand_id, day) make the long windows' rollup reads index-only. They replace the plain (brand_id, day) indexes, and both tables are autovacuumed about daily, as 00224 does for the answers. - health_probe gains 'topics_overview_window'. The morning health report now times the tab's own reads (24 hours and 90 days) instead of topics_overview_aggregates, which the page stopped reading at 00156; that fixed function's 20 s+ was the report's, not a user's. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
Author
|
@gkhngyk This one is for the Topics speed issue the user reported. On production the tab's default 24h fails every time at the 8 s timeout (8.4–8.6 s, Ansvisor brand), and 90d takes 5.9 s. The "20 s+" in the morning report was Changes:
On the dev DB the output is identical to 00170 over 7 windows. Three things only you can do on prod:
It's stacked on #1326 and uses migration 00236. |
gkhngyk
approved these changes
Oct 22, 2026
gkhngyk
left a comment
Contributor
There was a problem hiding this comment.
Looks good. On production, the largest brand, as a member:
| Window | Before | After |
|---|---|---|
| 24h | 20.9 s ("Failed to load") | 0.09 s |
| 7d | — | 0.09 s |
| 30d | 1.1 s | 0.9 s |
| 90d | 2.8 s | 1.5 s |
- Same rows. Before applying, I snapshotted the old function's rows for six windows across two brands: 24h, 36h, 7d, 30d and 90d. After 00236, all six are identical.
- Prod steps. I built the two cover indexes concurrently, then dropped the old
(brand_id, day)indexes concurrently, and applied 00236. Its function bodies match the file. Both rollup tables were vacuumed afterwards. - Branch. I merged main in now that #1326 is in;
schema.sqlmatches a rebuild. Server tests (2,007), lint and Prettier pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
From the user: the Topics overview has to open in under 4 s.
Measured on production (2026-10-22, Ansvisor brand, as the page runs it):
Where the time goes
topics_overview_window. Since 00156 the tab reads it for every range. Windows of 2 days or more read the daily rollups (00170).prompt_results, row by row, each with itscompetitor_mentionsjsonb.topics_overview_aggregatesin the morning health report: that fixed 30-day function is no longer a read of the page's, but the report still times it.Migration
00236_topics_overview_speed.sql(after #1336's 00235)idx_prompt_results_brand_created_metrics(fix(prompts): read the Prompts table's visibility and positions from an index #1326) alone, as an index-only scan; on the dev DB the plan showsHeap Fetches: 0.competitor_mentionsis read only for the window's own answers, the half the competitor share counts.insights_prompt_daily (brand_id, day)andinsights_competitor_prompt_daily (brand_id, day)carry every column the reads take, so the rollup reads are index-only.(brand_id, day)indexes from 00066. With the same key, every read that used those can use these, and the planner no longer picks the narrow one and fetches the rows anyway.autovacuum_vacuum_scale_factor/_insert_scale_factor0.01,analyze0.02), as 00224 did for the answers. The nightly refresh rewrites their latest days, and index-only scans need the visibility map to cover those.health_probegains'topics_overview_window', which times the windownow() - p_days(1 day when unset).Output is unchanged.
topics_overview_windowwith the new one in a rolled-back transaction on the dev DB, over 7 windows: four under 2 days, plus 7, 30 and 90 days. Every one returned identical rows (jsonb_aggequality).Server: the health report's
PROBESnow time the tab's own reads (topics_overview_windowat 1 and 90 days) instead oftopics_overview_aggregates. The function itself stays, for the old web fallback.Stacked on #1326, whose covering index the 24-hour read uses, with
upstream/mainmerged in for 00235. This PR's own commit is20300a38.For @gkhngyk: production
create index concurrently, and drop the two old(brand_id, day)indexes the same way, so the nightly refresh isn't held back. The migration then finds the work done; its header says so too.health_proberejects the new probe name, and the report shows "could not check" for Topics.Related issue
None. Performance report from the user (page speed queue).
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
00236(after00232from fix(prompts): read the Prompts table's visibility and positions from an index #1326).collectHealthReportprobestopics_overview_windowat 1 and 90 days first.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) — no web changeyarn typecheckpasses (run fromweb/) — no web changeyarn format:checkpasses (run fromweb/) — no web changeServer:
npm run format,npm run lintandnpm testpass (170 files, 2,007 tests).bash supabase/build-schema.shwas run, and the migration is applied on the dev DB and re-runnable.🤖 Generated with Claude Code