Skip to content

fix(prompts): open the Topics tab's 24 hours and long windows in time - #1338

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:fix/topics-overview-speed
Oct 22, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:fix/topics-overview-speed

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

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):

Window Before
24h (the tab's default) 8.4–8.6 s → "Failed to load", every time
7d 2.9 s
30d 3.5 s
90d 5.9 s

Where the time goes

  • The page's read is topics_overview_window. Since 00156 the tab reads it for every range. Windows of 2 days or more read the daily rollups (00170).
  • The 24h branch is the slow one. It reads both windows' answers from prompt_results, row by row, each with its competitor_mentions jsonb.
  • The "20 s+" comes from the health report. It's topics_overview_aggregates in 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)

  1. 24 hours: the counts for both windows come from 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 shows Heap Fetches: 0. competitor_mentions is read only for the window's own answers, the half the competitor share counts.
  2. Longer windows:
    • Covering indexes on insights_prompt_daily (brand_id, day) and insights_competitor_prompt_daily (brand_id, day) carry every column the reads take, so the rollup reads are index-only.
    • They replace the plain (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.
    • Both tables get daily-ish autovacuum (autovacuum_vacuum_scale_factor / _insert_scale_factor 0.01, analyze 0.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.
  3. health_probe gains 'topics_overview_window', which times the window now() - p_days (1 day when unset).

Output is unchanged.

  • Before replacing the function, I compared 00170's topics_overview_window with 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_agg equality).
  • The long-window branch is 00170's, untouched.

Server: the health report's PROBES now time the tab's own reads (topics_overview_window at 1 and 90 days) instead of topics_overview_aggregates. The function itself stays, for the old web fallback.

Stacked on #1326, whose covering index the 24-hour read uses, with upstream/main merged in for 00235. This PR's own commit is 20300a38.

For @gkhngyk: production

  • Indexes: build them by hand first with 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.
  • Timing: could you time the four windows on production before and after? The dev DB only has a few hundred answers, so I can't measure the speed-up locally.
  • Deploy order: apply the migration before the server deploys. Otherwise an older health_probe rejects 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 feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00236 (after 00232 from fix(prompts): read the Prompts table's visibility and positions from an index #1326).
  2. Prompts → Topics. 24h, 7d, 30d and 90d all load, with the same figures as before.
  3. Read-only check of the rows: in a transaction, create 00170's function under another name and compare the outputs, as described above.
  4. Health report: collectHealthReport probes topics_overview_window at 1 and 90 days first.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) — no web change
  • yarn typecheck passes (run from web/) — no web change
  • yarn format:check passes (run from web/) — no web change
  • Changes are focused — one concern per PR

Server: npm run format, npm run lint and npm test pass (170 files, 2,007 tests). bash supabase/build-schema.sh was run, and the migration is applied on the dev DB and re-runnable.

🤖 Generated with Claude Code

cihan-gyk and others added 3 commits October 22, 2026 00:18
…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]>
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]>
@cihan-gyk

Copy link
Copy Markdown
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 topics_overview_aggregates, which the page hasn't read since 00156.

Changes:

On the dev DB the output is identical to 00170 over 7 windows.

Three things only you can do on prod:

  1. Build the new indexes concurrently by hand (and drop the two old ones the same way) before the migration runs.
  2. Time 24h/7d/30d/90d before and after. The dev DB is too small to show the gain.
  3. Apply the migration before the server deploys, so the health probe knows the new name.

It's stacked on #1326 and uses migration 00236.

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 22, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.sql matches a rebuild. Server tests (2,007), lint and Prettier pass.

@gkhngyk
gkhngyk merged commit 99c8d91 into ansvisor:main Oct 22, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants