Speed up Reports, transactions and account pages
Problem
On a family with a long investment history, Reports took 3–5 s per month view, and transactions and account pages ~1.1 s. Three queries were responsible:
- Realized gains by tax treatment preloaded every holding of each account with a sale in the period: a row per security per day, mostly zero-quantity rows for positions sold long ago (hundreds of thousands of records, ~1.9 s per render).
- Investment flows joined entries and accounts twice under different aliases, so the date filter hit only one copy and Postgres paired every Contribution/Withdrawal with every entry of the family (547 ms for a one-row total).
- "Transfer?" hints compared every inflow with every outflow of the family before applying the page's id filter (~22 million pairs, ~0.6 s on every transactions and account page).
Behavior
No visible change other than speed. Each commit explains its fix:
Trade.preload_basis_holdingsloads, in one index-backed LATERAL query, only the holding each sale is measured against.InvestmentFlowStatementstarts fromTransactionwith the family's accounts as an entry filter, so each join appears once.transfer_match_candidatesbounds both legs by the page's date span widened by the match window before pairing. No pair the id filter keeps is dropped.
Validation
On a copy of a real family (1,015 sales since 2019, ~11k entries):
- Realized gains identical for every month and for all-time. Preload 1.9 s → 22 ms (month), 22.4 s → 1.0 s (all-time).
- Investment-flow totals identical for every month since 2018 and the 30-day, 365-day and all-time periods, for the whole family and a single user. 547 ms → 4 ms.
- Transfer candidates identical for 220 pages (every page of history plus 30 sampled across years, three window/tolerance settings). Transactions and account pages ~1.1 s → ~0.4 s.
- Reports month view 3–5 s → 0.4 s.
- Pre-PR checks:
bin/rails test: 17,937–17,940 runs, 3 failures, all unrelated to this change:Settings::LlmUsagesControllerTest(2 tests) andMobileReleaseWorkflowTest#test_automatic_TestFlight_accepts_only_canonical_calendar_tags. The same 3 fail on three other branches that touch unrelated code; the TestFlight one also fails on cleanmainlocally.- RuboCop, erb_lint, Biome and Brakeman: clean.
- System tests not run; there are no UI changes.
🤖 Generated with Claude Code
Summary by cubic
Speeds up Reports, transactions, and account pages on families with long investment histories, with no visible change to results. Three slow queries were fixed: realized gains preloaded every holding of each account with a sale, investment flow totals joined entries twice so the date filter hit only one copy, and "Transfer?" hints compared every inflow with every outflow before applying the page's filter.
On a copy of a real family, all outputs are identical. Monthly gains preload drops from 1.9 s to 22 ms, investment flow totals from 547 ms to 4 ms, transfer hint searches from ~0.6 s to ~0.35 s per page, Reports renders from 3–5 s to 0.4 s, and transactions/account pages from ~1.1 s to ~0.4 s.
- Realized gains —
Trade.preload_basis_holdingsloads each sale's basis position in one index-backed LATERAL query instead of every holding of the account's history. When two snapshots share a date (a security remap can leave them in different currencies), every path picks the account's currency, then the lower id. - Investment flows —
InvestmentFlowStatementstarts fromTransactionwith the family's accounts as an entry filter, so each join appears once and the date filter applies to the entries. - Transfer hints —
transfer_match_candidatesbounds both legs by the page's date span widened by the match window before pairing; no pair the id filter keeps is dropped. A compositeentries (entryable_type, entryable_id)index backs the entry lookups that narrowing adds; the oldentryable_typeindex stays because a failed concurrent build could otherwise leave entries with neither.
Summary by CodeRabbit
- Bug Fixes
- Investment flow statements now include transactions only from accounts belonging to the selected family.
- Transfer matching limits counterpart candidates to the configured date window around the selected transactions.
- Gains-by-tax-treatment reports use holdings and cost-basis data associated with each sale, with consistent selection when holdings share a date.
- Performance
- Gains report calculations and trade cost-basis lookups reduce repeated data fetching, while preserving consistent results across preloaded and individually retrieved holdings.