Explain plan with executing the SQL Query in PostgreSQL

Get the explain plan for SQL query with run in PostgreSQL

Explain plan with Analyze command will actually execute the command and give the execution plan according. It show output as actual run times. Explain plan helps to understand the SQL query that what index it used or using sequential scan and additional information like cost, disk , rows etc.

Check explain plan with executing the sql query:

Note: With Realtime executing it will give you execution time also while generating execution plan

testdb=# explain analyze select actor_id from actor where actor_id = 200;
                                          QUERY PLAN
-----------------------------------------------------------------------------------------------
 Seq Scan on actor  (cost=0.00..4.50 rows=1 width=4) (actual time=0.050..0.050 rows=1 loops=1)
   Filter: (actor_id = 200)
   Rows Removed by Filter: 199
 Planning Time: 0.133 ms
 Execution Time: 0.068 ms
(5 rows)


testdb=# explain analyze select * from film where film_id=33;
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Index Scan using film_pkey on film  (cost=0.28..8.29 rows=1 width=384) (actual time=0.028..0.029 rows=1 loops=1)
   Index Cond: (film_id = 33)
 Planning Time: 0.152 ms
 Execution Time: 0.051 ms
(4 rows)

Note: With Insert, update, delete or execute statement we need to take care for using ANALYZE parameter in Explain plan command because it actually modified your data. You can use these statements with help of rollback like

Begin;
EXPLAIN ANALYZE ....;
ROLLBACK;

To avoid insert or delete or update we will write as shown below example:


Example:
testdb=# explain analyze insert into test values(10);
                                         QUERY PLAN
-------------------------------------------------------------------------------
 Insert on test  (cost=0.00..0.01 rows=0 width=0) (actual time=0.019..0.019 rows=0 loops=1)
   ->  Result  (cost=0.00..0.01 rows=1 width=4) (actual time=0.001..0.001 rows=1 loops=1)
 Planning Time: 0.018 ms
 Execution Time: 0.031 ms
(4 rows)


testdb=# select * from test;
 id
----
 10
(1 row)

Use this way to avoid actual execution:
Begin;
explain analyze insert into test values(11);
Rollback;

Execution output:
testdb=# Begin;
BEGIN
testdb=*# explain analyze insert into test values(11);
                                         QUERY PLAN
--------------------------------------------------------------------------------------------
 Insert on test  (cost=0.00..0.01 rows=0 width=0) (actual time=0.073..0.074 rows=0 loops=1)
   ->  Result  (cost=0.00..0.01 rows=1 width=4) (actual time=0.002..0.003 rows=1 loops=1)
 Planning Time: 0.312 ms
 Execution Time: 0.111 ms
(4 rows)


testdb=*# Rollback;
ROLLBACK
testdb=# select * from test;
 id
----
 10
(1 row)
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.