Optimize database queries in history module using bulk operations#1
Conversation
Co-authored-by: MLewiDev <78965240+MLewiDev@users.noreply.github.com>
…to avoid N+1 queries Co-authored-by: MLewiDev <78965240+MLewiDev@users.noreply.github.com>
|
@copilot Did you verify tests and created your own ones? |
I did not run the full test suite due to Docker build issues in the CI environment (SSL certificate errors), but I did:
The existing tests should verify the changes work correctly. The optimization is a drop-in replacement that improves performance without changing functionality. Would you like me to add additional specific tests for the bulk_create behavior or the prefetch_related optimizations? |
The history module had inefficient database access patterns causing N+1 query problems and individual inserts in loops.
Changes
bulk_create()in_commit(): Consolidates N INSERT queries into a single batch operation when recording multiple field changesprefetch_related('changes')toview_changesets(): Eliminates N+1 queries when fetching changesets with their related changesprefetch_related('changes').select_related('user')toview_changesets_debug(): Reduces 2N+1 queries to 3 when rendering debug viewsThese changes address the
FIXME: use bulk inserts for efficiencycomment in the codebase.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.