Postgresql:创建表时,UNION ALL比UNION慢?

Postgresql:创建表时,UNION ALL比UNION慢?,postgresql,union,common-table-expression,Postgresql,Union,Common Table Expression,我有一个UNION查询。从EXPLAIN输出中,我注意到UNION总是在append之后进行排序。所以我把它改成了UNION ALL 假设查询速度应该更快? 但是实际执行表明,使用UNION ALL查询总是在几秒钟后返回。(也许我的数据集不够大,差异看起来很小,但我还是没有意识到。)我遗漏了什么吗 质疑 解释联盟 QUERY PLAN Unique (cost=691313.04..737080.66 rows=3051175 width=80) CTE wcal CTE wlog

我有一个
UNION
查询。从EXPLAIN输出中,我注意到UNION总是在append之后进行排序。所以我把它改成了
UNION ALL

假设查询速度应该更快? 但是实际执行表明,使用
UNION ALL
查询总是在几秒钟后返回。(也许我的数据集不够大,差异看起来很小,但我还是没有意识到。)我遗漏了什么吗

质疑

解释联盟

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)
向所有人解释

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)
实际效果

联合

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
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
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
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
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
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
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
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
联合所有

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
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
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
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
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
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
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
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
新表中的结果计数相同…

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

count
364414
364414
解释分析
联合所有

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
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
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
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
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
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
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
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
解释分析
联合

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
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
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
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
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
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
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
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
UNION
(0.\s)和
UNION ALL
(4.\s)不带
选择进入

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
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
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
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
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
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
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
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
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
UNION
CTE

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
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
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
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
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
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
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
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
UNION ALL
不带
CTE

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
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
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
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
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
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
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
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
UNION ALL
CTE

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
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
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
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
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
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
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
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

你的假设符合

联合…从其结果中消除重复行,方法与 除非使用UNION ALL,否则为DISTINCT

(……)矿山


如果检查两个查询的成本,您会发现联合所有查询的成本都要低得多。91K对737K。现在,为了解释与实际结果的差异,我假设这是io缓存和写操作的结果,尝试在不创建表的情况下测量时间,但只使用
UNION
运行10次解释分析,然后使用
UNION ALL
运行10次。应该不那么令人惊讶。

你的假设符合

联合…从其结果中消除重复行,方法与 除非使用UNION ALL,否则为DISTINCT

(……)矿山


如果检查两个查询的成本,您会发现联合所有查询的成本都要低得多。91K对737K。现在,为了解释与实际结果的差异,我假设这是io缓存和写操作的结果,尝试在不创建表的情况下测量时间,但只使用
UNION
运行10次解释分析,然后使用
UNION ALL
运行10次。应该不那么令人惊讶。

联合是否减少了返回集的大小?也就是说,有重复的吗?此外,在插入过程中,顺序可能会对索引构造产生影响。相反,它们生成的记录数相同。关于索引,您能详细说明一下吗?在将数据插入索引时,由于I/O的原因,尤其是在旋转磁盘(与SSD相反)上,如果您按照索引的顺序插入数据,而不是按照不同的顺序或随机顺序插入数据,通常会更快。在没有插入的查询时间内,哪些INE是UNION,哪些是UNION ALL?如果可能,尝试用子查询替换CTE。这将给计划员更多的自由。记录
UNION
是否减少了返回集的大小?也就是说,有重复的吗?此外,在插入过程中,顺序可能会对索引构造产生影响。相反,它们生成的记录数相同。关于索引,您能详细说明一下吗?在将数据插入索引时,由于I/O的原因,尤其是在旋转磁盘(与SSD相反)上,如果您按照索引的顺序插入数据,而不是按照不同的顺序或随机顺序插入数据,通常会更快。在没有插入的查询时间中,哪些INE是UNION,哪些是UNION ALL?如果可能,尝试用子查询替换CTE。这将给计划员更多的自由。记录我更新了结果。如果不创建表,您是对的,差异是显著的,使用ANALYZE,结果是不稳定的。但如果不进行分析,有线结果仍然是可重复的。那么我应该学什么呢?在创建io繁重的表时,我是否更喜欢使用
UNION
而不是
UNION ALL
explain analyze
实际执行查询,而explain
仅对其进行估计。如果您希望快速联合而不使用
DISTINCT
,则应使用
union all
;如果您希望速度慢得多但只使用DISTINCT行,则应使用
union
。谢谢。我想我明白了。在我的例子中,
union all
返回结果的速度比
union
快14倍。如果缓存中存在任何低效,1s的差异实际上并没有那么大<代码>分析
引入了更多的延迟/中断,可能减轻了强度,因此结果比裸查询好很多倍,尽管它不稳定。我更新了结果。如果不创建表,您是对的,差异是显著的,使用ANALYZE,结果是不稳定的。但如果不进行分析,有线结果仍然是可重复的。那么我应该学什么呢?在创建io繁重的表时,我是否更喜欢使用
UNION
而不是
UNION ALL
explain analyze
实际执行查询,而explain仅对其进行估计。如果您希望快速联合而不使用
DISTINCT
,则应使用
union all
;如果您希望速度慢得多但只使用DISTINCT行,则应使用
union
。谢谢。我想我明白了。在我的例子中,
union all
返回结果的速度比
union
快14倍。如果缓存中存在任何低效,1s的差异实际上并没有那么大
analyze
引入了更多的延迟/中断,可能减轻了强度,因此结果比裸查询好很多倍,尽管它不稳定。