I’ve got a query (partly generated by django orm, but can be modified).
For some reason, It’s very slow (between 1.5 -3 seconds, without having lots of data on the system) . I’ve got an “explain” from my newrelic trace. But I don’t really know how to read this.
Where is the query spending time?
How should I optimize the query (or add indexes to the db)
Explain plan in explain.depesz
Query:
SELECT
"top_traders_toptrader"."user_id", "top_traders_toptrader"."social_profile_id",
"top_traders_toptrader"."execution_account_id", "top_traders_toptrader"."performance_account_id",
COUNT(DISTINCT "social_subscription"."subscriber_id") AS "num_subscribers",
COUNT(DISTINCT "social_follow"."follower_id") AS "num_followerers",
COUNT(DISTINCT "execution_position"."isin") AS "stock_count",
"social_socialprofile"."user_id", "social_socialprofile"."is_hidden",
"social_socialprofile"."role", "social_socialprofile"."image",
"social_socialprofile"."is_excluded",
"authUsers_user"."id", "authUsers_user"."username",
"authUsers_user"."first_name", "authUsers_user"."last_name",
"authUsers_user"."is_deleted", "authUsers_user"."date_joined",
"authUsers_user"."is_superuser", "authUsers_user"."is_demo",
"execution_account"."user", "execution_account"."date_joined",
"execution_account"."is_active",
"performance_accountperformance"."user_id", "performance_accountperformance"."account_id",
"performance_accountperformance"."live_periodic_gains_id", "performance_accountperformance"."role",
"performance_accountperformance"."field", "performance_periodicgain"."id",
"performance_periodicgain"."daily", "performance_periodicgain"."weekly",
"performance_periodicgain"."monthly", "performance_periodicgain"."three_monthly",
"performance_periodicgain"."six_monthly", "performance_periodicgain"."yearly"
FROM
"top_traders_toptrader"
LEFT OUTER JOIN "execution_account"
ON ( "top_traders_toptrader"."execution_account_id" = "execution_account"."user" )
INNER JOIN "performance_accountperformance"
ON ( "top_traders_toptrader"."performance_account_id" = "performance_accountperformance"."user_id" )
LEFT OUTER JOIN "social_socialprofile"
ON ( "top_traders_toptrader"."social_profile_id" = "social_socialprofile"."user_id" )
LEFT OUTER JOIN "social_subscription"
ON ( "social_socialprofile"."user_id" = "social_subscription"."subscribed_to_id" )
LEFT OUTER JOIN "social_follower"
ON ( "social_socialprofile"."user_id" = "social_follower"."followeree_id" )
LEFT OUTER JOIN "execution_position"
ON ( "execution_account"."user" = "execution_position"."account_id" )
LEFT OUTER JOIN "authUsers_user"
ON ( "social_socialprofile"."user_id" = "authUsers_user"."id" )
LEFT OUTER JOIN "performance_periodicgain"
ON ( "performance_accountperformance"."live_periodic_gains_id" = "performance_periodicgain"."id" )
WHERE
(NOT ("top_traders_toptrader"."execution_account_id" IS NULL)
AND NOT ("top_traders_toptrader"."performance_account_id" IS NULL)
AND NOT ("top_traders_toptrader"."social_profile_id" IS NULL)
AND NOT ("social_socialprofile"."is_hidden" = True
AND "social_socialprofile"."is_hidden" IS NOT NULL)
AND "social_socialprofile"."role" = 20
AND NOT ((("top_traders_toptrader"."execution_account_id" IN
(SELECT U1."user"
FROM "execution_account" U1
LEFT OUTER JOIN "execution_order" U2
ON ( U1."user" = U2."account_id" )
WHERE U2."id" IS NULL)
AND "top_traders_toptrader"."execution_account_id" IS NOT NULL)
OR "top_traders_toptrader"."execution_account_id" IS NULL)))
GROUP BY
"top_traders_toptrader"."user_id",
"top_traders_toptrader"."social_profile_id",
"top_traders_toptrader"."execution_account_id",
"top_traders_toptrader"."performance_account_id",
"social_socialprofile"."user_id",
"social_socialprofile"."is_hidden",
"social_socialprofile"."role",
"social_socialprofile"."image",
"social_socialprofile"."is_excluded",
"authUsers_user"."id",
"authUsers_user"."username",
"authUsers_user"."first_name",
"authUsers_user"."last_name",
"authUsers_user"."is_deleted",
"authUsers_user"."date_joined",
"authUsers_user"."is_superuser",
"authUsers_user"."is_demo",
"execution_account"."user",
"execution_account"."date_joined",
"execution_account"."is_active",
"performance_accountperformance"."user_id",
"performance_accountperformance"."account_id",
"performance_accountperformance"."live_periodic_gains_id",
"performance_accountperformance"."role",
"performance_accountperformance"."field",
"performance_periodicgain"."id",
"performance_periodicgain"."daily",
"performance_periodicgain"."weekly",
"performance_periodicgain"."monthly",
"performance_periodicgain"."three_monthly",
"performance_periodicgain"."six_monthly",
"performance_periodicgain"."yearly"
ORDER BY
"num_subscribers" DESC ;