python sql语句拼接,利用format // pgsql查看执行效率

select company,prob where big_hy_name = '农业' from t_features

诸如这种形式的要用format的形式拼接,注意format只对{}有用,并且一般zhichuan


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=146938 loops=1)
 Total runtime: 73.379 ms
(3 rows)

详细参考;

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

猜你喜欢

转载自blog.csdn.net/sinat_26566137/article/details/81163190