说明:

日常业务系统在使用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. Rollup的基本使用

    Rollup的基本使用 rollup.js是一个模块打包工具,可以使项目从一个入口文件开始,将所有使用到的模块文件都打包到一个最终的发布文件中,Rollup极其适合构建一个工具库,Vue.js源码就是 ...

  2. 《系列二》-- 8、单例bean的创建

    目录 1 源码入口概述 2 getSingleton(beanName, ObjectFactory) 的行为 总结 阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主要内容需 ...

  3. 项目实战:Qt球机控制工具 v1.0.0(球机运动八个方向以及运动速度,设置运动到指定角度,查询当前水平和垂直角度)

    需求   1.调试球机控制,方向速度,设置到指定的角度:  2.支持串口,485等基于串口的协议端口配置打开:  3.子线程串口控制和.子线程协议解析:  4.支持球机水平运动速度.垂直运动速度设置: ...

  4. 如何渲染最原始的yuv视频数据?

    一.整体思路 我们在用纹理增加细节那篇文章中提到过,要将图片渲染在屏幕上,首先要拿到图片的像素数组数据,然后将像素数组数据通过纹理单元传递到片段着色器中,最后通过纹理采样函数将纹理中对应坐标的颜色值采 ...

  5. Java新建一个子线程异步运行方法

    如何在运行主方法的同时异步运行另一个方法,我是用来更新缓存: 1. 工具类 public class ThreadPoolUtils { private static final Logger LOG ...

  6. 【生成对抗网络学习 其二】GAN(keras实现)代码阅读笔记

    想来想去还是记录一下吧,主要是怕以后时间长忘了 好记性不如烂笔头 代码来自eriklindernoren的开源GAN实现:https://github.com/eriklindernoren/Kera ...

  7. STL-RBTree模拟实现

    #pragma once #include<assert.h> #include<iostream> using std::cout; using std::endl; usi ...

  8. 十: SQL执行流程

    SQL执行流程 1. MySQL 中的 SQL执行流程 MySQL的查询流程: 1.1 查询缓存 Server 如果在查询缓存中发现了这条 SQL 语句,就会直接将结果返回给客户端:如果没 有,就进入 ...

  9. VC-MFC 登陆界面 + 数据库账号+密码

    1 // DlgUser.cpp : 实现文件 2 // 3 4 #include "stdafx.h" 5 #include "Login.h" 6 #inc ...

  10. 从 HPC 到 AI:探索文件系统的发展及性能评估

    随着 AI 技术的迅速发展,模型规模和复杂度以及待处理数据量都在急剧上升,这些趋势使得高性能计算(HPC)变得越来越必要.HPC 通过集成强大的计算资源,比如 GPU 和 CPU 集群,提供了处理和分 ...