Skip to content

Commit c5b115e

Browse files
rotarurgusinacio
andauthored
perf: speed up startup setup queries (#625)
refactored the queries for optimization Co-authored-by: Gustavo Inacio <[email protected]>
1 parent f9453c4 commit c5b115e

5 files changed

+38
-50
lines changed

.sqlx/query-fef5849dc64c15d1188d6398c93a59bfaafd9a38cf342739cdabf8b7bba073d3.json renamed to .sqlx/query-11a939012cce92b3c148c22f13b2e81089b7b27af2a1dd22a8c6b8da561b6ec1.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-67c8c6323c92732bb994593223ccb4f7b9b4a4bda786542c18b404d2287e78a7.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.sqlx/query-8fe28629e2453852a41abd452ed519167d3f358d25aa02f306779270a084f8c3.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

crates/tap-agent/src/agent/sender_accounts_manager.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,11 @@ impl State {
415415
FROM scalar_tap_receipts
416416
GROUP BY signer_address, allocation_id
417417
)
418-
SELECT DISTINCT
418+
SELECT
419419
signer_address,
420-
(
421-
SELECT ARRAY
422-
(
423-
SELECT DISTINCT allocation_id
424-
FROM grouped
425-
WHERE signer_address = top.signer_address
426-
)
427-
) AS allocation_ids
428-
FROM grouped AS top
420+
ARRAY_AGG(allocation_id) AS allocation_ids
421+
FROM grouped
422+
GROUP BY signer_address
429423
"#
430424
)
431425
.fetch_all(&self.pgpool)
@@ -461,18 +455,11 @@ impl State {
461455

462456
let nonfinal_ravs_sender_allocations_in_db = sqlx::query!(
463457
r#"
464-
SELECT DISTINCT
458+
SELECT
465459
sender_address,
466-
(
467-
SELECT ARRAY
468-
(
469-
SELECT DISTINCT allocation_id
470-
FROM scalar_tap_ravs
471-
WHERE sender_address = top.sender_address
472-
AND NOT last
473-
)
474-
) AS allocation_id
475-
FROM scalar_tap_ravs AS top
460+
ARRAY_AGG(DISTINCT allocation_id) FILTER (WHERE NOT last) AS allocation_ids
461+
FROM scalar_tap_ravs
462+
GROUP BY sender_address
476463
"#
477464
)
478465
.fetch_all(&self.pgpool)
@@ -481,7 +468,7 @@ impl State {
481468

482469
for row in nonfinal_ravs_sender_allocations_in_db {
483470
let allocation_ids = row
484-
.allocation_id
471+
.allocation_ids
485472
.expect("all RAVs should have an allocation_id")
486473
.iter()
487474
.map(|allocation_id| {

justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ psql-down:
3434

3535
migrate:
3636
sqlx migrate run --database-url postgresql://postgres:postgres@127.0.0.1:5432
37+

0 commit comments

Comments
 (0)