-
Notifications
You must be signed in to change notification settings - Fork 1
perf(spp_programs): add composite indexes for frequent query patterns #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 19.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||||||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||||||||||
| from odoo.tests import TransactionCase | ||||||||||
|
|
||||||||||
|
|
||||||||||
| class TestCompositeIndexes(TransactionCase): | ||||||||||
| """Test that composite indexes exist for frequent query patterns.""" | ||||||||||
|
|
||||||||||
| def test_entitlement_cycle_partner_index_exists(self): | ||||||||||
| """Composite index on spp_entitlement(cycle_id, partner_id) must exist. | ||||||||||
|
|
||||||||||
| The prepare_entitlements duplicate check searches entitlements by | ||||||||||
| (cycle_id, partner_id). Without this index, each batch does a | ||||||||||
| sequential scan. | ||||||||||
| """ | ||||||||||
| self.env.cr.execute( | ||||||||||
| """ | ||||||||||
| SELECT 1 FROM pg_indexes | ||||||||||
| WHERE tablename = 'spp_entitlement' | ||||||||||
| AND indexdef LIKE '%%cycle_id%%' | ||||||||||
| AND indexdef LIKE '%%partner_id%%' | ||||||||||
| """ | ||||||||||
| ) | ||||||||||
| self.assertTrue( | ||||||||||
| self.env.cr.fetchone(), | ||||||||||
| "Composite index on (cycle_id, partner_id) must exist on spp_entitlement", | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| def test_entitlement_cycle_state_index_exists(self): | ||||||||||
| """Composite index on spp_entitlement(cycle_id, state) must exist. | ||||||||||
|
|
||||||||||
| Cycle computed fields (total_amount, show_approve_button, | ||||||||||
| entitlements_count) filter entitlements by cycle_id and state. | ||||||||||
| """ | ||||||||||
| self.env.cr.execute( | ||||||||||
| """ | ||||||||||
| SELECT 1 FROM pg_indexes | ||||||||||
| WHERE tablename = 'spp_entitlement' | ||||||||||
| AND indexdef LIKE '%%cycle_id%%' | ||||||||||
| AND indexdef LIKE '%%state%%' | ||||||||||
|
Comment on lines
+38
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The query to verify the index's existence is too broad. It only checks that
Suggested change
|
||||||||||
| """ | ||||||||||
| ) | ||||||||||
| self.assertTrue( | ||||||||||
| self.env.cr.fetchone(), | ||||||||||
| "Composite index on (cycle_id, state) must exist on spp_entitlement", | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| def test_program_membership_program_state_index_exists(self): | ||||||||||
| """Composite index on spp_program_membership(program_id, state) must exist. | ||||||||||
|
|
||||||||||
| get_beneficiaries() and count_beneficiaries() filter by | ||||||||||
| (program_id, state) on every async batch dispatch. | ||||||||||
| """ | ||||||||||
| self.env.cr.execute( | ||||||||||
| """ | ||||||||||
| SELECT 1 FROM pg_indexes | ||||||||||
| WHERE tablename = 'spp_program_membership' | ||||||||||
| AND indexdef LIKE '%%program_id%%' | ||||||||||
| AND indexdef LIKE '%%state%%' | ||||||||||
|
Comment on lines
+57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test for the program membership index is not specific enough. It confirms that
Suggested change
|
||||||||||
| """ | ||||||||||
| ) | ||||||||||
| self.assertTrue( | ||||||||||
| self.env.cr.fetchone(), | ||||||||||
| "Composite index on (program_id, state) must exist on spp_program_membership", | ||||||||||
| ) | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current query to check for the index is not specific enough. It checks for the presence of
cycle_idandpartner_idin 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.