SACGF / SACGF/variantgrid

Investigate N+1 queries due to missing select_related/prefetch_related

Open
#1,489 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
30
Forks
3
Avg merge
9h 28m
Merged PRs (30d)
42

Description

🤖 Written by Claude.

## Problem

Pages may be slow due to N+1 query patterns caused by missing `select_related` or `prefetch_related` on ORM querysets. Django's lazy loading means related objects are fetched one-by-one in loops or templates rather than in a single JOIN/IN query.

## Investigation approach

### django-debug-toolbar (recommended)

Install `django-debug-toolbar` and use the SQL panel to inspect queries per request. It highlights duplicate queries and shows timing, making N+1 patterns immediately visible.

Django's built-in SQL logging can complement this — add to dev settings to log all queries to a file:

```python
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'sql_file': {
'class': 'logging.FileHandler',
'filename': '/tmp/sql_queries.log',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['sql_file'],
'level': 'DEBUG',
'propagate': False,
},
},
}
```

### nplusone (useful for tests)

`nplusone` detects N+1 queries at runtime and can be integrated into Django `TestCase` to catch regressions automatically. With `NPLUSONE_RAISE = True` it raises an exception on detection, making it easy to enforce in CI.

### QueryCountMiddleware (last resort)

A custom middleware that logs requests exceeding a query count threshold can be added if the above tools aren't suitable, but this is more of a workaround than a proper solution.

## Areas likely to have issues

- Classification list/detail pages (deep model graph: `Classification` → `ClassificationModification` → `EvidenceKey`, `Lab`, `User`, `Allele`)
- Analysis node grids (variant querysets with sample/cohort relationships)
- Any view returning lists of objects with related `Lab`, `User`, or `GenomeBuild` fields

## Fix

Add `select_related(...)` or `prefetch_related(...)` to the relevant querysets in views or managers.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.