查看查询执行情况 ------------------ .. code-block:: html :linenos: 使用\timing命令可以打印出查询执行的时间。 test=# \timing on Timing is on. test=# select * from foo; # 这时再执行 SQL 语句会给出语句执行时间。 id | name ----+------ 1 | hawq 2 | hdfs (2 rows) Time: 16.369 ms test=# \timing off # 关闭时间输出 Timing is off. 使用explain语句可以显示出查询计划。 test=# explain select count(*) from foo; QUERY PLAN ------------------------------------------------------------------------------------ Aggregate (cost=1.04..1.05 rows=1 width=8) -> Gather Motion 12:1 (slice1; segments: 12) (cost=0.00..1.04 rows=2 width=0) -> Seq Scan on foo (cost=0.00..1.01 rows=1 width=0) Optimizer: Postgres query optimizer NewExecutor: ON (5 rows) test=# 使用 explain analyze 可以显示出查询在具体执行时的状态,包括每一个操作符开始执行时间,以及结束时间,可以帮助用户找到查询的瓶颈,进而优化查询。关于查询计划以及 explain analyze 的执行结果的解释可以参考查询计划与查询执行章节。针对一个查询,可能会有无数个查询计划。得出优化的查询计划是查询优化器的功能。一个查询执行时间的长短与查询的计划有很大关系,所以熟悉查询计划以及具体查询的执行对查询优化有很大意义。 .. code-block:: html :linenos: test=# explain analyze select count(*) from foo; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=1.04..1.05 rows=1 width=8) (actual time=6.129..6.132 rows=1 loops=1) 1 TupleBatch of 1 Tuple, (1/1/1)(avg/min/max) Tuple/TupleBatch, 47 B -> Gather Motion 12:1 (slice1; segments: 12) (cost=0.00..1.04 rows=2 width=0) (actual time=5.575..5.971 rows=2 loops=1) 2 TupleBatch of 2 Tuple, (1/1/1)(avg/min/max) Tuple/TupleBatch, 0 B InputStream Info: 28 byte(s) (76.808 KB/s) in 0.356 ms with 2 read call(s). -> Seq Scan on foo (cost=0.00..1.01 rows=1 width=0) (actual time=1.846..3.567 rows=1 loops=1) (seg4) 1 TupleBatch of 1 Tuple, (1/1/1)(avg/min/max) Tuple/TupleBatch, 0 B (seg4) SkipCtid: on Optimizer: Postgres query optimizer NewExecutor: ON Data Locality Ratio: 1.00 Planning Time: 4.371 ms (slice0) Executor memory: 32K bytes. (slice1) Executor memory: 89K bytes avg x 12 workers, 89K bytes max (seg0). Memory used: 262144kB Execution Time: 9.913 ms (18 rows) test=#