mirror of
https://github.com/django/django.git
synced 2025-11-18 19:01:40 +00:00
Fix performance regression in Model.objects.all().delete()
Avoid generating unnecessary subquery for full table deletes to restore fast deletes and compatibility with MySQL LOCK TABLES. References #660
This commit is contained in:
parent
437196da9a
commit
368e074192
2 changed files with 9 additions and 0 deletions
|
|
@ -1407,6 +1407,8 @@ class SQLInsertCompiler(SQLCompiler):
|
||||||
class SQLDeleteCompiler(SQLCompiler):
|
class SQLDeleteCompiler(SQLCompiler):
|
||||||
@cached_property
|
@cached_property
|
||||||
def single_alias(self):
|
def single_alias(self):
|
||||||
|
# Ensure base table is in aliases.
|
||||||
|
self.query.get_initial_alias()
|
||||||
return sum(self.query.alias_refcount[t] > 0 for t in self.query.alias_map) == 1
|
return sum(self.query.alias_refcount[t] > 0 for t in self.query.alias_map) == 1
|
||||||
|
|
||||||
def _as_sql(self, query):
|
def _as_sql(self, query):
|
||||||
|
|
|
||||||
|
|
@ -606,6 +606,13 @@ class DeletionTests(TestCase):
|
||||||
|
|
||||||
class FastDeleteTests(TestCase):
|
class FastDeleteTests(TestCase):
|
||||||
|
|
||||||
|
def test_fast_delete_all(self):
|
||||||
|
with self.assertNumQueries(1) as ctx:
|
||||||
|
User.objects.all().delete()
|
||||||
|
sql = ctx.captured_queries[0]['sql']
|
||||||
|
# No subqueries is used when performing a full delete.
|
||||||
|
self.assertNotIn('SELECT', sql)
|
||||||
|
|
||||||
def test_fast_delete_fk(self):
|
def test_fast_delete_fk(self):
|
||||||
u = User.objects.create(
|
u = User.objects.create(
|
||||||
avatar=Avatar.objects.create()
|
avatar=Avatar.objects.create()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue