Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I know JOIN on indexed columns are faster than sub-queries in a query result.

I am confused if : JOINs on non-indexed columns are also faster than sub-queries or not.

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

In a query such as this:

SELECT * 
FROM 
  some_table 
  JOIN (SELECT foo, col1 FROM bar) s_sub 
  ON (some_table.col1 = s_sub.col1)

MySQL is able to use an index on some_table.col1. So that it starts with the derived table (full scan), then moves on to some_table using index.

This means it can utilize an index. So a subquery does not altogether negates use of an index.

If no index exists on any relevant column, obviously hard work is done (full scan, join buffer) to make the JOIN.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.