Here's a couple of things you could try.
Try adding indexes:
... ON table1 (table1_id, table2_id)
Also, try adding the DISTINCT
keyword in subquery:
table2_id in (select DISTINCT table2_id from table1 where table1_id<1000)
Other than performance of the query that MySQL is using, I don't see anything there that should cause a "hang", other than that lock-all-tables
option. I'm assuming you've tested without that, and observed the same behavior.
Q: What do you suspect is causing the hang?
I suspect it's just the SELECT statement that is cranking. I think it's likely that for EVERY row in table2, the execution plan is doing a nested loops lookup from table1. I suspect that the IN (subquery)
predicate is being optimized into an equivalent EXISTS (correlated subquery)
.
Q: Why would adding indexes help?
Indexes may not help at all. But an appropriate index on table1 may cut down on the amount of time required to retrieve the results from the subquery. (It's likely the results from the subquery will be "cached", so that there will only be a savings when that subquery is first run, adn not a savings for each row from table2 that is evaluated.)