sub_test表中根据group_id取得fk_test_id(2,3,4,5,6),然后在到test中,带入test_id=2,3,4,5,6取得查询数据,但是实际mysql的处理方式为:
Select * from test where exists (
select * from sub_test where group_id=10 and sub_test.test_id=test.id)
mysql将会扫描test中的所有数据,每条数据将会传到子查询中与sub_test关联,子查询不能首先被执行,如果test表很大的话,那么性能上将会出现问题;
这时候就需要改写查询了:
select t1.* from test t1,(select fk_test_id from sub_test where group_id=10 ) t2 where t1.test_id=t2.fk_test_id ;
联系客服