You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have been using the great django-reversion tool for almost 10 years now. Thanks for the work! 👍
When trying to recover deleted objects it often happens that the request times out (limited to 30 seconds): thus the recover feature cannot be used.
SQL query for recovering objects of content type 124 (only 329 entries):
explain analyze SELECT "reversion_version"."id", "reversion_version"."revision_id", "reversion_version"."object_id", "reversion_version"."content_type_id", "reversion_version"."db", "reversion_version"."format", "reversion_version"."serialized_data", "reversion_version"."object_repr" FROM "reversion_version" WHERE "reversion_version"."id" IN (SELECT DISTINCT ON (V0."object_id") V0."id" FROM "reversion_version" V0 WHERE (V0."content_type_id" = 124 AND V0."db" = 'default' AND NOT EXISTS(SELECT (1) AS "a" FROM "main_siteconfiguration" U0 WHERE U0."id" = CAST(V0."object_id" AS integer) LIMIT 1)) ORDER BY V0."object_id" ASC, V0."id" DESC) ORDER BY "reversion_version"."id" LIMIT 21
The issue is caused by the ORDER BY "reversion_version"."id" as already explored in #748 (comment). Postgres first sorts the whole reversion_version table by id before filtering by content type and db.
Reversion with optimized query
explain analyze SELECT "reversion_version"."id", "reversion_version"."revision_id", "reversion_version"."object_id", "reversion_version"."content_type_id", "reversion_version"."db", "reversion_version"."format", "reversion_version"."serialized_data", "reversion_version"."object_repr" FROM "reversion_version" WHERE "reversion_version"."content_type_id" = 124 AND "reversion_version"."db" = 'default' AND "reversion_version"."id" IN (SELECT DISTINCT ON (V0."object_id") V0."id" FROM "reversion_version" V0 WHERE (V0."content_type_id" = 124 AND V0."db" = 'default' AND NOT EXISTS(SELECT (1) AS "a" FROM "main_siteconfiguration" U0 WHERE U0."id" = CAST(V0."object_id" AS integer) LIMIT 1)) ORDER BY V0."object_id" ASC, V0."id" DESC) ORDER BY "reversion_version"."id" LIMIT 21
This issue seems is closely related to: #748 (comment)
Situation
We have been using the great django-reversion tool for almost 10 years now. Thanks for the work! 👍
When trying to recover deleted objects it often happens that the request times out (limited to 30 seconds): thus the recover feature cannot be used.
Database size
Reversion "out of the box"
SQL query for recovering objects of content type 124 (only 329 entries):
explain analyze SELECT "reversion_version"."id", "reversion_version"."revision_id", "reversion_version"."object_id", "reversion_version"."content_type_id", "reversion_version"."db", "reversion_version"."format", "reversion_version"."serialized_data", "reversion_version"."object_repr" FROM "reversion_version" WHERE "reversion_version"."id" IN (SELECT DISTINCT ON (V0."object_id") V0."id" FROM "reversion_version" V0 WHERE (V0."content_type_id" = 124 AND V0."db" = 'default' AND NOT EXISTS(SELECT (1) AS "a" FROM "main_siteconfiguration" U0 WHERE U0."id" = CAST(V0."object_id" AS integer) LIMIT 1)) ORDER BY V0."object_id" ASC, V0."id" DESC) ORDER BY "reversion_version"."id" LIMIT 21
Query plan
The issue is caused by the
ORDER BY "reversion_version"."id"
as already explored in #748 (comment). Postgres first sorts the whole reversion_version table by id before filtering by content type and db.Reversion with optimized query
explain analyze SELECT "reversion_version"."id", "reversion_version"."revision_id", "reversion_version"."object_id", "reversion_version"."content_type_id", "reversion_version"."db", "reversion_version"."format", "reversion_version"."serialized_data", "reversion_version"."object_repr" FROM "reversion_version" WHERE "reversion_version"."content_type_id" = 124 AND "reversion_version"."db" = 'default' AND "reversion_version"."id" IN (SELECT DISTINCT ON (V0."object_id") V0."id" FROM "reversion_version" V0 WHERE (V0."content_type_id" = 124 AND V0."db" = 'default' AND NOT EXISTS(SELECT (1) AS "a" FROM "main_siteconfiguration" U0 WHERE U0."id" = CAST(V0."object_id" AS integer) LIMIT 1)) ORDER BY V0."object_id" ASC, V0."id" DESC) ORDER BY "reversion_version"."id" LIMIT 21
Query plan
To overcome the performance issue, simply filter reversion_version on content_type and db to limit the records to be filtered by id.
How to do it in Django-reversion
See
django-reversion/reversion/models.py
Line 192 in 1cc4023
Additional index
For large tables an additional index greatly helps!
Other tables
I compared the execution times of the queries with our largest table as well: content_type_id 85.
reversion_version_SPEED
index mentioned below).@etianen: what do you think of extending the query and adding the index as demonstrated above?
The text was updated successfully, but these errors were encountered: