我正在使用 psql 9.2.3。我有两个非常大的表t1
,并且t2
有很多重复项:
SELECT COUNT(*) FROM t1
= 37,792,210SELECT COUNT(*) FROM t2
= 73,464,030
我现在正在尝试使用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 使用 hashAggDISTINCT
和其他涉及DISTINCT
like的集合运算符EXCEPT
?
解决方案
克雷格的回答效果很好。设置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)