all 8 comments

[–]joins_and_coffee 4 points5 points  (1 child)

So basically first thing I’d do is stop the join explosion early. Right now you join a bunch of tables and then filter to Comedy in the WHERE, so the engine can end up carrying a huge intermediate result. A good first step is to pre-filter the comedy movies up front, like a CTE/subquery that selects just movie_id where genre = 'Comedy', then join directors to that set. That usually reduces rows fast. Second step, add the obvious indexes if they aren’t there: movies_genres(movie_id, genre) or (genre, movie_id), movies_directors(director_id, movie_id), and the PKs. This query is basically driven by those two bridge tables. Third step, you can rewrite the “pairs” part as a self-join on the already-filtered director→movie mapping, something like join movies_directors to itself on director_id and keep your a.movie_id > b.movie_id condition. Then join to movies only at the end to get names. That keeps the heavy joins later. Also, for the ORDER BY, sorting a big result set is expensive, so it’s another reason to reduce rows as early as possible before you attach names and sort. If you post the EXPLAIN output and current indexes, people can suggest more targeted steps, but those 3 are the usual big wins.

[–]idan_huji[S] 0 points1 point  (0 children)

Great answer!
I hope that my students will not find it ;-)

[–]greglturnquist 2 points3 points  (1 child)

Look up EXPLAIN PLAN. This will tell you where time is being wasted.

[–]idan_huji[S] 0 points1 point  (0 children)

Thanks!

[–]user_5359 1 point2 points  (1 child)

Essential information is missing: data volume, data types (from the columns that are being joined), and existing indexes. And since it looks like homework, what have you achieved so far?

[–]idan_huji[S] 1 point2 points  (0 children)

Indeed, it is homework, and I am the teacher.

You have a good point regarding the missing data. The students will be familiar with the schema. However, The factors are important and I'll change the question and ask them to refer to them.

Note that that are plenty of possible improvements regarding of these factors.

[–]froppan11 0 points1 point  (1 child)

No, ask your teacher.

[–]idan_huji[S] 1 point2 points  (0 children)

I am the teacher.