KingbaseES下查看SQL执行计划
关键字:
执行计划、EXPLAIN、人大金仓、KingbaseES
一、摘要
本文介绍了执行计划对数据库的重要性以及KINGBASE中使用EXPLAIN查看执行计划。
执行计划
数据库执行计划是数据库系统在执行查询或者操作时生成的一个详细的执行策略的描述,指示了数据库引擎如何获取数据、处理数据、执行操作及返回结果。
执行计划可以帮助用户了解查询是如何执行的,包括查询的顺序、连接方法、使用的索引等。通过查看执行计划,可以评估查询的效率,并进行必要的优化。可以根据执行计划的信息来优化查询语句、创建适当的索引或重写查询,以提高性能。
KingbaseES下查看执行计划
通过在SQL语句前添加EXPLAIN关键字,可以查看该SQL在数据库中的执行计划。具体的语法有:
| EXPLAIN [ ( option [, ...] ) ] statementEXPLAIN [ ANALYZE ] [ VERBOSE ] statement这里 option可以是:ANALYZE [ boolean ]VERBOSE [ boolean ]COSTS [ boolean ]SETTINGS [ boolean ]BUFFERS [ boolean ]TIMING [ boolean ]SUMMARY [ boolean ]FORMAT { TEXT | XML | JSON | YAML } | | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
下面是一个实例,有SQL如下:
EXPLAINSELECTl_returnflag,l_linestatus,SUM ( l_quantity ) AS sum_qty,SUM ( l_extendedprice ) AS sum_base_price,SUM ( l_extendedprice * ( 1 - l_discount ) ) AS sum_disc_price,SUM ( l_extendedprice * ( 1 - l_discount ) * ( 1 + l_tax ) ) AS sum_charge,AVG ( l_quantity ) AS avg_qty,AVG ( l_extendedprice ) AS avg_price,AVG ( l_discount ) AS avg_disc,COUNT ( * ) AS count_orderFROMlineitemWHEREl_shipdate <= DATE'1998-12-01' - INTERVAL '90' DAYGROUP BYl_returnflag,l_linestatusORDER BYl_returnflag,l_linestatus; |
---|
通过添加关键字EXPLAIN后发送到KES服务器,KES服务器会返回该SQL的执行计划,返回的执行结果如下:
Finalize GroupAggregate (cost=1346190.88..1346224.70 rows=6 width=236)Group Key: l_returnflag, l_linestatus-> Gather Merge (cost=1346190.88..1346216.38 rows=192 width=236)Workers Planned: 32-> Sort (cost=1345190.05..1345190.06 rows=6 width=236)Sort Key: l_returnflag, l_linestatus-> Partial HashAggregate (cost=1345189.81..1345189.97 rows=6 width=236)Group Key: l_returnflag, l_linestatus-> Parallel Seq Scan on lineitem (cost=0.00..1271314.89 rows=1846873 width=25)Filter: ((l_shipdate)::timestamp without time zone <= '1998-09-02 00:00:00'::timestamp without time zone) |
---|
可以看到该执行计划采用了并行查询,首先通过Parallel Seq Scan扫描了表lineitem,并添加了过滤条件Filter,然后进行Partial HashAggregate聚合,再然后进行sort排序,接下来将并行的结果通过Gather Merge进行合并,最后再进行最后的聚合。