Skip to content

Commit 9d9356d

Browse files
feat(cat-voices): smaller proposals query scope (#3747)
* smaller proposals page query * update PR nr
1 parent 5bb87d0 commit 9d9356d

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

catalyst_voices/docs/performance/proposals_query.csv

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ docs_count ,filer ,avg_duration ,PR ,note
3434
7008 ,categories:finals ,0:00:01.294485 ,#3622 ,-
3535
14008 ,categories ,0:00:02.108506 ,#3622 ,-
3636
14008 ,categories:drafts ,0:00:01.585000 ,#3622 ,-
37-
14008 ,categories:finals ,0:00:05.024950 ,#3622 ,-
37+
14008 ,categories:finals ,0:00:05.024950 ,#3622 ,-
38+
712 ,categories ,0:00:00.139314 ,#3747 ,-
39+
712 ,categories:drafts ,0:00:00.138364 ,#3747 ,-
40+
712 ,categories:finals ,0:00:00.148680 ,#3747 ,-
41+
7008 ,categories ,0:00:00.168405 ,#3747 ,-
42+
7008 ,categories:drafts ,0:00:00.190470 ,#3747 ,-
43+
7008 ,categories:finals ,0:00:00.167145 ,#3747 ,-
44+
14008 ,categories ,0:00:00.253610 ,#3747 ,-
45+
14008 ,categories:drafts ,0:00:00.266390 ,#3747 ,-
46+
14008 ,categories:finals ,0:00:00.247054 ,#3747 ,-

catalyst_voices/packages/internal/catalyst_voices_repositories/lib/src/database/dao/proposals_v2_dao.dart

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -570,34 +570,25 @@ class DriftProposalsV2Dao extends DatabaseAccessor<DriftCatalystDatabase>
570570
/// - Identifies the newest version of each proposal
571571
/// - Uses: idx_documents_v2_type_id
572572
///
573-
/// 2. **version_lists**
574-
/// - Collects all version ids for each proposal into comma-separated string
575-
/// - Ordered by ver ASC for consistent version history
576-
/// - Used to show version dropdown in UI
577-
///
578-
/// 3. **latest_actions**
573+
/// 2. **latest_actions**
579574
/// - Groups all proposal actions by ref_id and finds MAX(ver)
580575
/// - Ensures we only check the most recent action per proposal
581576
/// - Uses: idx_documents_v2_type_ref_id
582577
///
583-
/// 4. **action_status**
578+
/// 3. **action_status**
584579
/// - Joins actual action documents with latest_actions
585580
/// - Extracts action type ('draft'/'final'/'hide') from JSON content
586581
/// - Extracts ref_ver which may point to specific proposal version
587582
/// - COALESCE defaults to 'draft' when action field is missing
588583
/// - Uses: idx_documents_v2_type_ref_id_ver
589584
///
590-
/// 5. **effective_proposals**
585+
/// 4. **effective_proposals**
591586
/// - Applies version resolution logic:
592587
/// * Hide action: Filtered out by WHERE NOT EXISTS
593588
/// * Final action with ref_ver: Uses ref_ver (specific pinned version)
594589
/// * Final action without ref_ver OR draft OR no action: Uses max_ver (latest)
595590
/// - LEFT JOIN ensures proposals without actions are included (default to draft)
596591
///
597-
/// 6. **comments_count**
598-
/// - Counts comments per proposal version
599-
/// - Joins on both ref_id and ref_ver for version-specific counts
600-
///
601592
/// **Final Query:**
602593
/// - Joins documents_v2 with effective_proposals to get full document data
603594
/// - LEFT JOINs with comments, favorites, and template for enrichment
@@ -625,24 +616,12 @@ class DriftProposalsV2Dao extends DatabaseAccessor<DriftCatalystDatabase>
625616

626617
final cteQuery =
627618
'''
628-
WITH latest_proposals AS (
619+
WITH latest_proposals AS (
629620
SELECT id, MAX(ver) as max_ver
630621
FROM documents_v2
631622
WHERE type = ?
632623
GROUP BY id
633624
),
634-
version_lists AS (
635-
SELECT
636-
id,
637-
GROUP_CONCAT(ver, ',') as version_ids_str
638-
FROM (
639-
SELECT id, ver
640-
FROM documents_v2
641-
WHERE type = ?
642-
ORDER BY id, ver ASC
643-
)
644-
GROUP BY id
645-
),
646625
latest_actions AS (
647626
SELECT ref_id, MAX(ver) as max_action_ver
648627
FROM documents_v2
@@ -661,39 +640,46 @@ class DriftProposalsV2Dao extends DatabaseAccessor<DriftCatalystDatabase>
661640
effective_proposals AS (
662641
SELECT
663642
lp.id,
643+
-- Business Logic: Use specific version if final, otherwise latest
664644
CASE
665645
WHEN ast.action_type = 'final' AND ast.ref_ver IS NOT NULL AND ast.ref_ver != '' THEN ast.ref_ver
666646
ELSE lp.max_ver
667647
END as ver,
668-
ast.action_type,
669-
vl.version_ids_str
648+
ast.action_type
670649
FROM latest_proposals lp
671650
LEFT JOIN action_status ast ON lp.id = ast.ref_id
672-
LEFT JOIN version_lists vl ON lp.id = vl.id
673651
WHERE NOT EXISTS (
652+
-- Business Logic: Hide action hides all versions
674653
SELECT 1 FROM action_status hidden
675654
WHERE hidden.ref_id = lp.id AND hidden.action_type = 'hide'
676655
)
677-
),
678-
comments_count AS (
679-
SELECT
680-
c.ref_id,
681-
c.ref_ver,
682-
COUNT(*) as count
683-
FROM documents_v2 c
684-
WHERE c.type = ?
685-
GROUP BY c.ref_id, c.ref_ver
686656
)
687657
SELECT
688658
$proposalColumns,
689659
$templateColumns,
690-
ep.action_type,
691-
ep.version_ids_str,
692-
COALESCE(cc.count, 0) as comments_count,
660+
ep.action_type,
661+
662+
-- Only executes for the rows in the page
663+
(
664+
SELECT GROUP_CONCAT(v_list.ver, ',')
665+
FROM (
666+
SELECT ver
667+
FROM documents_v2 v_sub
668+
WHERE v_sub.id = p.id AND v_sub.type = ?
669+
ORDER BY v_sub.ver ASC
670+
) v_list
671+
) as version_ids_str,
672+
673+
-- Only executes for the rows in the page
674+
(
675+
SELECT COUNT(*)
676+
FROM documents_v2 c
677+
WHERE c.ref_id = p.id AND c.ref_ver = p.ver AND c.type = ?
678+
) as comments_count,
679+
693680
COALESCE(dlm.is_favorite, 0) as is_favorite
694681
FROM documents_v2 p
695682
INNER JOIN effective_proposals ep ON p.id = ep.id AND p.ver = ep.ver
696-
LEFT JOIN comments_count cc ON p.id = cc.ref_id AND p.ver = cc.ref_ver
697683
LEFT JOIN documents_local_metadata dlm ON p.id = dlm.id
698684
LEFT JOIN documents_v2 t ON p.template_id = t.id AND p.template_ver = t.ver AND t.type = ?
699685
WHERE p.type = ? $whereClause
@@ -714,13 +700,20 @@ class DriftProposalsV2Dao extends DatabaseAccessor<DriftCatalystDatabase>
714700
return customSelect(
715701
cteQuery,
716702
variables: [
717-
Variable.withString(DocumentType.proposalDocument.uuid),
703+
// CTE Variables
704+
// latest_proposals, latest_actions, action_status
718705
Variable.withString(DocumentType.proposalDocument.uuid),
719706
Variable.withString(DocumentType.proposalActionDocument.uuid),
720707
Variable.withString(DocumentType.proposalActionDocument.uuid),
708+
// Select Subquery Variables (Order matters!)
709+
// version_ids_str subquery, comments_count subquery
710+
Variable.withString(DocumentType.proposalDocument.uuid),
721711
Variable.withString(DocumentType.commentDocument.uuid),
712+
// Main Join Variables
713+
// template join, main WHERE
722714
Variable.withString(DocumentType.proposalTemplate.uuid),
723715
Variable.withString(DocumentType.proposalDocument.uuid),
716+
// Limit/Offset
724717
Variable.withInt(size),
725718
Variable.withInt(page * size),
726719
],

0 commit comments

Comments
 (0)