I'm debugging a possible performance bug in django-reversion (a django library). The issue that I'm experiencing is that every time I run django-reversion's createinitialrevisions
, my DB will take huge amount of time to process whatever is going on.
I have enabled the Performance Insights in RDS and I see that the query that is killing my DB looks like this:
SELECT "table_a"."id"
FROM "table_a"
WHERE NOT (CAST("table_a"."id" as text) IN (
SELECT U0."object_id"
FROM "reversion_version" U0
WHERE (U0."content_type_id" = 49 AND U0."db" = 'default')
))
If I understand correctly what I read here https://explainextended.com/2009/09/16/not-in-vs-not-exists-vs-left-join-is-null-postgresql/ it turns out that PostgreSQL isn't able to optimize the same way a NOT IN
than it would a LEFT JOIN
. That's why I decided to rewrite this query and see whether it would take the same amount of time to run.
This is the result after rewriting it:
SELECT "table_a"."id"
FROM "table_a"
LEFT JOIN
"reversion_version" U0
ON U0."object_id" = "table_a"."id"::text
WHERE U0."object_id" IS NULL AND U0."content_type_id" = 49 AND U0."db" = 'default'
I must have done something wrong, because I'm getting different results. My query (the rewritten one) isn't returning anything at all.
What did I miss?