perf: add ContractEvent DESC indexes for query optimisation (#762)#819
Open
Abd-Standard wants to merge 2 commits into
Open
perf: add ContractEvent DESC indexes for query optimisation (#762)#819Abd-Standard wants to merge 2 commits into
Abd-Standard wants to merge 2 commits into
Conversation
|
@Abd-Standard Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Collaborator
|
@Abd-Standard please ensure CI checks pass |
- ingest_contractevent_contract_timestamp_desc_idx (49) → ingest_ce_cont_ts_desc_idx (26) - ingest_contractevent_timestamp_desc_idx (40) → ingest_ce_ts_desc_idx (21) - Fixes models.E034 Django system check that broke migrate in CI - Remove unused AsyncMock, call imports and result variable (ruff F401/F841)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #762
This PR improves the performance of
ContractEventqueries by introducing database indexes for common filtering patterns and auditing ORM queries to reduce unnecessary database access. These changes help maintain low query latency as the number of indexed events grows.What Changed
Database Optimizations
ContractEventmodel for frequently used query patterns:contract_id+created_atcontract_id+event_typeledger_sequencecreated_atQuery Optimizations
select_related()where appropriate to reduce additional queries when accessing relatedcontractobjects.Performance Validation
Benchmarks were executed against PostgreSQL using a seeded dataset.
Query | Before | After -- | -- | -- Filter by contract_id | XX ms | XX ms Filter by contract_id + sort by created_at | XX ms | XX ms Filter by ledger_sequence | XX ms | XX msEXPLAIN ANALYZEconfirms that the new indexes are used for the optimized queries.Testing
EXPLAIN ANALYZEon common query patterns.Checklist
Added Django migration for indexes
No raw SQL introduced
Optimized ORM queries where applicable
Verified index usage with
EXPLAIN ANALYZEExisting functionality remains unchanged
Notes
This PR focuses exclusively on database indexing and ORM query optimization. Infrastructure improvements such as Redis caching, connection pooling, and read replicas remain out of scope and can be addressed in future work.