暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

AntDB数据库用户手册之执行计划

250

说明

在AntDB中,可以使用explain命令查看SQL的执行计划,帮助信息如下:

antdb=# \h explain
Command:     EXPLAIN
Description: show the execution plan of a statement
Syntax:
EXPLAIN [ ( option [, ...] ) ] statement
EXPLAIN [ ANALYZE ] [ VERBOSE ] statement

where option can be one of:

    ANALYZE [ boolean ]
    VERBOSE [ boolean ]
    COSTS [ boolean ]
    BUFFERS [ boolean ]
    TIMING [ boolean ]
    SUMMARY [ boolean ]
    FORMAT { TEXT | XML | JSON | YAML }

查看执行计划

explain select * from test1 where id=1; QUERY PLAN ------------------------------------------------------------- Cluster Gather (cost=0.00..27.68 rows=6 width=36) -> Seq Scan on test1 (cost=0.00..25.88 rows=6 width=36) Filter: (id = 1) (3 rows)

上述操作不会真正执行SQL语句,仅仅打印执行计划,如果想看SQL的真实执行计划,需要加上analyze关键字:

antdb=# explain analyze select * from test1 where id=1; QUERY PLAN ------------------------------------------------------------------------------------------------------- Cluster Gather (cost=0.00..27.68 rows=6 width=36) (actual time=1.527..1.527 rows=0 loops=1) -> Seq Scan on test1 (cost=0.00..25.88 rows=6 width=36) (actual time=0.054..0.054 rows=0 loops=1) Filter: (id = 1) Planning Time: 0.345 ms Execution Time: 2.256 ms (5 rows)

与之前相比较,会发现执行计划多了actual的内容,表示SQL真实执行时候的一些指标。

因为analyze会真正执行SQL,所以在查看insert、delete、update等语句的执行计划时,谨慎使用analyze

每一行代表一个执行步骤,其中的指标说明如下:

  • cost=0.00..27.68: 预计的cost大小,0.00表示第一行数据返回的cost,27.68表示所有数据返回的cost。
  • rows=6: 预估或者实际执行返回的行数。
  • width=36:预估的行宽度,单位字节。
  • time=1.527..1.527:实际执行的时间,单位 ms。与cost类似,先显示第一行返回的时间,再显示所有结果返回的时间。
  • loops=1: 该步骤执行了多少次。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论