关于exists中标量子查询条件改写经验

关于exists中标量子查询条件改写经验 1、问题最近在项目中遇到以下这种写法selectcount(1)from t1 where exists(select 1 from(select t3.GID,(selectLISTAGG(D1,,)from t2 where t2.gidt3.gid)as d1 from t3)a where a.d1 like%||C1||%and t1.gida.gid)and t1.c3 E1计划如下exists中存在标量子查询作为查询项后作为过滤条件的写法此写法一般会把标量子查询模块作为非相关查询表达式会转化为一行一行处理此做法比较低效建议exists中的标量子查询改写为简单的表关联后条件过滤方式。2、改写selectcount(1)from t1 where exists(select 1 from(select t3.GID,(selectLISTAGG(D1,,)from t2 where t2.gidt3.gid)as d1 from t3)a where a.d1 like%||C1||%and t1.gida.gid)and t1.c3 E1;计划如下执行时间从原来的11s降至0.22s。3、小结在子查询中套用标量子查询生成的查询项又作为过滤条件优化器对于此等情况会做成一行一行处理效率较为低下建议是改为简单的表关联过滤处理。