KingbaseES 查询优化消除SubPlan
说明:
日常业务系统在使用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的更多相关文章
- PostgreSQL查询优化逻辑优化之其他
上一节我们介绍了PostgreSQL的子查询优化,子查询优化把一部分可以优化的子查询上拉到主查询成为join. preprocess_expression 将表达式(目标列,where,join,ha ...
- PostgreSQL查询优化简介
简介 PostgreSQL查询优化器执行过程 语法分析:生成查询树 语义检查:对SQL表达的语义进行检查 查询优化 视图重写 逻辑优化:子查询优化,条件化简,等价谓词重写,连接消除,得到逻辑计划 物理 ...
- postgresql子查询优化(提升子查询)
问题背景 在开发项目过程中,客户要求使用gbase8s数据库(基于informix),简单的分页页面响应很慢.排查发现分页sql是先查询出数据在外面套一层后再取多少条,如果去掉嵌套的一层,直接获取则很 ...
- KingbaseES 并行查询
背景:随着硬件技术的提升,磁盘的IO能力及CPU的运算能力都得到了极大的增强,如何充分利用硬件资源为运算加速,是数据库设计过程中必须考虑的问题.数据库是IO和CPU密集型的软件,大规模的数据访问需要大 ...
- MySQL查询优化之explain的深入解析
在分析查询性能时,考虑EXPLAIN关键字同样很管用.EXPLAIN关键字一般放在SELECT查询语句的前面,用于描述MySQL如何执行查询操作.以及MySQL成功返回结果集需要执行的行数.expla ...
- SQL优化----百万数据查询优化
百万数据查询优化 1.合理使用索引 索引是数据库中重要的数据结构,它的根本目的就是为了提高查询效率.现在大多数的数据库产品都采用IBM最先提出的ISAM索引结构.索引的使用要恰到好处,其使用原则如下: ...
- MySql学习(六) —— 数据库优化理论(二) —— 查询优化技术
逻辑查询优化包括的技术 1)子查询优化 2)视图重写 3)等价谓词重写 4)条件简化 5)外连接消除 6)嵌套连接消除 7)连接消除 8)语义优化 9)非SPJ优化 一.子查询优化 1. ...
- 1025WHERE执行顺序以及MySQL查询优化器
转自http://blog.csdn.net/zhanyan_x/article/details/25294539 -- WHERE执行顺序-- 过滤比较多的放在前面,然后更加容易匹配,从左到右进行执 ...
- SQL 查询优化 索引优化
sql语句优化 性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化. 为了获得稳定的执行性能,SQL语句越简单越好.对复杂的 ...
- 列存储段消除(ColumnStore Segment Elimination)
列存储索引是好的!对于数据仓库和报表工作量,它们是真正的性能加速器.与聚集列存储结合,你会在常规行存储索引(聚集索引,非聚集索引)上获得巨大的压缩好处.而且创建聚集列存储索引非常简单: CREATE ...
随机推荐
- 易语言连接Mysql
最近在写游戏的辅助工具研究了下易语言,下面就说下如何连接Mysql. .版本 2 .支持库 mysql .支持库 spec Mysql句柄 = 连接MySql ("127.0.0.1&quo ...
- C++常用快读
1.快读相关代码 inline int read() { int x=0,f=0; char ch=getchar(); while(!isdigit(ch))f|=(ch=='-'),ch=getc ...
- go语言编程常见问题
在Goland中运行单元测试报错Error: Cannot find package 如下图,在Goland中运行单元测试时报错:"Error: Cannot find package&qu ...
- 矩池云产品最新动态 All in One
AI/ML 的不断革新,让我们看到了更多激动人心的应用方向,也迸发了更多的训练&应用场景. 在用户的反馈和建议下,矩池云持续丰富和优化在 AI+Science 链路上的相关产品,为了帮助研究人 ...
- 【Azure Redis】Azure Redis添加了内部虚拟网络后,其他区域的主机通过虚拟网络对等互连访问失败
问题描述 跨区域无法访问Azure Redis服务, Redis 启用了Network并设置在一个VNET中,现在客户端部署在另一个区域数据中心中,两个数据中心区域使用VNET Peer(对等互连)访 ...
- 【Azure 环境】如果Azure中的某一个资源被删除后是否可以查看到删除的记录呢?如Resource Group
问题描述 当一个资源从Azure中删除后,是否有地方可以查看到这些操作的记录呢?如操作人,操作时间等. 问题解答 可以的.通过 Azure订阅页面的活动日志,可以查看所有对订阅下资源的操作记录,包含D ...
- 分布式事务框架seata入门
一.简介 在近几年流行的微服务架构中,由于对服务和数据库进行了拆分,原来的一个单进程本地事务变成多个进程的本地事务,这时要保证数据的一致性,就需要用到分布式事务了.分布式事务的解决方案有很多,其中国内 ...
- 手把手带你认识GaussDB轻量化运维管理工具
本文分享自华为云社区<GaussDB轻量化运维管理工具介绍>,作者: Gauss松鼠会小助手. 一.GaussDB 运维管理平台简介 开放生态层 友好Web界面,多云皮肤个性化定制 丰富的 ...
- inner join on 1=1 在查询中的高级用法
最近在项目中看到一个查询语句,让我有兴趣去研究.研究.查询语句如下: 重点分析第二个INNER JOIN ON 1 = 1 这个语句:内连接表示查询两个表的交集,而且ON的条件为 1=1 就表示连接 ...
- Dendron vscode笔记插件 F12 可自动跳转 页面 很实用
Dendron vscode笔记插件 F12 可自动跳转 页面 很实用 Dendron 技巧汇总 新建工作区 新建一个 工作区 建立一个空目录 然后 ctrl + shift P 输入 init 就可 ...