analyze会真正执行,除了select语句,最好放在一个事务中运行,然后rollback.
BEGIN;
EXPLAIN ANALYZE UPDATE ...;
ROLLBACK;
常用方式如下:
EXPLAIN (ANALYZE, BUFFERS, TIMING) SELECT ...;
sql
ccc=# select * from t1;
id | name
----+------
1 | aaa
2 | bbb
3 | ccc
4 | ddd
(4 rows)
ccc=# begin;
BEGIN
ccc=*# EXPLAIN (ANALYZE, BUFFERS, TIMING) update t1 set name='yyy' where id=3;
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Update on t1 (cost=0.00..21.25 rows=0 width=0) (actual time=0.113..0.114 rows=0.00 loops=1)
Buffers: shared hit=3 dirtied=1
-> Seq Scan on t1 (cost=0.00..21.25 rows=4 width=64) (actual time=0.012..0.014 rows=1.00 loops=1)
Filter: (id = 3)
Rows Removed by Filter: 3
Buffers: shared hit=1
Planning:
Buffers: shared hit=3 read=6
Planning Time: 2.737 ms
Execution Time: 2.241 ms
(10 rows)
ccc=*# rollback;
ROLLBACK
ccc=# select * from t1;
id | name
----+------
1 | aaa
2 | bbb
3 | ccc
4 | ddd
(4 rows)
ccc=#
调试执行计划时可以临时enable/disable一些选项,详细列表见:
https://www.postgresql.org/docs/current/runtime-config-query.html
-- Plan with nested loop allowed (default)
EXPLAIN (ANALYZE, FORMAT JSON)
SELECT ... ;
-- Compare with nested loop discouraged
BEGIN;
SET LOCAL enable_nestloop = off;
EXPLAIN (ANALYZE, FORMAT JSON)
SELECT ... ;
COMMIT;