python - How can i speed up this django orm generated query? -
i have movie table , genre table , moviegenre table genres movie has.
the django orm came query when i'm trying movies have @ least of genres target movie has in common.
select "movies_movie"."id", "movies_movie"."imdb_id", ..etc.. "movies_movie"."last_ingested_on", count("movies_movie"."id") "count", count("movies_moviegenre"."genre_id") "genres_count" "movies_movie" left outer join "movies_moviegenre" on ( "movies_movie"."id" = "movies_moviegenre"."movie_id" ) inner join "movies_moviegenre" t4 on ( "movies_movie"."id" = t4."movie_id" ) inner join "movies_moviegenre" t6 on ( "movies_movie"."id" = t6."movie_id" ) ("movies_movie"."last_ingested_on" not null , not ("movies_movie"."imdb_id" = 'tt0111161' ) , "movies_movie"."type" = 'feature' , "movies_movie"."certification" = 'r' , t4."genre_id" = 1 , t6."genre_id" = 10 ) group "movies_movie"."id", "movies_movie"."imdb_id", "movies_movie"."movie", "movies_movie"."type", "movies_movie"."year", "movies_movie"."tagline", "movies_movie"."plot", "movies_movie"."runtime", "movies_movie"."rating", "movies_movie"."certification", "movies_movie"."budget", "movies_movie"."box_office_revenue", "movies_movie"."poster_url", "movies_movie"."trailer_url", "movies_movie"."mood_data", "movies_movie"."created_on", "movies_movie"."modified_on", "movies_movie"."last_ingested_on" having count("movies_moviegenre"."genre_id") >= 2 order "count" desc
can see causing slow, it's taking 1107.26499557 ms isn't acceptable. in advance
explain output: http://explain.depesz.com/s/lev
not sql expert double inner join looks strange me, strange in equivalent.
left outer join "movies_moviegenre" on ( "movies_movie"."id" = "movies_moviegenre"."movie_id" ) inner join "movies_moviegenre" t4 on ( "movies_movie"."id" = t4."movie_id" ) inner join "movies_moviegenre" t6 on ( "movies_movie"."id" = t6."movie_id" )
said adding index columns of clause? i'm looking @ these in particular looks index candidates:
and "movies_movie"."type" = 'feature' , "movies_movie"."certification" = 'r' , t4."genre_id" = 1 , t6."genre_id" = 10 )
Comments
Post a Comment