perf: add query-aligned indexes for Login model (timestamp desc, country upper, ip)#563
Merged
eugenioseveri merged 1 commit intocertego:developfrom Feb 9, 2026
Conversation
Contributor
Author
|
@Lorygold Could you please review this when you have a chance? This PR addresses Part 1 of issue #494 (Login model indexes). I've added 3 query-aligned indexes based on comprehensive codebase analysis:
I followed your feedback from #524, #531, and #540 - skipping low-value fields like All 276 tests pass, and EXPLAIN ANALYZE confirms the indexes are being used (0.015-0.080ms query times). Thanks! 🙏 |
Contributor
|
For next times: it's not necessary to open new PRs, you can work on the initial one and update it. Btw, now it's okay, thank you! @lucaCigarini @eugenioseveri I think this PR can be merged |
eugenioseveri
approved these changes
Feb 9, 2026
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.
🎯 Problem Statement
Fixes #494 (Part 1: Login Model Indexes)
BuffaLogs has performance issues due to missing database indexes on the
Loginmodel. Queries usingtimestamp,country, andipfields are performing full table scans, causing:ORDER BY -timestamp)🔍 Solution
Added 3 query-aligned indexes to the
Loginmodel based on comprehensive codebase analysis:1. Descending Timestamp Index
Login.objects.filter(...).order_by("-timestamp")(used in 10+ locations)models.py:76,dashboards/charts.py,views/logins.py:252. Functional Country Index (Case-Insensitive)
Login.objects.filter(country__iexact=...)(used in 8+ locations)models.py:89,detection.py:138,dashboards/charts.py3. IP Address Index
Login.objects.filter(ip=...)(exact matches)models.py:81, detection queries✅ Verification
Database Indexes Created:
Query Performance (EXPLAIN ANALYZE):
Test 1: Timestamp Descending
Test 2: Country Case-Insensitive
Test 3: IP Exact Match
📋 What Was NOT Done (Per Maintainer Requirements)
Based on feedback from @Lorygold on PRs #524, #531, #540:
indexfield - Skipped (low entropy, per Perf: add missing indexes for Login filters to avoid sequential scans #524)event_idfield - Skipped (only used in tests, per perf: add indexes for Login filter fields #540)user_agentfield - Skipped (large text field, minimal benefit)🔬 Testing
📚 References
Login.objectsqueries across models, views, dashboards, charts📝 Migration Details
File:
0023_login_login_timestamp_desc_idx_and_more.pyOperations:
AddIndexon-timestamp(descending order)AddIndexonUpper(country)(case-insensitive functional index)AddIndexonip(standard B-tree)Note: This PR addresses Login model performance only. Alert model optimization (mentioned in #494) requires a different approach due to JSON field queries and should be handled in a separate PR.