3.3 通过EXPLAIN进行SQL语句优化

说明:

  • 使用explain能显示SQL语句的执行计划;
  • 执行计划将显示SQL语句所引用的表会采用什么样的扫描方式,如:简单的顺序扫描、索引扫描等。如果引用了多个表,执行计划还会显示用到的JOIN算法;
  • 执行计划的最关键的部分是语句的预计执行开销,这是计划生成器估算执行该语句将花费多长的时间;
  • 若指定了 YZE 选项,则该语句模拟执行并形成最优的执行计划(并非真正执行),然后根据实际的运行结果显示统计数据,包括每个计划节点内时间总开销(毫秒为单位)和实际返回的总行数。这对于判断计划生成器的估计是否接近现实非常有用。

步骤 1 用root用户登录装有openGauss数据库服务的操作系统然后用 su - omm命令切换至omm用户环境,登录后信息如下。

Welcome to 4.19.90-2003.4.0.0036.oe1.aarch64
System information as of time: Tue Jul 21 09:21:11 CST 2020
System load: 0.01
Processes: 109
Memory used: 6.7%
Swap used: 0.0%
Usage On: 15%
IP address: 192.168.0.96
Users online: 1
[root@ecs-e1b3 ~]# su - omm
Last login: Fri Jul 10 19:05:39 CST 2020 on pts/0
Welcome to 4.19.90-2003.4.0.0036.oe1.aarch64
System information as of time: Tue Jul 21 09:21:25 CST 2020
System load: 0.01
Processes: 111
Memory used: 7.0%
Swap used: 0.0%
Usage On: 15%
IP address: 192.168.0.96
Users online: 1
[omm@ecs-e1b3 ~]$

步骤 2 先启动数据库服务,然后使用gsql客户端以管理员用户身份连接postgres数据库,假设端口号为26000。
启动数据库服务。

[omm@ecs-e1b3 ~]$ gs_om -t start;
Starting cluster.
=========================================
=========================================
Successfully started.

然后连接postgres数据库。

[omm@ecs-e1b3 ~]$ gsql -d postgres -p 26000 -r
gsql ((openGauss 1.1.0 build 38a9312a) compiled at 2020-05-27 14:56:08 commit 472 last mr 549 )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

postgres=#

步骤 3 创建student表。

postgres=# CREATE TABLE student
( std_id INT NOT NULL,
std_name VARCHAR(20) NOT NULL,
std_** VARCHAR(6),
std_birth DATE,
std_in DATE NOT NULL,
std_address VARCHAR(100)
);

CREATE TABLE

步骤 4 表数据插入。

INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (1,'张一','男','1993-01-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (2,'张二','男','1993-01-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (3,'张三','男','1993-01-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (4,'张四','男','1993-01-04','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (5,'张五','男','1993-01-05','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (6,'张六','男','1993-01-06','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (7,'张七','男','1993-01-07','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (8,'张八','男','1993-01-08','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (9,'张九','男','1993-01-09','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (10,'李一','男','1993-01-10','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (11,'李二','男','1993-01-11','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (12,'李三','男','1993-01-12','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (13,'李四','男','1993-01-13','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (14,'李五','男','1993-01-14','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (15,'李六','男','1993-01-15','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (16,'李七','男','1993-01-16','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (17,'李八','男','1993-01-17','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (18,'李九','男','1993-01-18','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (19,'王一','男','1993-01-19','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (20,'王二','男','1993-01-20','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (21,'王三','男','1993-01-21','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (22,'王四','男','1993-01-22','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (23,'王五','男','1993-01-23','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (24,'王六','男','1993-01-24','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (25,'王七','男','1993-01-25','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (26,'王八','男','1993-01-26','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (27,'王九','男','1993-01-27','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (28,'钱一','男','1993-01-28','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (29,'钱二','男','1993-01-29','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (30,'钱三','男','1993-01-30','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (31,'钱四','男','1993-02-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (32,'钱五','男','1993-02-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (33,'钱六','男','1993-02-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (34,'钱七','男','1993-02-04','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (35,'钱八','男','1993-02-05','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (36,'钱九','男','1993-02-06','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (37,'吴一','男','1993-02-07','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (38,'吴二','男','1993-02-08','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (39,'吴三','男','1993-02-09','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (40,'吴四','男','1993-02-10','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (41,'吴五','男','1993-02-11','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (42,'吴六','男','1993-02-12','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (43,'吴七','男','1993-02-13','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (44,'吴八','男','1993-02-14','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (45,'吴九','男','1993-02-15','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (46,'柳一','男','1993-02-16','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (47,'柳二','男','1993-02-17','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (48,'柳三','男','1993-02-18','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (49,'柳四','男','1993-02-19','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_**,std_birth,std_in,std_address) VALUES (50,'柳五','男','1993-02-20','2011-09-01','江苏省南京市雨花台区');

步骤 5 数据查询统计。

postgres=# select count(*) from student;
count
-------
50
(1 row)

postgres=# select * from student order by std_id;
std_id | std_name | std_** | std_birth | std_in | std_address
--------+----------+---------+---------------------+---------------------+----------------------
1 | 张一 | 男 | 1993-01-01 00:00:00 | 2011-09-01 00:00:00 | 江苏省南京市雨花台区
2 | 张二 | 男 | 1993-01-02 00:00:00 | 2011-09-01 00:00:00 | 江苏省南京市雨花台区
3 | 张三 | 男 | 1993-01-03 00:00:00 | 2011-09-01 00:00:00 | 江苏省南京市雨花台区
4 | 张四 | 男 | 1993-01-04 00:00:00 | 2011-09-01 00:00:00 | 江苏省南京市雨花台区
……………..

步骤 6 查看表信息。

postgres=# \d student
Table "public.student"
Column | Type | Modifiers
-------------+--------------------------------+-----------
std_id | integer | not null
std_name | character varying(20) | not null
std_** | character varying(6) |
std_birth | timestamp(0) without time zone |
std_in | timestamp(0) without time zone | not null
std_address | character varying(100) |

步骤 7 收集表的统计信息。

postgres=# **YZE VERBOSE student;
INFO: **yzing "public.student"(dn_6001 pid=48036)
INFO: **YZE INFO : "student": scanned 1 of 1 pages, containing 50 live rows and 0 dead rows; 50 rows in sample, 50 estimated total rows(dn_6001 pid=48036)
**YZE

使用**YZE VERBOSE语句更新统计信息,会同时输出表的相关信息。
步骤 8 查看语句的执行计划。

postgres=# explain select * from student where std_id=30;
QUERY PLAN
--------------------------------------------------------
Seq Scan on student (cost=0.00..1.62 rows=1 width=62)
Filter: (std_id = 30)
(2 rows)

Seq Scan on student 表示使用的是全表扫描。

步骤 9 给表添加主键。

postgres=# alter table student add primary key (std_id);
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "student_pkey" for table "student"
ALTER TABLE

步骤 10 再次查看表信息。
确定主键是否建好。

postgres=# \d student
Table "public.student"
Column | Type | Modifiers
-------------+--------------------------------+-----------
std_id | integer | not null
std_name | character varying(20) | not null
std_** | character varying(6) |
std_birth | timestamp(0) without time zone |
std_in | timestamp(0) without time zone | not null
std_address | character varying(100) |
Indexes:
"student_pkey" PRIMARY KEY, btree (std_id) TABLESPACE pg_default

student_pkey 为主键名称。

步骤 11通过hint来优化语句扫描方式。
通过加hint来使查询语句进行索引扫描。

postgres=# explain select /*+indexscan(student student_pkey)*/ * from student where std_id=30;
QUERY PLAN
----------------------------------------------------------------------
[Bypass]
Index Scan using student_pkey on student (cost=0.00..8.27 rows=1 width=62)
Index Cond: (std_id = 30)
(3 rows)

postgres=#

Index Scan using student_pkey on student 表示语句通过student表上的主键索引student_pkey进行了索引扫描。

步骤 12退出数据库

postgres=# \q

EXPLAIN进行SQL优化实验结束。

openGauss 数据库维护管理指导(四)相关推荐

  1. openGauss数据库维护管理指导(下)

    上期我们介绍了openGauss数据库操作系统参数检查.健康状态检查.数据库性能检查,本期继续介绍日志检查和清理.最大连接数设置.表和索引的例行维护相关内容. 日志检查 4.1 实验介绍 4.1.1 ...

  2. 【参赛作品15】openGauss数据库维护管理

    openGauss数据库维护管理 openGauss数据库维护管理 1 操作系统参数检查 1.1 实验介绍 1.1.1 关于本实验 gs_checkos工具用来帮助检查操作系统.控制参数.磁盘配置等内 ...

  3. openGauss数据库开发指导手册(下)

    本篇将介绍学校数据模型以及学校数据模型表操作. 1.4 学校数据模型 1.4.1 关于本实验 以学校数据库模型为例,介绍openGauss数据库数据库.表.表空间.用户及其它对象,以及SQL语法使用的 ...

  4. OpenGauss线程管理-检查点线程-Checkpointer

    OpenGauss线程管理-检查点线程-Checkpointer 检查点线程处理所有检查点. 检查点会在自上一个检查点后经过一定时间后自动分派,并且它也可以发出信号以执行请求的检查点. (要求每隔这么 ...

  5. OpenGauss线程管理-系统日志线程-syslogger

    OpenGauss线程管理-系统日志线程-syslogger 系统日志(syslogger)出现在Postgres 8.0中.它通过重定向到管道来捕获postmaster.后端和其他子进程的所有std ...

  6. 敏捷管理第四章《敏捷落地--团队试点》

    敏捷管理第四章<敏捷落地--团队试点> 敏捷落地–团队试点,让你具有大局观 我们上章节主要讲述了评估诊断,具备了项目短期规划的能力,接下来我们主要重点会围绕长期规划来进行展开.那么团队试点 ...

  7. [ITIL]-ITIL4服务管理的四个维度

    ITIL4服务管理的四个维度 四个维度 组织和人员 合作伙伴和供应商 信息和技术 价值流和流程 四个维度 服务管理的四个维度是: 组织和人员. 合作伙伴和供应商. 信息和技术. 价值流和流程 组织和人 ...

  8. OpenGauss线程管理-主线程-Postmaster(1)

    OpenGauss线程管理-主线程-Postmaster(1) 主线程postmaster负责内存.全局信息.信号.线程池等的初始化,用来创建其他子线程,OpenGauss是单进程多线程,在程序启动时 ...

  9. react中数据状态管理的四种方案

    我们为什么需要状态管理? (1) 一个是为了解决相邻组件的通信问题. 虽然可以通过「状态提升」解决,但有两个问题: 每次子组件更新,都会触发负责下发状态的父组件的整体更新(使用 Context 也有这 ...

最新文章

  1. oracle如何删除重复数据第一条,oracle删除重复数据保留第一条记录
  2. 近期论文中的数据集整理0409
  3. 企业网络推广网站排名首页但确保用户体验也是企业网络推广的关键
  4. 基于BERT模型的文本分类研究 TensorFlow2实现(内附源码)【自然语言处理NLP-100例】
  5. 2018-2019-2 20175204 张湲祯 实验五《网络编程与安全》实验报告
  6. python __builtins__ float类 (25)
  7. python切片输出_Python语言之详解切片
  8. 在线班课程-第10周 http基础知识、nginx网站服务
  9. 论文选题的原则、来源与方法
  10. java 网络五子棋游戏_基于JAVA的网络五子棋游戏
  11. 突破领英限制如何查找非好友电话,邮箱技巧
  12. 古琴入门之基本指法(右手)一
  13. JDK环境配置及Python、Pycharm、Git
  14. C++ Qt自建网页浏览器
  15. Swift - 判等
  16. android 刷机原理,分区。
  17. 树莓派打造一个适合音乐制作的系统(声卡部分)
  18. 全志JAVA_全志R11处理器参数详细说明
  19. 使用SVN构建自己的本地代码库
  20. 选择头像php,随机头像PHP版

热门文章

  1. 爱奇艺视频QSV格式转为MP4格式----SQV格式转换
  2. 煤炭企业管理中项目化管理的应用探寻
  3. 如何安装python codecs
  4. 增删改查sql语句基本语法
  5. 学习HTML5的一些总结
  6. 创始人亲自操刀,呷哺呷哺打响“翻身仗”?
  7. 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个1~9的数字。例如2+22+222+2222+22222(此时共有5个数相加)。
  8. 劳动保障职业学院计算机专业,北京劳动保障职业学院2019年各省录取分数线详情...
  9. java中允许多重继承吗,java允许接口的多重继承吗
  10. 程序之三高(高可用、高性能、高并发)