简体   繁体   中英

How to measure time in postgres

In postgres I want to know the time taken to generate plan. I know that \timing gives me the time taken to execute the plan after finding the optimal plan. But I want to find out the time which postgres takes in finding out the optimal plan. Is it possible to determine this time in postgres. If yes, then how?

Also query plan generators at times do not find the optimal plan. Can I force postgres to use the optimal plan for plan generation. If yes, then how can I do so?

For the time taken to prepare a plan and the time taken to execute it, you can use explain (which merely finds a plan) vs explain analyze (which actually runs it) with \timing turned on:

test=# explain select * from test where val = 1 order by id limit 10;
                                   QUERY PLAN                                   
--------------------------------------------------------------------------------
 Limit  (cost=0.00..4.35 rows=10 width=8)
   ->  Index Scan using test_pkey on test  (cost=0.00..343.25 rows=789 width=8)
         Filter: (val = 1)
(3 rows)

Time: 0.759 ms
test=# explain analyze select * from test where val = 1 order by id limit 10;
                                                        QUERY PLAN                                                         
---------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..4.35 rows=10 width=8) (actual time=0.122..0.170 rows=10 loops=1)
   ->  Index Scan using test_pkey on test  (cost=0.00..343.25 rows=789 width=8) (actual time=0.121..0.165 rows=10 loops=1)
         Filter: (val = 1)
         Rows Removed by Filter: 67
 Total runtime: 0.204 ms
(5 rows)

Time: 1.019 ms

Note that there is a tiny overhead in both commands to actually output the plan.

Usually it happens eg in DB2 that since finding the optimal plan takes a lot of time..therefore the database engine decides to use a suboptimal plan...i think it must be the case with postgres also.

In Postgres, this only occurs if your query is gory enough that it cannot reasonably do an exhaustive search. When you reach the relevant thresholds (which are high, if your use-cases are typical), the planner uses the genetic query optimizer:

http://www.postgresql.org/docs/current/static/geqo-pg-intro.html

If it is then how can i fiddle with postgres such that it chooses the optimal plan.

In more general use cases, there are many things that you can fiddle, but be very wary of messing around with them (apart, perhaps, from collecting a bit more statistics on a select few columns using ALTER TABLE SET STATISTICS ):

http://www.postgresql.org/docs/current/static/runtime-config-query.html

如果您在 psql 客户端中使用 \i 命令而不是在 sql 文件中添加以下行

\timing true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM