Skip to content
Advertisement

How to optimize a SQL query that combines INNER JOINs and DISTINCT?

The query above returns me 5222 rows, and it runs locally in 0.63 seconds. I’m wondering if it is as fast as it can be or if I can tweak it further somehow, as this dataset will grow fast overtime.

Things that I’ve tried without change in speed:

1 – Change the join order, joining from the smallest to the biggest table.

2 – Adding an index to af.ID.

Advertisement

Answer

Move the junction table out of the main query:


User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement