Faster SQL Joins Print

  • 6

Improve the speed of a JOIN by arranging your comparisons properly.

Instead of placing your comparisons for a join with the primary query, as in:

SELECT foo.id
FROM tblfoo AS foo
JOIN tblbar AS bar ON bar.fooId = foo.id
WHERE foo.type = 'good'
  AND bar.style = 'flat'

Move your joined comparisons directly to the JOIN command, as in:

SELECT foo.id
FROM tblfoo AS foo
JOIN tblbar AS bar ON bar.fooId = foo.id AND bar.style = 'flat'
WHERE foo.type = 'good'
  AND bar.id IS NOT NULL

This reduces the size of your temporary join dataset before your primary query is executed.

Was this answer helpful?

« Back