说明:

日常业务系统在使用SQL语句进行查询时,开发人员容易将sql查询的子查询放到select语句中进行使用,会造成sql性能的下降。

数据准备:

test=#
test=# select count(1) from student;
count
-------
499
(1 行记录) test=# select count(1) from course;
count
-------
4
(1 行记录) test=# select count(1) from SCORE;
count
-------
506
(1 行记录)

示例1:

 test=# explain (verbose, analyze, buffers)   select student.sno , student.SNAME ,score.SNO ,SCORE.SCORE ,
(select CNAME from course where course.cno = score.cno) CNAME from student left join SCORE on true;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------
Nested Loop Left Join (cost=0.00..2064859.00 rows=252494 width=99) (actual time=0.020..365.552 rows=252494 loops=1)
Output: student.sno, student.sname, score.sno, score.score, (SubPlan 1)
Buffers: shared hit=505066
-> Seq Scan on public.student (cost=0.00..11.99 rows=499 width=11) (actual time=0.004..0.086 rows=499 loops=1)
Output: student.sno, student.sname, student.gender, student.phone, student.id_no, student.city, student.reg_date, student.job, student.company
Buffers: shared hit=7
-> Materialize (cost=0.00..78.59 rows=506 width=15) (actual time=0.000..0.027 rows=506 loops=499)
Output: score.sno, score.score, score.cno
Buffers: shared hit=71
-> Seq Scan on public.score (cost=0.00..76.06 rows=506 width=15) (actual time=0.004..0.121 rows=506 loops=1)
Output: score.sno, score.score, score.cno
Buffers: shared hit=71
SubPlan 1
-> Index Scan using cno_pk on public.course (cost=0.15..8.17 rows=1 width=78) (actual time=0.001..0.001 rows=1 loops=252494)
Output: course.cname
Index Cond: (course.cno = score.cno)
Buffers: shared hit=504988
Planning Time: 0.229 ms
Execution Time: 374.383 ms
(19 行记录)

提升查询:


test=# explain (verbose, analyze, buffers) select student.sno , student.SNAME ,score.SNO ,SCORE.SCORE ,course.CNAME CNAME
from student left join SCORE on true left join course on course.cno = score.cno;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------
Nested Loop Left Join (cost=20.35..3267.18 rows=252494 width=99) (actual time=0.026..42.468 rows=252494 loops=1)
Output: student.sno, student.sname, score.sno, score.score, course.cname
Buffers: shared hit=79
-> Seq Scan on public.student (cost=0.00..11.99 rows=499 width=11) (actual time=0.007..0.102 rows=499 loops=1)
Output: student.sno, student.sname, student.gender, student.phone, student.id_no, student.city, student.reg_date, student.job, student.company
Buffers: shared hit=7
-> Materialize (cost=20.35..100.28 rows=506 width=88) (actual time=0.000..0.024 rows=506 loops=499)
Output: score.sno, score.score, course.cname
Buffers: shared hit=72
-> Hash Left Join (cost=20.35..97.75 rows=506 width=88) (actual time=0.016..0.264 rows=506 loops=1)
Output: score.sno, score.score, course.cname
Inner Unique: true
Hash Cond: (score.cno = course.cno)
Buffers: shared hit=72
-> Seq Scan on public.score (cost=0.00..76.06 rows=506 width=15) (actual time=0.002..0.098 rows=506 loops=1)
Output: score.sno, score.cno, score.ino, score.exam_date, score.score, score.certificate
Buffers: shared hit=71
-> Hash (cost=14.60..14.60 rows=460 width=90) (actual time=0.008..0.009 rows=4 loops=1)
Output: course.cname, course.cno
Buckets: 1024 Batches: 1 Memory Usage: 9kB
Buffers: shared hit=1
-> Seq Scan on public.course (cost=0.00..14.60 rows=460 width=90) (actual time=0.004..0.005 rows=4 loops=1)
Output: course.cname, course.cno
Buffers: shared hit=1
Planning Time: 0.106 ms
Execution Time: 52.229 ms
(26 行记录)

示例2:

test=# explain (verbose, analyze, buffers) select score.cno,
(select course.cname from course where course.cno = score.cno ) sno from score ;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
Seq Scan on public.score (cost=0.00..4207.55 rows=506 width=83) (actual time=0.024..0.709 rows=506 loops=1)
Output: score.cno, (SubPlan 1)
Buffers: shared hit=1083
SubPlan 1
-> Index Scan using cno_pk on public.course (cost=0.15..8.17 rows=1 width=78) (actual time=0.001..0.001 rows=1 loops=506)
Output: course.cname
Index Cond: (course.cno = score.cno)
Buffers: shared hit=1012
Planning Time: 0.069 ms
Execution Time: 0.746 ms
(10 行记录)

提升查询:

test=# explain (verbose, analyze, buffers) select score.cno, course.cname
from score ,LATERAL(select course.cname from course where course.cno = score.cno) course ;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Hash Join (cost=20.35..97.75 rows=506 width=83) (actual time=0.019..0.236 rows=506 loops=1)
Output: score.cno, course.cname
Inner Unique: true
Hash Cond: (score.cno = course.cno)
Buffers: shared hit=72
-> Seq Scan on public.score (cost=0.00..76.06 rows=506 width=5) (actual time=0.005..0.087 rows=506 loops=1)
Output: score.sno, score.cno, score.ino, score.exam_date, score.score, score.certificate
Buffers: shared hit=71
-> Hash (cost=14.60..14.60 rows=460 width=90) (actual time=0.008..0.009 rows=4 loops=1)
Output: course.cname, course.cno
Buckets: 1024 Batches: 1 Memory Usage: 9kB
Buffers: shared hit=1
-> Seq Scan on public.course (cost=0.00..14.60 rows=460 width=90) (actual time=0.004..0.005 rows=4 loops=1)
Output: course.cname, course.cno
Buffers: shared hit=1
Planning Time: 0.125 ms
Execution Time: 0.271 ms
(17 行记录)

结论:

SQL查询语句SELECT中出现的子查询会随着查询结果条数进行Loop循环,每条进行一次数据的匹配查询,会造成查询性能的下降,

建议在写业务查询SQL时,将SELECT子查询进行提升到FROM中,减少扫描次数。

KingbaseES 查询优化消除SubPlan的更多相关文章

  1. PostgreSQL查询优化逻辑优化之其他

    上一节我们介绍了PostgreSQL的子查询优化,子查询优化把一部分可以优化的子查询上拉到主查询成为join. preprocess_expression 将表达式(目标列,where,join,ha ...

  2. PostgreSQL查询优化简介

    简介 PostgreSQL查询优化器执行过程 语法分析:生成查询树 语义检查:对SQL表达的语义进行检查 查询优化 视图重写 逻辑优化:子查询优化,条件化简,等价谓词重写,连接消除,得到逻辑计划 物理 ...

  3. postgresql子查询优化(提升子查询)

    问题背景 在开发项目过程中,客户要求使用gbase8s数据库(基于informix),简单的分页页面响应很慢.排查发现分页sql是先查询出数据在外面套一层后再取多少条,如果去掉嵌套的一层,直接获取则很 ...

  4. KingbaseES 并行查询

    背景:随着硬件技术的提升,磁盘的IO能力及CPU的运算能力都得到了极大的增强,如何充分利用硬件资源为运算加速,是数据库设计过程中必须考虑的问题.数据库是IO和CPU密集型的软件,大规模的数据访问需要大 ...

  5. MySQL查询优化之explain的深入解析

    在分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.expla ...

  6. SQL优化----百万数据查询优化

    百万数据查询优化 1.合理使用索引 索引是数据库中重要的数据结构,它的根本目的就是为了提高查询效率.现在大多数的数据库产品都采用IBM最先提出的ISAM索引结构.索引的使用要恰到好处,其使用原则如下: ...

  7. MySql学习(六) —— 数据库优化理论(二) —— 查询优化技术

    逻辑查询优化包括的技术 1)子查询优化  2)视图重写  3)等价谓词重写  4)条件简化  5)外连接消除  6)嵌套连接消除  7)连接消除  8)语义优化 9)非SPJ优化 一.子查询优化 1. ...

  8. 1025WHERE执行顺序以及MySQL查询优化器

    转自http://blog.csdn.net/zhanyan_x/article/details/25294539 -- WHERE执行顺序-- 过滤比较多的放在前面,然后更加容易匹配,从左到右进行执 ...

  9. SQL 查询优化 索引优化

    sql语句优化 性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化. 为了获得稳定的执行性能,SQL语句越简单越好.对复杂的 ...

  10. 列存储段消除(ColumnStore Segment Elimination)

    列存储索引是好的!对于数据仓库和报表工作量,它们是真正的性能加速器.与聚集列存储结合,你会在常规行存储索引(聚集索引,非聚集索引)上获得巨大的压缩好处.而且创建聚集列存储索引非常简单: CREATE ...

随机推荐

  1. AdvanceRestClient即食即用已集成definitions.json

    下载链接: https://pan.baidu.com/s/19Vdwda8L8PIsYc9v3S3cdA 提取码: diq8 最后完成即可使用

  2. Java操作EasyExcel实现导入导出入门

    介绍 EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单.节省内存著称.EasyExcel能大大减少占用内存的主要原因是在解析Excel时没有将文件数据一次性全部加载到内存中,而是从 ...

  3. spring boot读取json文件并实现接口查询

    0.源码下载 https://download.csdn.net/download/IndexMan/84238085 1.说明 最近需要在spring boot项目启动时读取json文件并保存到Li ...

  4. Java I/O 教程(九) FileWriter和FileReader

    FileWriter Java FileWriter 用于往文件中写入字符数据. 不像FileOutputStream类,你无需转换字符串成字节数组,因为它提供了直接写字符串的方法. 类定义 publ ...

  5. c++STL用法总结

    一.vector的用法 vector<int> vet; 1.排序:sort(vet.begin(), vet.end()), 时间复杂度O(nlogn) 2.查找:if(find(vet ...

  6. 神经网络优化篇:详解TensorFlow

    TensorFlow 先提一个启发性的问题,假设有一个损失函数\(J\)需要最小化,在本例中,将使用这个高度简化的损失函数,\(Jw= w^{2}-10w+25\),这就是损失函数,也许已经注意到该函 ...

  7. Qt5.15.0 升级至 Qt5.15.9 遇到的一些错误

    按照之前我写的文章教程,可以很简单的编译出静态库(仅供学习交流) 编译 windows 上的 qt 静态库 编译出静态库后,替换旧版本的库,见我另一篇文章教程 VS2019 配置 QT 库 之所以没有 ...

  8. [BUUCTF][Web][SUCTF 2019]EasySQL 1

    这一题有点蛋疼,比较难顶 看了别人的write up 也很难get到解题思路,感觉必须要拿到源码进行审计才能解 大佬们猜后端是这么写的 select $_POST['query'] || flag f ...

  9. 在vue项目中使用scss语法的准备步骤

    在vue项目中使用scss语法的准备步骤 个人总结: 在项目根目录cmd控制台中使用以下命令行,安装vue项目中使用scss的相关依赖; 在["项目根目录/build/webpack.bas ...

  10. gorm整理

    目录 1. 约定 2. 结构体标签 3. 创建记录 4. 更新 5.删除 6. 查询 7.关联 8.链式操作 9.范围 10.多个立即执行方法的注意事项 11.错误处理 12.钩子 13.事务 14. ...