I'm flabbergasted to see that everyone points out to use JOIN
as if it is the same thing. IT IS NOT!, not with the information given here. E.g. What if t2.column1
has doubles ?
=> Assuming there are no doubles in t2.column1
, then yes, put a UNIQUE INDEX
on said column and use a JOIN construction as it is more readable and easier to maintain. If it is going to be faster; that depends on what the query engine makes from it. In MSSQL the query-optimizer (probably) would consider them the same thing; maybe MySQL is 'not so eager' to recognize this... don't know.
=> Assuming there can be doubles in t2.column1
, put a (non-unique) INDEX
on said column and rewrite the WHERE IN (SELECT ..)
into a WHERE EXISTS ( SELECT * FROM t2 WHERE t2.column1 = t1.column1)
. Again, mostly for readability and ease of maintenance; most likely the query engine will treat them the same...
The things to remember are
- Always make sure you have proper indexing (but don't go overboard)
- Always realize that what really happens will be an interpretation of your sql-code; not a 'direct translation'. You can write the same functionality in different ways to achieve the same goal. And some of these are indeed more resilient to different scenarios.
If you only have 10 rows, pretty much everything works. If you have 10M rows it could be worth examining the query plan... which most-likely will be different from the one with 10 rows.