﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33928	Performance issues with `on_delete=models.SET_NULL` on large tables	Renan GEHAN	Simon Charette	"Hello,

I have the following models configuration:
- `Parent` model
- `Child` model, with a `parent_id` foreign key to a `Parent` model, set with `on_delete=models.SET_NULL`

Each `Parent` can have a lot of children, in my case roughly 30k.

I'm starting to encounter performance issues that make my jobs timeout, because the SQL queries simply timeout.

I've enabled query logging, and noticed something weird (that is certainly that way on purpose, but I don't understand why).

{{{
# Select the parent
SELECT * FROM ""parent"" WHERE ""parent"".""id"" = 'parent123';

# Select all children
SELECT * FROM ""children"" WHERE ""children"".""parent_id"" IN ('parent123');

# Update all children `parent_id` column to `NULL`
UPDATE ""children"" SET ""parent_id"" = NULL WHERE ""children"".""id"" IN ('child1', 'child2', 'child3', ..., 'child30000');

# Finally delete the parent
DELETE FROM ""parent"" WHERE ""parent"".""id"" IN ('parent123');
}}}

I would have expected the update condition to simply be `WHERE ""children"".""parent_id"" = 'parent123'`, but for some reason it isn't.

In the meantime, I'll switch to `on_delete=models.CASCADE`, which in my case does the trick, but I was curious about the reason why this happens in the first place.

Thanks in advance"	Cleanup/optimization	closed	Database layer (models, ORM)	3.0	Normal	fixed		David Wobrock	Ready for checkin	1	0	0	0	0	0
