select t1.*,
(select count(*) from table2 t2 where t2.check=t1.check and rownum=1) as flag
from table1 t1
总觉得这样写方式或效率有问题
能否改成两表关联的形式,或者用where exists...
(select count(*) from table2 t2 where t2.check=t1.check and rownum=1) as flag
from table1 t1
总觉得这样写方式或效率有问题
能否改成两表关联的形式,或者用where exists...
from table1 t1
left join table2 on t1.check=t2.check
不过解释计划没有变化
SORT AGGREGATE
COUNT STOPKEY
TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN(table2.check)
TABLE ACCESS FULL(table1)
能否转成:
nest loop
TABLE ACCESS FULL(table1)
INDEX RANGE SCAN(table2.check)
from table1 t1
left join table2 on t1.check=t2.check;
再次强调
many-to-many
非常郁闷
对应table2中字段check值,可能存在多条记录,也可能不存在现在要构造一个结果集:
select table1.*, flag from ...flag=1表示存在一条或多条,0表示不存在
也就是
select t1.*,
(select count(*) from table2 t2 where t2.check=t1.check and rownum=1) as flag
from table1 t1
可是这样嵌套解释计划不理想,最好能类似nest loop的
from table1 t1,t2
where ti.check=t2.check(+)
这样不知道行不行,试试