分享一篇:sql语句中使用子查询,可能会引起查询的性能问题,查询时间会变长

前段时间,做自动化适配的时候,查找需要的数据的时候,使用到了dblink,跨数据库实例进行访问,整段sql拼接再加上dblink,在plsql查询的时候,性能还不是很长时间,最多2分钟可以查到,前期调试阶段,在plsql里面调,没有留意到这个性能问题,后期,投入自动化使用的时候,就出现问题了,一个脚本,查找需要数据的方法,耗时长达10分钟。

原始版本设计:

查找需要数据的原有sql进行拼接筛选

如:

select * from student t ,class b where t.studentNo=b.studentNo ....中间省略其他很多过滤的sql

and t.studentNo not in (select c.studentNo from scort c where c.sort>'90') --中间使用的子查询

分析耗时长的原因:

  1.使用plsql查询,速度还是可以接受的(使用到索引),使用自动化框架,怎么就变慢了  -- 原因是jdbc框架,没有使用索引来查找

  2.优化:先创建一个临时表,然后再创建关联

优化之后的sql

WITH sort_a AS (select c.studentNo from scort c where c.sort>'90')

select * from student t ,class b,sort_a d where t.studentNo=b.studentNo and t.studentNo=d.studentNo

备注:可能做开发的同事,比较清楚,一般查询里面,都不建议在sql语句里面加很多的子查询,一般会建立临时表等的去过滤

with xx as的作用:https://www.cnblogs.com/mingforyou/p/8295239.html

原文地址:https://www.cnblogs.com/cuitang/p/14061448.html