PostgreSql的Explain命令详解
		http://toplchx.iteye.com/blog/2091860   使用EXPLAIN   PostgreSQL为每个收到的查询设计一个查询规划。选择正确的匹配查询结构和数据属性的规划对执行效率是至关重要要的,所以系统包含一个复杂的规划器来试图选择好的规划。你可以使用EXPLAIN命令查看查询规划器创建的任何查询。阅读查询规划是一门艺术,需要掌握一定的经验,本节试图涵盖一些基础知识。   以下的例子来自PostgreSQL 9.3开发版。   EXPLAIN基础   查询规划是以规划为节点的树形结构。树的最底节点是扫描节点:他返回表中的原数据行。   不同的表有不同的扫描节点类型:顺序扫描,索引扫描和位图索引扫描。   也有非表列源,如VALUES子句并设置FROM返回,他们有自己的扫描类型。   如果查询需要关联,聚合,排序或其他操作,会在扫描节点之上增加节点执行这些操作。通常有不只一种可能的方式做这些操作,所以可能出现不同的节点类型。   EXPLAIN的输出是每个树节点显示一行,内容是基本节点类型和执行节点的消耗评估。可能会楚翔其他行,从汇总行节点缩进显示节点的其他属性。第一行(最上节点的汇总行)是评估执行计划的总消耗,这个值越小越好。   下面是一个简单的例子:      Sql代码   
   
    
        
            
                
                    
                        
                     
                    
                        
                            扫码关注我们
                         
                        
                            微信号:SRE实战
                         
                        
                            拒绝背锅 运筹帷幄
                         
                     
                 
             
         
     
 				
		
		
		
			
    - EXPLAIN SELECT * FROM tenk1;
 - QUERY PLAN
 - -------------------------------------------------------------
 - Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=244)
 
- 评估开始消耗。这是可以开始输出前的时间,比如排序节点的排序的时间。
 - 评估总消耗。假设查询从执行到结束的时间。有时父节点可能停止这个过程,比如LIMIT子句。
 - 评估查询节点的输出行数,假设该节点执行结束。
 - 评估查询节点的输出行的平均字节数。
 
- EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 7000;
 - QUERY PLAN
 - ------------------------------------------------------------
 - Seq Scan on tenk1 (cost=0.00..483.00 rows=7001 width=244)
 - Filter: (unique1 < 7000)
 
- EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100;
 - QUERY PLAN
 - ------------------------------------------------------------------------------
 - Bitmap Heap Scan on tenk1 (cost=5.07..229.20 rows=101 width=244)
 - Recheck Cond: (unique1 < 100)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0)
 - Index Cond: (unique1 < 100)
 
- EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100 AND stringu1 = 'xxx';
 - QUERY PLAN
 - ------------------------------------------------------------------------------
 - Bitmap Heap Scan on tenk1 (cost=5.04..229.43 rows=1 width=244)
 - Recheck Cond: (unique1 < 100)
 - Filter: (stringu1 = 'xxx'::name)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0)
 - Index Cond: (unique1 < 100)
 
- EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100 AND unique2 > 9000;
 - QUERY PLAN
 - -------------------------------------------------------------------------------------
 - Bitmap Heap Scan on tenk1 (cost=25.08..60.21 rows=10 width=244)
 - Recheck Cond: ((unique1 < 100) AND (unique2 > 9000))
 - -> BitmapAnd (cost=25.08..25.08 rows=10 width=0)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0)
 - Index Cond: (unique1 < 100)
 - -> Bitmap Index Scan on tenk1_unique2 (cost=0.00..19.78 rows=999 width=0)
 - Index Cond: (unique2 > 9000)
 
- EXPLAIN SELECT * FROM tenk1 WHERE unique1 < 100 AND unique2 > 9000 LIMIT 2;
 - QUERY PLAN
 - -------------------------------------------------------------------------------------
 - Limit (cost=0.29..14.48 rows=2 width=244)
 - -> Index Scan using tenk1_unique2 on tenk1 (cost=0.29..71.27 rows=10 width=244)
 - Index Cond: (unique2 > 9000)
 - Filter: (unique1 < 100)
 
- EXPLAIN SELECT *
 - FROM tenk1 t1, tenk2 t2
 - WHERE t1.unique1 < 10 AND t1.unique2 = t2.unique2;
 - QUERY PLAN
 - --------------------------------------------------------------------------------------
 - Nested Loop (cost=4.65..118.62 rows=10 width=488)
 - -> Bitmap Heap Scan on tenk1 t1 (cost=4.36..39.47 rows=10 width=244)
 - Recheck Cond: (unique1 < 10)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..4.36 rows=10 width=0)
 - Index Cond: (unique1 < 10)
 - -> Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.29..7.91 rows=1 width=244)
 - Index Cond: (unique2 = t1.unique2)
 
- EXPLAIN SELECT *
 - FROM tenk1 t1, tenk2 t2
 - WHERE t1.unique1 < 10 AND t2.unique2 < 10 AND t1.hundred < t2.hundred;
 - QUERY PLAN
 - ---------------------------------------------------------------------------------------------
 - Nested Loop (cost=4.65..49.46 rows=33 width=488)
 - Join Filter: (t1.hundred < t2.hundred)
 - -> Bitmap Heap Scan on tenk1 t1 (cost=4.36..39.47 rows=10 width=244)
 - Recheck Cond: (unique1 < 10)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..4.36 rows=10 width=0)
 - Index Cond: (unique1 < 10)
 - -> Materialize (cost=0.29..8.51 rows=10 width=244)
 - -> Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.29..8.46 rows=10 width=244)
 - Index Cond: (unique2 < 10)
 
- EXPLAIN SELECT *
 - FROM tenk1 t1, tenk2 t2
 - WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2;
 - QUERY PLAN
 - ------------------------------------------------------------------------------------------
 - Hash Join (cost=230.47..713.98 rows=101 width=488)
 - Hash Cond: (t2.unique2 = t1.unique2)
 - -> Seq Scan on tenk2 t2 (cost=0.00..445.00 rows=10000 width=244)
 - -> Hash (cost=229.20..229.20 rows=101 width=244)
 - -> Bitmap Heap Scan on tenk1 t1 (cost=5.07..229.20 rows=101 width=244)
 - Recheck Cond: (unique1 < 100)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0)
 - Index Cond: (unique1 < 100)
 
- EXPLAIN SELECT *
 - FROM tenk1 t1, onek t2
 - WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2;
 - QUERY PLAN
 - ------------------------------------------------------------------------------------------
 - Merge Join (cost=198.11..268.19 rows=10 width=488)
 - Merge Cond: (t1.unique2 = t2.unique2)
 - -> Index Scan using tenk1_unique2 on tenk1 t1 (cost=0.29..656.28 rows=101 width=244)
 - Filter: (unique1 < 100)
 - -> Sort (cost=197.83..200.33 rows=1000 width=244)
 - Sort Key: t2.unique2
 - -> Seq Scan on onek t2 (cost=0.00..148.00 rows=1000 width=244)
 
- SET enable_sort = off;
 - EXPLAIN SELECT *
 - FROM tenk1 t1, onek t2
 - WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2;
 - QUERY PLAN
 - ------------------------------------------------------------------------------------------
 - Merge Join (cost=0.56..292.65 rows=10 width=488)
 - Merge Cond: (t1.unique2 = t2.unique2)
 - -> Index Scan using tenk1_unique2 on tenk1 t1 (cost=0.29..656.28 rows=101 width=244)
 - Filter: (unique1 < 100)
 - -> Index Scan using onek_unique2 on onek t2 (cost=0.28..224.79 rows=1000 width=244)
 
- EXPLAIN ANALYZE SELECT *
 - FROM tenk1 t1, tenk2 t2
 - WHERE t1.unique1 < 10 AND t1.unique2 = t2.unique2;
 - QUERY PLAN
 - ---------------------------------------------------------------------------------------------------------------------------------
 - Nested Loop (cost=4.65..118.62 rows=10 width=488) (actual time=0.128..0.377 rows=10 loops=1)
 - -> Bitmap Heap Scan on tenk1 t1 (cost=4.36..39.47 rows=10 width=244) (actual time=0.057..0.121 rows=10 loops=1)
 - Recheck Cond: (unique1 < 10)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..4.36 rows=10 width=0) (actual time=0.024..0.024 rows=10 loops=1)
 - Index Cond: (unique1 < 10)
 - -> Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.29..7.91 rows=1 width=244) (actual time=0.021..0.022 rows=1 loops=10)
 - Index Cond: (unique2 = t1.unique2)
 - Total runtime: 0.501 ms
 
- EXPLAIN ANALYZE SELECT *
 - FROM tenk1 t1, tenk2 t2
 - WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2 ORDER BY t1.fivethous;
 - QUERY PLAN
 - --------------------------------------------------------------------------------------------------------------------------------------------
 - Sort (cost=717.34..717.59 rows=101 width=488) (actual time=7.761..7.774 rows=100 loops=1)
 - Sort Key: t1.fivethous
 - Sort Method: quicksort Memory: 77kB
 - -> Hash Join (cost=230.47..713.98 rows=101 width=488) (actual time=0.711..7.427 rows=100 loops=1)
 - Hash Cond: (t2.unique2 = t1.unique2)
 - -> Seq Scan on tenk2 t2 (cost=0.00..445.00 rows=10000 width=244) (actual time=0.007..2.583 rows=10000 loops=1)
 - -> Hash (cost=229.20..229.20 rows=101 width=244) (actual time=0.659..0.659 rows=100 loops=1)
 - Buckets: 1024 Batches: 1 Memory Usage: 28kB
 - -> Bitmap Heap Scan on tenk1 t1 (cost=5.07..229.20 rows=101 width=244) (actual time=0.080..0.526 rows=100 loops=1)
 - Recheck Cond: (unique1 < 100)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0) (actual time=0.049..0.049 rows=100 loops=1)
 - Index Cond: (unique1 < 100)
 - Total runtime: 8.008 ms
 
- EXPLAIN ANALYZE SELECT * FROM tenk1 WHERE ten < 7;
 - QUERY PLAN
 - ---------------------------------------------------------------------------------------------------------
 - Seq Scan on tenk1 (cost=0.00..483.00 rows=7000 width=244) (actual time=0.016..5.107 rows=7000 loops=1)
 - Filter: (ten < 7)
 - Rows Removed by Filter: 3000
 - Total runtime: 5.905 ms
 
- EXPLAIN ANALYZE SELECT * FROM polygon_tbl WHERE f1 @> polygon '(0.5,2.0)';
 - QUERY PLAN
 - ------------------------------------------------------------------------------------------------------
 - Seq Scan on polygon_tbl (cost=0.00..1.05 rows=1 width=32) (actual time=0.044..0.044 rows=0 loops=1)
 - Filter: (f1 @> '((0.5,2))'::polygon)
 - Rows Removed by Filter: 4
 - Total runtime: 0.083 ms
 
- SET enable_seqscan TO off;
 - EXPLAIN ANALYZE SELECT * FROM polygon_tbl WHERE f1 @> polygon '(0.5,2.0)';
 - QUERY PLAN
 - --------------------------------------------------------------------------------------------------------------------------
 - Index Scan using gpolygonind on polygon_tbl (cost=0.13..8.15 rows=1 width=32) (actual time=0.062..0.062 rows=0 loops=1)
 - Index Cond: (f1 @> '((0.5,2))'::polygon)
 - Rows Removed by Index Recheck: 1
 - Total runtime: 0.144 ms
 
- EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM tenk1 WHERE unique1 < 100 AND unique2 > 9000;
 - QUERY PLAN
 - ---------------------------------------------------------------------------------------------------------------------------------
 - Bitmap Heap Scan on tenk1 (cost=25.08..60.21 rows=10 width=244) (actual time=0.323..0.342 rows=10 loops=1)
 - Recheck Cond: ((unique1 < 100) AND (unique2 > 9000))
 - Buffers: shared hit=15
 - -> BitmapAnd (cost=25.08..25.08 rows=10 width=0) (actual time=0.309..0.309 rows=0 loops=1)
 - Buffers: shared hit=7
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0) (actual time=0.043..0.043 rows=100 loops=1)
 - Index Cond: (unique1 < 100)
 - Buffers: shared hit=2
 - -> Bitmap Index Scan on tenk1_unique2 (cost=0.00..19.78 rows=999 width=0) (actual time=0.227..0.227 rows=999 loops=1)
 - Index Cond: (unique2 > 9000)
 - Buffers: shared hit=5
 - Total runtime: 0.423 ms
 
- BEGIN;
 - EXPLAIN ANALYZE UPDATE tenk1 SET hundred = hundred + 1 WHERE unique1 < 100;
 - QUERY PLAN
 - --------------------------------------------------------------------------------------------------------------------------------
 - Update on tenk1 (cost=5.07..229.46 rows=101 width=250) (actual time=14.628..14.628 rows=0 loops=1)
 - -> Bitmap Heap Scan on tenk1 (cost=5.07..229.46 rows=101 width=250) (actual time=0.101..0.439 rows=100 loops=1)
 - Recheck Cond: (unique1 < 100)
 - -> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0) (actual time=0.043..0.043 rows=100 loops=1)
 - Index Cond: (unique1 < 100)
 - Total runtime: 14.727 ms
 - ROLLBACK;
 
                    更多精彩
		
													
													
													
													
	
		
