2017-04-09 7 views
1

UNION 검색어가 하나 있습니다. EXPLAIN 출력에서, UNION은 항상 추가 후에 정렬을 수행한다는 것을 알았습니다. 그래서 나는 그것을 UNION ALL으로 바 꾸었습니다.Postgresql : 테이블을 만들 때 UNION ALL이 UNION보다 느립니다?

쿼리가 더 빨라야한다고 생각하십니까? 그러나 실제로 실행하면 UNION ALL으로 쿼리는 항상 초 후에 반환됩니다. (어쩌면 내 데이터 세트가 충분하지 않을 수도 있고, 그 차이가 미미한 것처럼 보일 수도 있지만, 나는 여전히 나에게 이해가 가지 않는다.) 나는 무엇이든지 놓쳤는가?

QUERY

WITH wcal AS (...), wlog AS (...) 
SELECT * INTO temp.utlog FROM (
    SELECT * FROM wcal 
    UNION SELECT * FROM wlog 
) as t2; 

는 연합

QUERY PLAN 
Unique (cost=691313.04..737080.66 rows=3051175 width=80) 
    CTE wcal 
    CTE wlog 
    -> Sort (cost=691313.04..698940.98 rows=3051175 width=80) 
     Sort Key: wcal.wts, wcal.wdate, wcal.wstate, wcal.wln, wcal.wid 
     -> Append (cost=0.00..91535.25 rows=3051175 width=80) 
       -> CTE Scan on wcal (cost=0.00..52600.00 rows=2630000 width=60) 
       -> Subquery Scan on "*SELECT* 2" (cost=0.00..12635.25 rows=421175 width=64) 
        -> CTE Scan on wlog (cost=0.00..8423.50 rows=421175 width=64) 

UNION 모든

QUERY PLAN 
Append (cost=0.00..91535.25 rows=3051175 width=80) 
    CTE wcal ... 
    CTE wlog ... 
    -> CTE Scan on wcal (cost=0.00..52600.00 rows=2630000 width=60) 
    -> Subquery Scan on "*SELECT* 2" (cost=0.00..12635.25 rows=421175 width=64) 
     -> CTE Scan on wlog (cost=0.00..8423.50 rows=421175 width=64) 

실제 결과를 설명 EXPLAIN; UNION ALL

Table temp.utlog1 dropped 

Execution time: 0.02s 
Statement 1 of 5 finished 

0 rows affected 
WITH executed successfully 

Execution time: 6.81s 
Statement 2 of 5 finished 
새 테이블에

결과 카운트가 ... 동일

SELECT count(*) FROM temp.utlog1 
UNION ALL 
SELECT count(*) FROM temp.utlog 

count 
364414 
364414 
UNION

Table temp.utlog dropped 

Execution time: 0.02s 
Statement 1 of 5 finished 

0 rows affected 
WITH executed successfully 

Execution time: 6.05s 
Statement 2 of 5 finished 

QUERY PLAN Unique (cost=65006.04..70835.22 rows=388612 width=80) (actual time=4259.243..4534.226 rows=364414 loops=1) CTE wcal ... CTE wlog ... -> Sort (cost=65006.04..65977.57 rows=388612 width=80) (actual time=4259.239..4370.691 rows=364414 loops=1) Sort Key: wcal.wts, wcal.wdate, wcal.wstate, wcal.wln, wcal.wid Sort Method: external sort Disk: 11832kB -> Append (cost=0.00..11658.36 rows=388612 width=80) (actual time=0.677..3486.529 rows=364414 loops=1) -> CTE Scan on wcal (cost=0.00..720.00 rows=36000 width=60) (actual time=0.676..120.320 rows=33624 loops=1) -> Subquery Scan on "*SELECT* 2" (cost=0.00..10578.36 rows=352612 width=64) (actual time=2760.279..3309.084 rows=330790 loops=1) -> CTE Scan on wlog (cost=0.00..7052.24 rows=352612 width=64) (actual time=2760.275..3189.444 rows=330790 loops=1) Planning time: 1.500 ms Execution time: 7577.380 ms ... Planning time: 1.375 ms Execution time: 6777.699 ms ... Planning time: 1.375 ms Execution time: 6777.699 ms ... Planning time: 1.340 ms Execution time: 6964.954 ms 

UNION

EXPLAIN ANALYZEUNION

QUERY PLAN 
Append (cost=0.00..11658.36 rows=388612 width=80) (actual time=0.670..3382.220 rows=364414 loops=1) 
    CTE wcal ... 
    CTE wlog ... 
    -> CTE Scan on wcal (cost=0.00..720.00 rows=36000 width=60) (actual time=0.668..140.251 rows=33624 loops=1) 
    -> Subquery Scan on "*SELECT* 2" (cost=0.00..10578.36 rows=352612 width=64) (actual time=2635.901..3179.006 rows=330790 loops=1) 
     -> CTE Scan on wlog (cost=0.00..7052.24 rows=352612 width=64) (actual time=2635.897..3043.421 rows=330790 loops=1) 
Planning time: 1.759 ms 
Execution time: 6570.793 ms 
... 
Planning time: 1.713 ms 
Execution time: 5883.275 ms 
... 
Planning time: 1.351 ms 
Execution time: 7092.713 ms 
... 
Planning time: 1.318 ms 
Execution time: 5803.567 ms 

EXPLAIN ANALYZEUNION ALL (0._s) 및 UNION ALL (4._s)없이 SELECT INTO

WITH executed successfully 

Execution time: 0.41s 
Statement 1 of 4 finished 

WITH executed successfully 

Execution time: 0.28s 
Statement 1 of 4 finished 

WITH executed successfully 

Execution time: 0.33s 
Statement 1 of 4 finished 

WITH executed successfully 

Execution time: 0.25s 
Statement 1 of 4 finished 

WITH executed successfully 

Execution time: 0.28s 
Statement 1 of 5 finished 

WITH executed successfully 

Execution time: 4.67s 
Statement 1 of 5 finished 

WITH executed successfully 

Execution time: 4.17s 
Statement 1 of 5 finished 

WITH executed successfully 

Execution time: 4.17s 
Statement 1 of 5 finished 

WITH executed successfully 

Execution time: 4.33s 
Statement 1 of 5 finished 

UNION없이 CTE

QUERY PLAN 
Unique (cost=699529.52..752925.08 rows=3051175 width=65) (actual time=3041.962..3488.246 rows=364414 loops=1) 
    -> Sort (cost=699529.52..707157.46 rows=3051175 width=65) (actual time=3041.958..3296.505 rows=364414 loops=1) 
     Sort Key: s1.s1, ((date_trunc('day'::text, s1.s1))::date), (NULL::character varying(3)), s2.wline, (NULL::boolean), (NULL::integer) 
     Sort Method: external merge Disk: 12544kB 
     -> Append (cost=0.00..120611.73 rows=3051175 width=65) (actual time=0.729..2218.973 rows=364414 loops=1) 
       -> Nested Loop (cost=0.00..46077.88 rows=2630000 width=36) (actual time=0.727..94.359 rows=33624 loops=1) 
       ... 
       -> WindowAgg (cost=198.08..44022.11 rows=421175 width=24) (actual time=0.737..2049.979 rows=330790 loops=1) 
       ... 

Planning time: 1.318 ms 
Execution time: 6438.852 ms 

UNIONCTE

QUERY PLAN 
Unique (cost=670453.04..723848.60 rows=3051175 width=65) (actual time=3726.279..3973.870 rows=364414 loops=1) 
    CTE wcal 
    -> Nested Loop (cost=0.00..46077.88 rows=2630000 width=36) (actual time=1.788..82.337 rows=33624 loops=1) 
    ... 
    CTE wlog 
    -> Sort (cost=92006.40..93059.34 rows=421175 width=24) (actual time=2542.472..2743.255 rows=330790 loops=1) 
      Sort Key: tbl_line_evts.evt_time 
      Sort Method: external merge Disk: 11712kB 
      -> WindowAgg (cost=198.08..44022.11 rows=421175 width=24) (actual time=0.932..1982.497 rows=330790 loops=1) 
      ... 
    -> Sort (cost=670453.04..678080.98 rows=3051175 width=65) (actual time=3726.275..3823.236 rows=364414 loops=1) 
     Sort Key: wcal.wts, wcal.wdate, wcal.wstate, wcal.wln, wcal.wdirty, wcal.wid 
     Sort Method: external sort Disk: 12584kB 
     -> Append (cost=0.00..91535.25 rows=3051175 width=65) (actual time=1.800..3082.954 rows=364414 loops=1) 
       -> CTE Scan on wcal (cost=0.00..52600.00 rows=2630000 width=57) (actual time=1.798..125.699 rows=33624 loops=1) 
       -> CTE Scan on wlog (cost=0.00..8423.50 rows=421175 width=49) (actual time=2542.476..2915.524 rows=330790 loops=1) 
Planning time: 2.381 ms 
Execution time: 5950.324 ms 

0,123,945와

QUERY PLAN 
Sort (cost=721442.46..729070.40 rows=3051175 width=80) (actual time=3416.176..3627.253 rows=364414 loops=1) 
    Sort Key: s1.s1 
    Sort Method: external merge Disk: 11816kB 
    -> Append (cost=0.00..121664.67 rows=3051175 width=80) (actual time=0.675..2726.980 rows=364414 loops=1) 
     -> Nested Loop (cost=0.00..46077.88 rows=2630000 width=39) (actual time=0.674..118.694 rows=33624 loops=1) 
     ... 
     -> Subquery Scan on "*SELECT* 2" (cost=198.08..49286.79 rows=421175 width=51) (actual time=1.223..2517.292 rows=330790 loops=1) 
       -> WindowAgg (cost=198.08..45075.04 rows=421175 width=51) (actual time=1.218..2334.702 rows=330790 loops=1) 
       ... 
Planning time: 1.360 ms 
Execution time: 5795.449 ms 

UNION ALL 당신의 가정이 화목 CTE

QUERY PLAN 
Append (cost=0.00..91535.25 rows=3051175 width=80) (actual time=0.718..3595.377 rows=364414 loops=1) 
    CTE wcal 
    -> Nested Loop (cost=0.00..46077.88 rows=2630000 width=39) (actual time=0.709..86.700 rows=33624 loops=1) 
    ... 
    CTE wlog 
    -> Sort (cost=98820.34..99873.28 rows=421175 width=51) (actual time=2818.220..3049.432 rows=330790 loops=1) 
      Sort Key: tbl_line_evts.evt_time 
      Sort Method: external merge Disk: 10952kB 
      -> WindowAgg (cost=198.08..45075.04 rows=421175 width=51) (actual time=0.884..2224.477 rows=330790 loops=1) 
      ... 
    -> CTE Scan on wcal (cost=0.00..52600.00 rows=2630000 width=60) (actual time=0.716..129.933 rows=33624 loops=1) 
    -> Subquery Scan on "*SELECT* 2" (cost=0.00..12635.25 rows=421175 width=64) (actual time=2818.229..3400.468 rows=330790 loops=1) 
     -> CTE Scan on wlog (cost=0.00..8423.50 rows=421175 width=64) (actual time=2818.224..3250.670 rows=330790 loops=1) 
Planning time: 2.415 ms 
Execution time: 6223.041 ms 
+0

'UNION'은 반환되는 세트의 크기를 줄입니까? 중복 된 부분이 있습니까? 또한 순서는 INSERT 중에 인덱스 생성에 영향을 줄 수 있습니다. – jcaron

+0

부정, 그들은 동일한 수의 기록을 산출했다. 인덱스에 대해 자세히 설명해 주시겠습니까? – Ben

+0

인덱스에 데이터를 삽입 할 때 I/O로 인해 인덱스가 다른 순서가 아닌 인덱스 순서로 삽입하는 것이 일반적으로 더 빠릅니다 (특히 회전 디스크 (SSD와 반대)). Ines가 UNION이고 UNION ALL 인 쿼리를 삽입하지 않은 경우? – jcaron

답변

0

CTE없이 docs

UNION ... 과 같은 방법으로 그 결과에서 중복 행을 제거 UNION ALL이 사용되지 않는 한 DISTINCT . 당신이 두 쿼리의 비용을 체크하면

(...)는 내

, 당신은 UNION ALL이 훨씬 적은 비용 것을 찾을 수 있습니다. 91K 대 737K. 이제는 실제 결과와의 차이점을 설명하기 위해 IIO 캐시 및 쓰기 작업의 결과로 표를 만들지 않고 시간을 측정 해 보았습니다. 단지 실행중인 설명을 UNION으로 실행 한 다음 UNION ALL으로 10 번 분석하면됩니다. 덜 놀랄만 한 일 이군.

+0

결과를 업데이트했습니다. 당신은 테이블 작성없이 바로, 차이점은 ANALYZE로 중요합니다. 결과는 불안정합니다. 그러나 ANALYZE없이 유선 결과는 여전히 반복 가능합니다. 그렇다면 무엇을 배워야합니까? 무거운 테이블을 만들 때'UNION'을'UNION ALL'보다 선호해야합니까? – Ben

+0

'explain explain'은 실제로 쿼리를 수행하는 반면'explain analyze'는 실제로 쿼리를 수행합니다. 'DISTINCT'가없는 빠른 조합을 원할 때는'union all'을 사용해야하고, 훨씬 느리지 만 별개의 행만 원한다면'union'을 사용해야합니다. –

+0

thanks. 나는 그것을 얻는다라고 생각한다.내 경우에는'union all' 결과가'union'보다 14 배 빠르며, 캐시에 비효율이 있으면 1s 차이가별로 나쁘지 않습니다. 'analyze'는 더 많은 지연/중단을 가져 왔고, 어쩌면 강도를 완화 시켰기 때문에, 결과는 불안정하지만 맨 처음 쿼리보다 훨씬 좋아 보입니다. – Ben