r/learnSQL • u/idan_huji • Jan 10 '26
SQL performance improvement training question
- The following query compute pairs of comedies by the same director:
# Movies pairs of the same director
Select
d.first_name
, d.last_name
, fm.name
, sm.name
from
movies_genres as fmg
join
movies as fm
on
fmg.movie_id = fm.id
join
imdb_ijs.movies_directors as fmd
on
fm.id = fmd.movie_id
join
imdb_ijs.movies_directors as smd
on
fmd.director_id = smd.director_id
join
imdb_ijs.movies as sm
on
smd.movie_id = sm.id
join
imdb_ijs.directors as d
on
fmd.director_id = d.id
join
movies_genres as smg
on
sm.id = smg.movie_id
where
fmd.movie_id != smd.movie_id # Avoid reflexiveness
and
fmd.movie_id > smd.movie_id # No symmetry
and
fmg.genre = 'Comedy'
and
smg.genre = 'Comedy'
order by
d.first_name, d.last_name, fm.name, sm.name
;
There is a lot that can be done to improve the query performance.
- Compute the execution plan and time of the original query.
- Try to improve the query in steps. Per step:
- Explain the intuition explaining why it should improve the performance.
- Implement the change.
- Compute the execution plan and time of the new query.
- Steps that do not improve are also good. Trial and error is very common in this field. Explain why these steps did not improve.
View all comments
Show parent comment
2
u/DatabaseSpace Jan 10 '26
It would be better to never do that for any reason. Same with join and from. Use SQL beautifier online and it will format it for you.