Skip to content

perf(spp_programs): add composite indexes for frequent query patterns#118

Open
kneckinator wants to merge 1 commit into19.0from
ken/optimize_spp_program_phase_2
Open

perf(spp_programs): add composite indexes for frequent query patterns#118
kneckinator wants to merge 1 commit into19.0from
ken/optimize_spp_program_phase_2

Conversation

@kneckinator
Copy link
Contributor

Summary

Add composite database indexes for the three most frequent query patterns in spp_programs. No version bump needed — indexes are created idempotently via init().


Changes

Index Table Columns Justification
spp_entitlement_cycle_id_partner_id_idx spp_entitlement (cycle_id, partner_id) prepare_entitlements duplicate check searches by these columns on every batch
spp_entitlement_cycle_id_state_idx spp_entitlement (cycle_id, state) Cycle computed fields (total_amount, show_approve_button, entitlements_count) filter by cycle + state
spp_program_membership_program_id_state_idx spp_program_membership (program_id, state) get_beneficiaries() and count_beneficiaries() filter by program + state on every async batch dispatch

Indexes are created via init() with CREATE INDEX IF NOT EXISTS, so they're safe to apply on existing databases without migration.


Context

Phase 2 of 9 in the spp_programs performance optimization effort. Independent of Phase 1 (SQL constraints).


Test Plan

  • ./scripts/test_single_module.sh spp_programs passes
  • Verify indexes exist after install/upgrade:
SELECT indexname, indexdef FROM pg_indexes
WHERE tablename IN ('spp_entitlement', 'spp_program_membership')
  AND indexname LIKE '%_idx';

Add indexes on spp_entitlement(cycle_id, partner_id),
spp_entitlement(cycle_id, state), and
spp_program_membership(program_id, state) to avoid sequential scans
during prepare_entitlements duplicate checks, cycle computed fields,
and beneficiary pagination.
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the performance of the spp_programs module by strategically adding composite database indexes. These indexes are designed to accelerate common and performance-critical database queries, specifically targeting operations like entitlement duplicate checks, calculations for cycle-computed fields, and beneficiary retrieval. The implementation ensures that these performance improvements can be deployed safely and repeatedly across environments without requiring complex database migrations.

Highlights

  • Performance Optimization: Introduced three new composite database indexes to optimize frequent query patterns in spp_entitlement and spp_program_membership tables.
  • Idempotent Index Creation: Implemented index creation within init() methods using CREATE INDEX IF NOT EXISTS to ensure safe and idempotent application on existing databases without requiring migrations.
  • spp_entitlement Indexing: Added composite indexes on (cycle_id, partner_id) to speed up duplicate checks and on (cycle_id, state) for efficient computation of cycle-related fields.
  • spp_program_membership Indexing: Added a composite index on (program_id, state) to improve performance for get_beneficiaries() and count_beneficiaries() queries.
  • Test Coverage: Included a new test file to verify the successful creation and existence of all newly added composite indexes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces three composite database indexes to improve query performance for frequently accessed data patterns in spp_programs. The indexes are created idempotently within the models' init methods, and new tests are included to verify their existence. The implementation for creating the indexes is sound. However, the accompanying tests could be more robust. I've provided suggestions to make the tests more specific by checking for the exact index names and column order, which will improve their reliability and prevent potential future regressions.

Comment on lines +19 to +20
AND indexdef LIKE '%%cycle_id%%'
AND indexdef LIKE '%%partner_id%%'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current query to check for the index is not specific enough. It checks for the presence of cycle_id and partner_id in the index definition but doesn't enforce that they are the only columns or their specific order. A more robust test would check for the exact index name and column order.

Suggested change
AND indexdef LIKE '%%cycle_id%%'
AND indexdef LIKE '%%partner_id%%'
AND indexname = 'spp_entitlement_cycle_id_partner_id_idx'
AND indexdef LIKE '%(cycle_id, partner_id)%'

Comment on lines +38 to +39
AND indexdef LIKE '%%cycle_id%%'
AND indexdef LIKE '%%state%%'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The query to verify the index's existence is too broad. It only checks that cycle_id and state are mentioned in the index definition but doesn't validate the column order or confirm it's a composite index specifically on these two columns. This could lead to false positives.

Suggested change
AND indexdef LIKE '%%cycle_id%%'
AND indexdef LIKE '%%state%%'
AND indexname = 'spp_entitlement_cycle_id_state_idx'
AND indexdef LIKE '%(cycle_id, state)%'

Comment on lines +57 to +58
AND indexdef LIKE '%%program_id%%'
AND indexdef LIKE '%%state%%'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test for the program membership index is not specific enough. It confirms that program_id and state are in the index definition but doesn't check their order or if they are the only columns. This could allow an incorrectly defined index to pass the test.

Suggested change
AND indexdef LIKE '%%program_id%%'
AND indexdef LIKE '%%state%%'
AND indexname = 'spp_program_membership_program_id_state_idx'
AND indexdef LIKE '%(program_id, state)%'

@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 42.85714% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.94%. Comparing base (1a91296) to head (d868742).
⚠️ Report is 6 commits behind head on 19.0.

Files with missing lines Patch % Lines
spp_programs/models/entitlement_base_model.py 0.00% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             19.0     #118      +/-   ##
==========================================
- Coverage   70.14%   69.94%   -0.21%     
==========================================
  Files         739      783      +44     
  Lines       43997    46604    +2607     
==========================================
+ Hits        30863    32595    +1732     
- Misses      13134    14009     +875     
Flag Coverage Δ
spp_api_v2 79.96% <ø> (ø)
spp_api_v2_change_request 66.66% <ø> (ø)
spp_api_v2_cycles 71.12% <ø> (ø)
spp_api_v2_data 64.41% <ø> (ø)
spp_api_v2_entitlements 70.19% <ø> (ø)
spp_api_v2_gis 71.52% <ø> (ø)
spp_api_v2_products 66.27% <ø> (ø)
spp_api_v2_service_points 70.94% <ø> (ø)
spp_api_v2_simulation 71.12% <ø> (ø)
spp_api_v2_vocabulary 57.26% <ø> (ø)
spp_audit 64.19% <ø> (ø)
spp_base_common 90.26% <ø> (ø)
spp_case_entitlements 97.61% <ø> (ø)
spp_case_programs 97.14% <ø> (ø)
spp_cel_event 85.11% <ø> (?)
spp_claim_169 58.11% <ø> (?)
spp_dci_client_dr 55.87% <ø> (?)
spp_dci_client_ibr 60.17% <ø> (?)
spp_programs 45.51% <42.85%> (-0.01%) ⬇️
spp_security 66.66% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
spp_programs/models/program_membership.py 33.33% <100.00%> (+1.36%) ⬆️
spp_programs/models/entitlement_base_model.py 0.00% <0.00%> (ø)

... and 44 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant