





(1, 5)(1, 2)(2, 9)(2, 1)(2, 5)(3, 3)(3, 7)
(1, 5) (1, 2) (2, 9) (2, 1) (2, 5) (3, 3) (3, 7)===================== (1, 2) (1, 5) (2, 1) (2, 5) (2, 9) (3, 3) (3, 7)postgres=# create table t (a int, b int, c int);CREATE TABLE postgres=# insert into t select mod(i,10),mod(i,10),i from generate_series(1,10000) s(i);INSERT 10000 postgres=# create index on t (a);CREATE INDEX postgres=# analyze t;ANALYZE postgres=# set enable_incrementalsort = off;SET postgres=# explain analyze select a,b,sum(c) from t group by 1,2 order by 1,2,3 limit 1;QUERY PLAN------------------------------------------------------------------------------------------------------------------------Limit (cost=231.50..231.50 rows=1 width=16) (actual time=2.814..2.815 rows=1 loops=1) -> Sort (cost=231.50..231.75 rows=100 width=16) (actual time=2.813..2.813 rows=1 loops=1)Sort Key: a, b, (sum(c))Sort Method: top-N heapsort Memory: 25kB -> HashAggregate (cost=230.00..231.00 rows=100 width=16) (actual time=2.801..2.804 rows=10 loops=1)Group Key: a, b Peak Memory Usage: 37 kB -> Seq Scan on t (cost=0.00..155.00 rows=10000 width=12) (actual time=0.012..0.951 rows=10000 loops=1) Planning Time: .169 ms Execution Time: 2.858 ms (10 rows)
postgres=# set enable_incrementalsort = on;SET postgres=# explain analyze select a,b,sum(c) from t group by 1,2 order by 1,2,3 limit 1;QUERY PLAN---------------------------------------------------------------------------------------------------------------------------------------------Limit (cost=133.63..146.52 rows=1 width=16) (actual time=1.177..1.177 rows=1 loops=1) -> Incremental Sort (cost=133.63..1422.16 rows=100 width=16) (actual time=1.176..1.176 rows=1 loops=1)Sort Key: a, b, (sum(c)) Presorted Key: a, bFull-sort Groups: 1 Sort Method: quicksort Average Memory: 25kB Peak Memory: 25kB -> GroupAggregate (cost=120.65..1417.66 rows=100 width=16) (actual time=0.746..1.158 rows=2 loops=1)Group Key: a, b -> Incremental Sort (cost=120.65..1341.66 rows=10000 width=12) (actual time=0.329..0.944 rows=2001 loops=1)Sort Key: a, b Presorted Key: aFull-sort Groups: 3 Sort Method: quicksort Average Memory: 28kB Peak Memory: 28kB Pre-sorted Groups: 3 Sort Method: quicksort Average Memory: 71kB Peak Memory: 71kB -> Index Scan using t_a_idx on t (cost=0.29..412.65 rows=10000 width=12) (actual time=0.011..0.504 rows=3001 loops=1) Planning Time: .164 ms Execution Time: 1.205 ms (15 rows)=================================PG 13 parallel vacuum=============================== postgres=# create table testva(id int,info text);CREATE TABLETime: 2.334 ms postgres=# insert into testva select generate_series(1,1000000),md5(random()::text);INSERT 1000000Time: 1448.098 ms (00:01.448) postgres=# create index idx_testva on testva(id);CREATE INDEXTime: 364.988 ms postgres=# create index idx_testva_info on testva(info);CREATE INDEXTime: 873.416 ms postgres=# vacuum (parallel 4) testva; VACUUMTime: 114.846 ms =================================PG 12 normal vacuum=============================== postgres=# create table testva(id int,info text);CREATE TABLETime: 5.817 ms postgres=# insert into testva select generate_series(1,1000000),md5(random()::text);INSERT 1000000Time: 3023.958 ms (00:03.024) postgres=# create index idx_testva on testva(id);CREATE INDEXTime: 631.632 ms postgres=# create index idx_testva_info on testva(info);CREATE INDEXTime: 1374.849 ms (00:01.375) postgres=# vacuum testva; VACUUMTime: 216.944 ms来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69917105/viewspace-3133542/ ,如需转载,请注明出处,否则将追究法律责任。