Skip to content

perf: add indexes for Login filter fields#540

Closed
nishantxscooby wants to merge 1 commit intocertego:developfrom
nishantxscooby:perf/login-index-only
Closed

perf: add indexes for Login filter fields#540
nishantxscooby wants to merge 1 commit intocertego:developfrom
nishantxscooby:perf/login-index-only

Conversation

@nishantxscooby
Copy link
Contributor

Fixes #494

Improve Login query performance by adding DB indexes

Problem
Queries on the Login model were causing sequential scans when filtering by
timestamp, ip, country, and event_id, leading to poor performance on large datasets.

Solution
Add database indexes for the most commonly filtered fields in Login:

  • timestamp
  • ip
  • country
  • event_id

The index field is intentionally not indexed, as previously discussed.

Scope

  • Login model only
  • Single migration
  • No unrelated file changes

@nishantxscooby
Copy link
Contributor Author

nishantxscooby commented Jan 20, 2026

Hi @Lorygold

This change only adds database indexes on the fields involved in the reported Seq Scan (timestamp, ip, country, event_id).
Since BuffaLogs is a Django app package (not a standalone project), I validated the fix by ensuring the migration cleanly adds the intended indexes in a single migration and avoids the index field as previously discussed.

Let me know if you prefer a different indexing strategy, ty :)

@Lorygold
Copy link
Contributor

in addition to that, I was wondering that the Login.ip field could be changed in a models.GenericIPAddressField() type, as done for the UsersIP.ip field for improving performances, what do you think about that?

in that case, a new test in the test_migrations.py file should be added and a custom function at the beginning of the migration (as done in the 0022 migration, for example) for deleting the existing logs in the Login model that hasn't a valid IP (IPv4 or IPv6) in the Login.ip field

@Lorygold
Copy link
Contributor

Lorygold commented Jan 21, 2026

Could be nice to add also the dkango-debug-toolbar and attaching the prove of query improvements

models.Index(fields=["timestamp"], name="login_timestamp_idx"),
models.Index(fields=["ip"], name="login_ip_idx"),
models.Index(fields=["country"], name="login_country_idx"),
models.Index(fields=["event_id"], name="login_event_id_idx"),
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't remember if the event_id is queried somewhere? Because if not, its indexing is useless


class Meta:
indexes = [
models.Index(fields=["timestamp"], name="login_timestamp_idx"),
Copy link
Contributor

Choose a reason for hiding this comment

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

If I remember well, the timestamp is often queried as the inverse order:
.order_by("-timestamp")
so, it could be usefull to index it as "-timestamp"
what do you think?

@Lorygold
Copy link
Contributor

You should double-check whether these indexes actually match the way queries are executed and prove their match

For example, country is often queried with __iexact (case-insensitive), which does UPPER(country) = UPPER(value) in SQL.

A plain index on country won’t be used in this case; for it to help, you’d need a functional index like models.Index(Upper("country"), name="login_country_upper_idx").

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