Commit b407d87
committed
feat(models): optimize user loading from 3 queries to 1 using JSON aggregation
Reduces FindUserWithRefreshToken from 3 separate queries (user + identities + factors)
to a single query using json_agg subqueries. This optimization impacts the
/token endpoint (~45% of total traffic) and /user endpoint by eliminating 2 database
round-trips per call. Since both of those endpoints call FindUserWithRefreshToken twice
it removes 4 database roundtrips per request.
Performance impact:
- Query execution: 399µs → 209µs (47.6% faster)
- Memory allocations: 18.4KB → 7.1KB (61% reduction)
- Allocation count: 299 → 117 allocs (61% reduction)
- /token throughput: +20.3% (55.18 vs 45.87 req/s) in local testing
- /token latency: -16.8% (181ms vs 218ms)
Replaces Pop ORM .Eager() pattern with explicit SQL column enumeration and
coalesce(json_agg()) for related entities. No changes to User struct or API.1 parent effd662 commit b407d87
1 file changed
Lines changed: 63 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
604 | 605 | | |
605 | 606 | | |
606 | 607 | | |
607 | | - | |
608 | | - | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
609 | 660 | | |
610 | 661 | | |
611 | 662 | | |
612 | 663 | | |
613 | 664 | | |
614 | 665 | | |
615 | | - | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
616 | 676 | | |
617 | 677 | | |
618 | 678 | | |
| |||
0 commit comments