perf: add database indexes and optimize dashboard query for production-scale workloads#268
Open
KaparthyReddy wants to merge 3 commits into
Open
Conversation
- Add composite index idx_tasks_status_created(status, created_at DESC) for dashboard running tasks query — eliminates full scan + filter - Add findings indexes: severity, task_id, discovered_at DESC, plugin_id, target, and composite (task_id, severity) for grouped severity counts - Add reports indexes: task_id, generated_at DESC, status - Add audit_log indexes: timestamp DESC, event_type, task_id - Refactor dashboard query: replace SELECT * FROM findings full table load + Python-side severity counting with a single DB-level GROUP BY query — reduces dashboard latency from O(n) to O(1) on large datasets - Fetch only 5 most recent findings for dashboard instead of entire table - Add migration script 001_add_performance_indexes.sql for existing DBs
- Add composite index idx_tasks_status_created(status, created_at DESC) for dashboard running tasks query — eliminates full scan + filter - Add findings indexes: severity, task_id, discovered_at DESC, plugin_id, target, and composite (task_id, severity) for grouped severity counts - Add reports indexes: task_id, generated_at DESC, status - Add audit_log indexes: timestamp DESC, event_type, task_id - Refactor dashboard query: replace SELECT * FROM findings full table load + Python-side severity counting with a single DB-level GROUP BY query — reduces dashboard latency from O(n) to O(1) on large datasets - Fetch only 5 most recent findings for dashboard instead of entire table - Add migration script 001_add_performance_indexes.sql for existing DBs - Add integration tests: verify all 10 new indexes exist post-migration, verify dashboard severity counts correct on seeded dataset, verify recent_findings limited to 5 regardless of total count - Add benchmark script scripts/benchmark_db.py: seeds 10k findings + 1k tasks, prints EXPLAIN QUERY PLAN and timed results for hot paths
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.
Description
Profiles and optimizes the four hot query paths identified in the issue scope: dashboard aggregation, findings list, reports list, and task queries.
Query optimization (
routes.py):SELECT * FROM findingsfull table load + Python-side severity counting loop with a singleSELECT severity, COUNT(*) GROUP BY severityDB-level aggregation — reduces dashboard latency from O(n) to O(1) on large finding datasetsSELECT ... ORDER BY discovered_at DESC LIMIT 5— only 5 rows transferred instead of the full collectionSELECT COUNT(*) AS total FROM findingsfor total count instead oflen(all_findings)after full fetchIndex additions (
database.py+migrations/001_add_performance_indexes.sql):idx_tasks_status_created(status, created_at DESC)— composite index for dashboard running tasks query; eliminates full scan + filteridx_findings_severity— supports GROUP BY severity aggregationidx_findings_task_id— supports foreign key lookups from tasksidx_findings_discovered_at DESC— supports ORDER BY on findings listidx_findings_plugin_id,idx_findings_target— common filter columnsidx_findings_task_severity(task_id, severity)— composite for per-task severity breakdownidx_reports_task_id,idx_reports_generated_at DESC,idx_reports_status— reports list and filter queriesidx_audit_timestamp DESC,idx_audit_event_type,idx_audit_task_id— audit log queriesRelated Issues
Closes #257
Type of Change
How Has This Been Tested?
Integration tests —
testing/backend/integration/test_database_indexes.py(14 tests, all passing):test_dashboard_severity_counts_correct: seeds 50 findings (10 per severity), asserts exact counts from dashboard endpointtest_dashboard_recent_findings_limit: seeds 200 findings, assertsrecent_findingslength ≤ 5test_dashboard_empty_findings: asserts correct zero-state response with no findingstest_dashboard_severity_counts_with_single_severity: seeds 15 critical findings, asserts all other severities return 0Benchmark script —
scripts/benchmark_db.py:EXPLAIN QUERY PLANoutput for hot pathsRun tests:
Run benchmark:
Checklist