我正在使用psql 9.2.3。我有两个非常大的表t1t2有许多副本:
SELECT COUNT(*) FROM t1=37792210
SELECT COUNT(*) FROM t2=73464030
我现在尝试使用UNION合并和消除这些表的重复数据。根据SQL UNION Question,Postgresql应该尝试使用HashAgg以获得最佳性能。但是,当我在表上尝试相同的查询EXPLAIN SELECT * FROM t1 UNION SELECT * FROM t2时,Postgres总是尝试基于排序的算法:

                                    QUERY PLAN
----------------------------------------------------------------------------------
 Unique  (cost=29210659.24..30879489.76 rows=111255368 width=20)
   ->  Sort  (cost=29210659.24..29488797.66 rows=111255368 width=20)
         Sort Key: t1.col1, t1.col2, t1.col3, t1.col4, t1.col5
         ->  Append  (cost=0.00..2933746.36 rows=111255368 width=20)
               ->  Seq Scan on t1  (cost=0.00..618633.96 rows=37791896 width=20)
               ->  Seq Scan on t2  (cost=0.00..1202558.72 rows=73463472 width=20)

生成速度慢得让人无法忍受的查询计划。我已将enable_hashagg参数设置为ON
db=# SET enable_hashagg=ON;
SET

如何强制PostgreSQL使用hashAgg算法?
另外,有没有办法告诉PostgreSQL对DISTINCT和其他涉及DISTINCT的set操作符(如EXCEPT)使用hashAgg?
解决方案
克雷格的回答很有效。设置work_mem参数后,PostgreSQL生成所需的计划:
EXPLAIN ANALYZE
  SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t3 UNION
  SELECT * FROM t4 UNION SELECT * FROM t5 UNION SELECT * FROM t6;
                                                            QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
 HashAggregate  (cost=7205428.09..9062949.04 rows=185752095 width=20) (actual time=324868.512..327072.616 rows=3379701 loops=1)
   ->  Append  (cost=0.00..4883526.90 rows=185752095 width=20) (actual time=13.537..175073.860 rows=183451688 loops=1)
         ->  Seq Scan on t1  (cost=0.00..25912.73 rows=1582973 width=20) (actual time=13.536..554.381 rows=1582973 loops=1)
         ->  Seq Scan on t2  (cost=0.00..9204.29 rows=562229 width=20) (actual time=0.293..128.485 rows=562229 loops=1)
         ->  Seq Scan on t3  (cost=0.00..618633.96 rows=37791896 width=20) (actual time=20.919..41062.926 rows=37792210 loops=1)
         ->  Seq Scan on t4  (cost=0.00..1202558.72 rows=73463472 width=20) (actual time=26.689..50081.367 rows=73464030 loops=1)
         ->  Seq Scan on t5  (cost=0.00..161043.91 rows=9838091 width=20) (actual time=9.930..4548.116 rows=9837957 loops=1)
         ->  Seq Scan on t6  (cost=0.00..1008652.34 rows=62513434 width=20) (actual time=38.144..52663.002 rows=60212289 loops=1)
 Total runtime: 328914.575 ms (5.5 min)

鉴于
SET enable_hashagg=OFF;
SET
EXPLAIN ANALYZE
  SELECT * FROM t1 UNION SELECT * FROM t2 UNION SELECT * FROM t3 UNION
  SELECT * FROM t4 UNION SELECT * FROM t5 UNION SELECT * FROM t6;

                                                               QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=33779086.16..36530850.99 rows=183450989 width=20) (actual time=1372760.630..1569685.807 rows=3379701 loops=1)
   ->  Sort  (cost=33779086.16..34237713.63 rows=183450989 width=20) (actual time=1372760.628..1528232.239 rows=183451688 loops=1)
         Sort Key: t1.col1, t1.col2, t1.col3, t1.col4, t1.col5
         Sort Method: external merge  Disk: 5379792kB
         ->  Append  (cost=0.00..4837504.78 rows=183450989 width=20) (actual time=14.780..98779.365 rows=183451688 loops=1)
               ->  Seq Scan on t1  (cost=0.00..25912.73 rows=1582973 width=20) (actual time=14.777..389.774 rows=1582973 loops=1)
               ->  Seq Scan on t2  (cost=0.00..9204.29 rows=562229 width=20) (actual time=4.994..137.951 rows=562229 loops=1)
               ->  Seq Scan on t3  (cost=0.00..618633.96 rows=37791896 width=20) (actual time=12.028..16287.735 rows=37792210 loops=1)
               ->  Seq Scan on t4  (cost=0.00..1202558.72 rows=73463472 width=20) (actual time=51.791..33784.820 rows=73464030 loops=1)
               ->  Seq Scan on t5  (cost=0.00..161043.91 rows=9838091 width=20) (actual time=26.564..3206.521 rows=9837957 loops=1)
               ->  Seq Scan on t6  (cost=0.00..985641.28 rows=60212328 width=20) (actual time=22.941..20808.849 rows=60212289 loops=1)
 Total runtime: 1570609.508 ms (26.2 min)

最佳答案

SET enable_hashagg = on无效;这是默认值。这些参数用于强制PostgreSQL将给定方法设置为off,作为最后手段使用该方法,它们实际上是用于测试和开发的。
显著提高work_mem可能会鼓励选择hashagg计划。您可能还想修改random_page_costseq_page_cost,并确保effective_cache_size反映系统的可用内存。

关于sql - PostgreSQL不使用HashAgg进行UNION查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17334100/

10-12 05:54