Skip to content

perf: add ContractEvent DESC indexes for query optimisation (#762)#819

Open
Abd-Standard wants to merge 2 commits into
SoroScan:mainfrom
Abd-Standard:Db/issues
Open

perf: add ContractEvent DESC indexes for query optimisation (#762)#819
Abd-Standard wants to merge 2 commits into
SoroScan:mainfrom
Abd-Standard:Db/issues

Conversation

@Abd-Standard

Copy link
Copy Markdown

Closes #762

This PR improves the performance of ContractEvent queries 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

  • Added database indexes to the ContractEvent model for frequently used query patterns:
    • contract_id + created_at
    • contract_id + event_type
    • ledger_sequence
    • created_at
  • Generated a Django migration for the new indexes (no raw SQL used).

Query Optimizations

  • Audited API views for unnecessary database queries.
  • Applied select_related() where appropriate to reduce additional queries when accessing related contract objects.
  • Reviewed list views for potential N+1 query issues.

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 ms

EXPLAIN ANALYZE confirms that the new indexes are used for the optimized queries.

Testing

  • Added and applied the Django migration successfully.
  • Verified index creation in PostgreSQL.
  • Confirmed API functionality after changes.
  • Ran EXPLAIN ANALYZE on common query patterns.
  • Checked for N+1 queries after applying ORM optimizations.

Checklist

  • Added Django migration for indexes

  • No raw SQL introduced

  • Optimized ORM queries where applicable

  • Verified index usage with EXPLAIN ANALYZE

  • Existing 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.

@drips-wave

drips-wave Bot commented Jun 28, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@DokaIzk

DokaIzk commented Jun 28, 2026

Copy link
Copy Markdown
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)
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.

Database Index and Query Performance Optimisation

2 participants