Skip to content

perf: add query-aligned indexes for Login model#559

Closed
nishantxscooby wants to merge 10 commits intocertego:developfrom
nishantxscooby:perf/login-indexes
Closed

perf: add query-aligned indexes for Login model#559
nishantxscooby wants to merge 10 commits intocertego:developfrom
nishantxscooby:perf/login-indexes

Conversation

@nishantxscooby
Copy link
Contributor

@nishantxscooby nishantxscooby commented Jan 31, 2026

Fixes #494

Problem

Several queries on the Login model were triggering sequential scans on large datasets, leading to slow response times and increased database CPU usage.
This was especially visible in query paths relying on apply_filters, where results are always ordered by timestamp and filtered by fields such as ip, country, and event_id.

Solution

This PR adds database indexes that are explicitly aligned with how queries are executed in the codebase:

  • Added a descending index on timestamp to match the default order_by("-timestamp")
  • Added a functional index on UPPER(country) to support country__iexact
  • Added indexes on ip and event_id based on confirmed real usage
  • Intentionally did not index the index field, as previously discussed

Scope

  • Login model only
  • Single migration
  • No schema or constraint changes beyond indexes

These changes ensure PostgreSQL can use indexes effectively, avoiding sequential scans and improving query performance on large datasets.

@Lorygold
Copy link
Contributor

Lorygold commented Feb 9, 2026

@nishantxscooby please close this PR, since the correct one has been already merged

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.

[PERFORMANCE] Backend: Missing Database Indexes Causing Slow Queries

2 participants

Comments