설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 94건, 최근 0 건
   

SQL 실행계획 살펴보기

글쓴이 : PostgresDBA 날짜 : 2012-12-18 (화) 09:18 조회 : 17588
오라클에서는 explain plan for 구문으로 예상 실행계획을 살펴봅니다.
PostgreSQL 에서는 어떻게 볼까요?

EXPLAIN 과 EXPLAIN ANALYZE 가 있습니다.

EXPLAIN 은 단순히 예상실행계획이며, SQL 을 수행하지 않습니다.
그러나, EXPLAIN ANALYZE 는 SQL 을 실제 수행합니다.

실 예를 살펴보시죠.

scott@cloud-00:5432 scottdb#SQL> explain select * from emp;
                      QUERY PLAN                       
-------------------------------------------------------
 Seq Scan on emp  (cost=0.00..16.30 rows=630 width=98)
(1 row)

Time: 0.372 ms
scott@cloud-00:5432 scottdb#SQL> explain analyze select * from emp;
                                            QUERY PLAN                                            
--------------------------------------------------------------------------------------------------
 Seq Scan on emp  (cost=0.00..16.30 rows=630 width=98) (actual time=0.006..0.008 rows=14 loops=1)
 Total runtime: 0.023 ms
(2 rows)

Time: 0.355 ms
scott@cloud-00:5432 scottdb#SQL> explain select * from emp limit 1;
                         QUERY PLAN                          
-------------------------------------------------------------
 Limit  (cost=0.00..0.03 rows=1 width=98)
   ->  Seq Scan on emp  (cost=0.00..16.30 rows=630 width=98)
(2 rows)

Time: 0.360 ms
scott@cloud-00:5432 scottdb#SQL> explain analyze select * from emp limit 1;
                                              QUERY PLAN                                               
-------------------------------------------------------------------------------------------------------
 Limit  (cost=0.00..0.03 rows=1 width=98) (actual time=0.006..0.006 rows=1 loops=1)
   ->  Seq Scan on emp  (cost=0.00..16.30 rows=630 width=98) (actual time=0.005..0.005 rows=1 loops=1)
 Total runtime: 0.022 ms
(3 rows)

Time: 0.394 ms
scott@cloud-00:5432 scottdb#SQL> 

아래와 같은 포맷의 출력도 가능합니다. (PostgreSQL version >= 9.0)

scott@pg-00:5432:scottdb] 
SQL> explain (format yaml) select * from dept;
        QUERY PLAN         
---------------------------
 - Plan:                  +
     Node Type: "Seq Scan"+
     Relation Name: "dept"+
     Alias: "dept"        +
     Startup Cost: 0.00   +
     Total Cost: 16.50    +
     Plan Rows: 650       +
     Plan Width: 94
(1 row)

Time: 6.923 ms
scott@pg-00:5432:scottdb] 
SQL> explain (format xml) select * from dept;
                        QUERY PLAN                        
----------------------------------------------------------
 <explain xmlns="http://www.postgresql.org/2009/explain">+
   <Query>                                               +
     <Plan>                                              +
       <Node-Type>Seq Scan</Node-Type>                   +
       <Relation-Name>dept</Relation-Name>               +
       <Alias>dept</Alias>                               +
       <Startup-Cost>0.00</Startup-Cost>                 +
       <Total-Cost>16.50</Total-Cost>                    +
       <Plan-Rows>650</Plan-Rows>                        +
       <Plan-Width>94</Plan-Width>                       +
     </Plan>                                             +
   </Query>                                              +
 </explain>
(1 row)

Time: 3.218 ms
scott@pg-00:5432:scottdb] 
SQL> explain (format json) select * from dept;
           QUERY PLAN           
--------------------------------
 [                             +
   {                           +
     "Plan": {                 +
       "Node Type": "Seq Scan",+
       "Relation Name": "dept",+
       "Alias": "dept",        +
       "Startup Cost": 0.00,   +
       "Total Cost": 16.50,    +
       "Plan Rows": 650,       +
       "Plan Width": 94        +
     }                         +
   }                           +
 ]
(1 row)

Time: 1.185 ms
scott@pg-00:5432:scottdb] 

PostgresDBA 2013-08-30 (금) 09:23
explain verbose select * from dept;  -- verbose 옵션도 있답니다.
---------------------------------------------------------------------------------------------

"Seq Scan on public.dept  (cost=0.00..1.04 rows=4 width=94)"
"  Output: deptno, dname, loc"
댓글주소
   

postgresdba.com