main
tag: launch-2026-06-01
tag: magicmirror-v0.1.1
tag: magicmirror-v0.3.0
tag: mnw-cli-v0.1.2
tag: mnw-cli-v0.1.3
tag: mnw-cli-v0.1.4
tag: pom-v0.4.1
tag: pom-v0.4.2
tag: pom-v0.4.3
tag: pom-v0.4.4
tag: pom-v0.4.5
tag: wam-v0.3.0
tag: wam-v0.3.1
Files
Commits
Tags
Notes
Issues
Rewrite get_user_purchases has_new_version subquery
Replace double-nested correlated EXISTS/NOT EXISTS with two LATERAL
COUNT joins (total versions with s3_key vs downloaded count). The old
pattern scanned the versions table per-row per-purchase; the new
pattern counts once per item. Improves library page query performance
as users accumulate purchases.
Co-Authored-By
Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2 files changed,
+12 insertions,
-9 deletions
43
43
- [x] Fix XSS: escape `s.category` and `s.url` in search suggestions innerHTML (discover.html)
44
44
- [x] Fix XSS: replace inline `onsubmit` handler with `addEventListener` in collections.js
45
45
- [x] Fix a11y: convert `<span onclick>` save buttons to `<button>` with `aria-label` on discover cards
46
-
- [ ] Performance: consider rewriting double-nested correlated subquery in `get_user_purchases` (has_new_version) to use LEFT JOIN or CTE
46
+
- [x] Performance: rewrite `get_user_purchases` double-nested correlated subquery — replaced with LATERAL joins counting total versions vs downloaded versions
47
47
- [x] Remove unused import `spawn_email` in `stripe/checkout/cart.rs`
48
48
- [x] Remove dead code `update_app_sync_sub_tier` in `db/app_sync.rs`
49
49
505
505
p.purchased_at,
506
506
(i.price_cents = 0) as is_free,
507
507
lk.key_code as license_key_code,
508
-
EXISTS (
509
-
SELECT 1 FROM versions v
510
-
WHERE v.item_id = i.id AND v.s3_key IS NOT NULL
511
-
AND NOT EXISTS (
512
-
SELECT 1 FROM user_downloads ud
513
-
WHERE ud.user_id = p.buyer_id AND ud.version_id = v.id
514
-
)
515
-
) as has_new_version
508
+
(vc.total_versions > 0 AND vc.total_versions > COALESCE(dc.downloaded_count, 0)) as has_new_version
516
509
FROM purchases p
517
510
JOIN items i ON p.item_id = i.id
518
511
JOIN projects proj ON i.project_id = proj.id
519
512
JOIN users u ON proj.user_id = u.id
520
513
LEFT JOIN license_keys lk ON lk.item_id = p.item_id AND lk.owner_id = p.buyer_id AND lk.revoked_at IS NULL
514
+
LEFT JOIN LATERAL (
515
+
SELECT COUNT(*) AS total_versions
516
+
FROM versions v
517
+
WHERE v.item_id = i.id AND v.s3_key IS NOT NULL
518
+
) vc ON true
519
+
LEFT JOIN LATERAL (
520
+
SELECT COUNT(*) AS downloaded_count
521
+
FROM user_downloads ud
522
+
WHERE ud.user_id = p.buyer_id AND ud.item_id = i.id
523
+
) dc ON true
521
524
WHERE p.buyer_id = $1
522
525
ORDER BY p.item_id, p.purchased_at DESC
523
526
) deduped