Changes between Initial Version and Version 2 of Ticket #17276
- Timestamp:
- Nov 25, 2011, 7:27:17 AM (13 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #17276
- Property Triage Stage Unreviewed → Accepted
-
Ticket #17276 – Description
initial v2 6 6 Basically, in a many-to-many mappings between models Student and Course, if I want to find all instances of Students that aren't registered for classes, I would issue the following Django query: 7 7 8 {{{ 8 9 Student.objects.filter(course__isnull=True) 10 }}} 9 11 10 12 Django translates this into the following query: 11 13 {{{ 12 14 SELECT "student"."id", "student"."name" 13 15 FROM "student" … … 17 19 ON ("course_students"."course_id" = "course"."id") 18 20 WHERE "course"."id" IS NULL 19 21 }}} 20 22 The problem is that the way the WHERE clause is generated is very inefficient (at least when used with Postgres). Changing WHERE to "course_students"."student_id" IS NOT NULL yields orders of magnitude improved query plan. Here's the difference I'm seeing on real data: 21 23 22 Django way: (cost=1072479.36..6256437.79 rows=1 width=145)23 Hand-crafted: (cost=1518.71..1533.35 rows=1 width=145)24 * Django way: (cost=1072479.36..6256437.79 rows=1 width=145) 25 * Hand-crafted: (cost=1518.71..1533.35 rows=1 width=145) 24 26 25 27 I'm attaching a sample project with the model already set up. To see the generated SQL query, simply run "python manage.py anti-join."