postgres库-----查看sql执行时间

来源:互联网 发布:无尽的传说2 mac 编辑:程序博客网 时间:2024/06/02 00:41

postgres 数据库查看sql的执行时间:

方法一;


\timing on

你的sql语句


不再使用时:

\timing off

方法二: 查看执行计划

explain select count(*) from  你的表;

                                     QUERY PLAN                                     
------------------------------------------------------------------------------------
 Aggregate  (cost=14798.38..14798.39 rows=1 width=0)
   ->  Seq Scan on 你的表  (cost=0.00..
14423.10 rows=150110 width=0)
(2 rows)

cost=说明:

  • 第一个数字0.00表示启动cost,这是执行到返回第一行时需要的cost值。
  • 第二个数字14423.10表示执行整个SQL的cost

explain后加analyze来通过真实执行这个SQL来获得真实的执行计划和执行时间

EXPLAIN ANALYZE select count(*) from 你的表;
                                                             QUERY PLAN                                                             
------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=14798.38..14798.39 rows=1 width=0) (actual time=73.336..73.336 rows=1 loops=1)
   ->  Seq Scan on cancellations_changes  (cost=0.00..14423.10 rows=150110 width=0) (actual time=0.009..47.861 rows=146938loops=1)
 Total runtime: 73.379 ms
(3 rows)

详细参考;

http://blog.csdn.net/ls3648098/article/details/7602136

0 0
原创粉丝点击