Hi all,

I came here with following problem:

I was pretty happy user of Django ORM delete method until I found that
it takes a lot of time when you want to delete object that has A LOT
of objects connected to it via FK.

Assume we have 3 models defined:

class A(models.Model):
        pass

class B(models.Model):
        a = models.ForeignKey(A)

class C(models.Model):
        b = models.ForeignKey(B)

for this models definitions we create following objects:
a - 1 instance of A
b - 10 000 instances of B, each pointed on a
c - no instances (or some - no matter)

My use case is to delete (as fast as possible) a and all related b and
c objects, but a.delete() takes way to much time than accepted.
What I found is that Django behind the scenes tries to SELECT
potential c objects existing for each b, which obviously leads to
enormous number of SQL statements (10 000 selects + other statements).

This must be effect of DJango's deleting policy described in docs as:
"When Django deletes an object, it emulates the behavior of the SQL
constraint ON DELETE CASCADE --
in other words, any objects which had foreign keys pointing at the
object to be deleted will be deleted along with it. ",
which is something I really want - but performed in shorter time.

Do you know how to improve speed of operation I have described
(assuming large ammount of related objects)?

Any help is much appreciated.
Kind Regards,
Lukasz

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to