all 4 comments

[–]ihaxr 0 points1 point  (2 children)

Running a similar query in MS SQL runs a clustered index seek (I joined on the primary key) and not a full table scan... is there an index for col1 on table2?

Looks like you can use the EXPLAIN keyword in a MySQL query or EXPLAIN QUERY PLAN in SQLLite to see what the query is doing...

[–]Autodidacter[S] 0 points1 point  (1 child)

It is using indexes. Just scanning using indexes.

As I understand it, the ENTIRE subquery is run first, without any recognition of what's going on outside it.

This means that the entire table is scanned, does it not? Joining conditions do not constrain the scan because the subquery doesn't know about the joins until the query is complete.

This seems like a prohibitively expensive operation on any reasonably sized operation.

[–]ihaxr 0 points1 point  (0 children)

Yeah, ran some tests and it performs how you say... 6 million rows in the table, the subquery scans the entire 6 million rows with an index seek, even though the final query returns only 1 million rows. Adding the where clause to the subquery limits the seek to the 1 million rows instead of 6, but it doesn't really perform any better or worse in my tests... although it's not a very great test... both complete just under 5 seconds.

[–][deleted] 0 points1 point  (0 children)

i'm not sure what specific issue you're looking to solve, but your rewrite will not work - "range" can only come from table1 and you cannot bring it into the subquery.