南大通用 GBase 8s 数据库 查询计划使用介绍

在数据库开发和优化过程中,查询计划是一个重要的工具。它可以帮助我们理解 SQL 查询的执行过程,从而优化查询性能。本文将详细介绍如何在 GBase 8s 中使用查询计划,以及如何阅读和分析查询计划的内容。
数据准备
在脚本prepare_data.sh里,完成了建表,插入数据,截取内容如下:
create table customer(customer_id int,customer_num Serial,customer_name char(10));
insert into customer values (1,101,'zhangsan');
insert into customer values (2,102,'lisi');
insert into customer values (3,103,'wangwu');
create table order(order_id int,customer_num Serial,order_name varchar(20));
insert into order values(1,101,'phone'),(2,102,'computer'),(3,103,'car model');
insert into order values(2,102,'computer');
insert into order values(3,103,'car model');
create index idx_order on order(customer_num);
set explain on
set explain on 是GBase 8s后台查看执行计划的命令,在执行SQL语句执行该语句。
选项:
ON: 在为每个后续查询生成评估并将结果写入当前目录中的输出文件。如果文件已经存在,那么新输出会附加到现有文件中。
AVOID_EXECUTE:在数据库服务器将查询计划打印到输出文件中时,防止SELECT、INSERT、UPDATE 或者DELETE语句执行。
OFF: 终止SET EXPLAIN 语句,不再为后续查询生成评估或不再将评估写入输出文件。
FILE TO: 为每个后续查询生成评估并使能够指定输出文件的位置。
下面SQL语句的查询计划,执行该SQL语句:
set explain on;
select * from customer,order where customer.customer_num = order.customer_num;
如果只想了解SQL语句的查询计划但不希望执行SQL语句,可以执行:
set explain on avoid_execute;
select * from customer,order where customer.customer_num = order.customer_num;
如果想了解了解SQL语句的查询计划并将结果输出到自己指定的文件,该文件为数据库服务端的文件信息:
set explain on avoid_execute;
set explain file to ‘/tmp/explain.out’;
select * from customer,order where customer.customer_num = order.customer_num;
阅读查询计划
执行计划包含以下几部分:
(1)执行时间(OPTIMIZATION TIMESTAMP): 执行SQL语句的实际时间
(2)查询语句:执行计划的原SQL语句
(3)预估开销值(Estimated Cost):优化器用来决定查询路径的数值。与查询耗时不直接相关,不能用于比较不同SQL的执行效率,可用于比较相同SQL不同执行计划的执行开销。
(4)预估返回行数( Estimated # of Rows Returned):预估SQL语句返回的数据行数。
(5)表的访问顺序
(6)表的访问方法
(7)使用的索引路径
(8)实际执行情况统计
根据执行计划的划分,在下面的例子中一一对应说明:
QUERY: (OPTIMIZATION TIMESTAMP: 12-20-2024 15:07:14) --①执行时间
------
--②执行的SQL语句
select * from customer,order where customer.customer_num = order.customer_num
Estimated Cost: 4 --③预估开销值
Estimated # of Rows Returned: 1 --④预估返回行数
--⑤表访问顺序1.customer 表 2.order表
--⑥表访问方法 customer使用顺序扫描(SEQUENTIAL SCAN)order使用索引(INDEX PATH
)
root.customer: SEQUENTIAL SCAN
root.order: INDEX PATH
--⑦使用的索引名为”idx_order”,索引包含字段customer_num,使用的索引过滤条件为root.customer.customer_num = root.order.customer_num
Index Name: root.idx_order
Index Keys: customer_num (Serial, fragments: ALL)
Lower Index Filter: root.customer.customer_num = root.order.customer_num
NESTED LOOP JOIN
--⑧ 实际执行情况统计
--名称 含义
--type 操作类型
--table 表名
--rows_prod 实际操作行数
--est_rows 预估操作行数
--rows_scan 实际扫描行数
--time 实际执行实际
--est_cost 预估开销值
Query statistics:
-----------------
Table map :
----------------------------
Internal name Table name
----------------------------
t1 customer
t2 order
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t1 3 1 3 00:00.00 2
type table rows_prod est_rows rows_scan time est_cost
-------------------------------------------------------------------
scan t2 3 3 3 00:00.00 0
type rows_prod est_rows time est_cost
-------------------------------------------------
nljoin 3 1 00:00.00 4
通过本文的介绍,我们了解了如何在 GBase 8s 中生成和查看查询计划,以及如何阅读和分析查询计划的内容。查询计划工具,可以帮助我们优化 SQL 查询,提高数据库性能。希望本文对您有所帮助!
评论
热门帖子
- 12023-05-09浏览数:19113
- 22023-09-25浏览数:12231
- 32020-05-11浏览数:12046
- 42019-04-26浏览数:11487
- 52023-07-04浏览数:10691