#29568: Avoid trying to UPDATE a parent model when its child just got INSERT
-------------------------------------+-------------------------------------
     Reporter:  François Dupayrat    |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by François Dupayrat):

 * has_patch:  0 => 1


Comment:

 Pull request created: https://github.com/django/django/pull/10197

 Tests showed a different behavior than the one listed in the ticket:
 instead of doing UPDATE queries, SELECT queries were made in-between
 INSERT INTO queries. Editing Parent, Child and GrandChild to add a Boolean
 field revert to UPDATE queries.

 In both cases, only INSERT INTO queries should be made anyway, so I don't
 think it's necessary to add fields to Parent, Child or GrandChild.


 Summary of queries:
 - with existing I see 6 queries on GrandChild.objects.create()
 {{{
 INSERT INTO "model_inheritance_grandparent" ("first_name", "last_name",
 "email", "place_id") VALUES ('', '', '', NULL)
 SELECT (1) AS "a" FROM "model_inheritance_parent" WHERE
 "model_inheritance_parent"."grandparent_ptr_id" = 1  LIMIT 1
 INSERT INTO "model_inheritance_parent" ("grandparent_ptr_id") SELECT 1
 SELECT (1) AS "a" FROM "model_inheritance_child" WHERE
 "model_inheritance_child"."parent_ptr_id" = 1  LIMIT 1
 INSERT INTO "model_inheritance_child" ("parent_ptr_id") SELECT 1
 INSERT INTO "model_inheritance_grandchild" ("child_ptr_id") SELECT 1
 }}}
 - by adding a BooleanField with a default value to each of Parent, Child
 and GrandChild, I still see 6 queries
 {{{
 INSERT INTO "model_inheritance_grandparent" ("first_name", "last_name",
 "email", "place_id") VALUES ('', '', '', NULL)
 UPDATE "model_inheritance_parent" SET "parent_field" = 1 WHERE
 "model_inheritance_parent"."grandparent_ptr_id" = 1
 INSERT INTO "model_inheritance_parent" ("grandparent_ptr_id",
 "parent_field") SELECT 1, 1
 UPDATE "model_inheritance_child" SET "child_field" = 1 WHERE
 "model_inheritance_child"."parent_ptr_id" = 1
 INSERT INTO "model_inheritance_child" ("parent_ptr_id", "child_field")
 SELECT 1, 1
 INSERT INTO "model_inheritance_grandchild" ("child_ptr_id",
 "grand_child_field") SELECT 1, 1
 }}}
 - with patch I see 4 queries, both for modified and unmodified Models,
 provided the new fields have default values.
 {{{
 INSERT INTO "model_inheritance_grandparent" ("first_name", "last_name",
 "email", "place_id") VALUES ('', '', '', NULL)
 INSERT INTO "model_inheritance_parent" ("grandparent_ptr_id") SELECT 1
 INSERT INTO "model_inheritance_child" ("parent_ptr_id") SELECT 1
 INSERT INTO "model_inheritance_grandchild" ("child_ptr_id") SELECT 1
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29568#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/074.ed5125b85ec4c578246610f9449890fb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to