作者
Anthony Sotolongo
译者简介
崔鹏
PostgreSQL爱好者
校对者简介
吴伟略
CET中电技术 研发工程师
PostgreSQL爱好者
引言
< 8MB (3^0 * 8) -> 0 workers 表大小小于3MB时,启动0个workers< 24MB (3^1 * 8) -> 1 workers< 72MB (3^2 * 8) -> 2 workers< 216MB (3^3 * 8) -> 3 workers...
CREATE TABLE tab1 (i int, j text);ALTER TABLE tab1 ALTER COLUMN j SET STORAGE EXTERNAL; -- to force to use store in toast without compressionINSERT INTO tab1 SELECT i, repeat('textvalue ',500) from generate_series (1,900000 ) AS i;ANALYZE tab1;postgres=# \dt+ tab1List of relationsSchema | Name | Type | Owner | Persistence | Access method | Size | Description--------+------+-------+----------+-------------+---------------+---------+-------------public | tab1 | table | postgres | permanent | heap | 5126 MB |(1 row)
postgres=# EXPLAIN ANALYZE select i, j from tab1 where i%2=0;QUERY PLAN---------------------------------------------------------------------------------------------------------------------------Gather (cost=1000.00..12808.00 rows=4500 width=22) (actual time=0.338..54.463 rows=450000 loops=1)Workers Planned: 2Workers Launched: 2-> Parallel Seq Scan on tab1 (cost=0.00..11358.00 rows=1875 width=22) (actual time=0.038..23.329 rows=150000 loops=3)Filter: ((i % 2) = 0)Rows Removed by Filter: 150000Planning Time: 0.093 msExecution Time: 65.269 ms(8 rows)
postgres=# set max_parallel_workers_per_gather = 3;SETpostgres=# EXPLAIN ANALYZE select i, j from tab1 where i%2=0;QUERY PLAN---------------------------------------------------------------------------------------------------------------------------Gather (cost=1000.00..12808.00 rows=4500 width=22) (actual time=0.363..59.643 rows=450000 loops=1)Workers Planned: 2Workers Launched: 2-> Parallel Seq Scan on tab1 (cost=0.00..11358.00 rows=1875 width=22) (actual time=0.041..24.766 rows=150000 loops=3)Filter: ((i % 2) = 0)Rows Removed by Filter: 150000Planning Time: 0.070 msExecution Time: 71.027 ms(8 rows)
postgres=# SELECTformat($$Normal table %s:oid: %ssize: %sToast table %s:oid: %ssize: %s$$,relname,oid,pg_size_pretty(pg_relation_size(oid)),c.reltoastrelid::regclass,c.reltoastrelid,pg_size_pretty(pg_relation_size(c.reltoastrelid))) as sizesFROM pg_class as cWHERE relname = 'tab1';sizes----------------------------------------------+Normal table tab1: +oid: 2717534 +size: 45 MB +Toast table pg_toast.pg_toast_2717534:+oid: 2717537 +size: 5022 MB +(1 row)
postgres=# set min_parallel_table_scan_size = '4MB';-- this will force to use 3 workersSETpostgres=# EXPLAIN ANALYZE select i, j from tab1 where i%2=0;QUERY PLAN---------------------------------------------------------------------------------------------------------------------------Gather (cost=1000.00..11537.84 rows=4500 width=22) (actual time=0.186..38.858 rows=450000 loops=1)Workers Planned: 3Workers Launched: 3-> Parallel Seq Scan on tab1 (cost=0.00..10087.84 rows=1452 width=22) (actual time=0.021..19.218 rows=112500 loops=4)Filter: ((i % 2) = 0)Rows Removed by Filter: 112500Planning Time: 0.035 msExecution Time: 49.475 ms(8 rows)
postgres=# ALTER TABLE tab1 SET (parallel_workers = 4);postgres=# EXPLAIN ANALYZE select i, j from tab1 where i%2=0;QUERY PLAN-------------------------------------------------------------------------------------------------------------------------Gather (cost=1000.00..10558.00 rows=4500 width=22) (actual time=0.363..30.522 rows=450000 loops=1)Workers Planned: 4Workers Launched: 4-> Parallel Seq Scan on tab1 (cost=0.00..9108.00 rows=1125 width=22) (actual time=0.026..13.946 rows=90000 loops=5)Filter: ((i % 2) = 0)Rows Removed by Filter: 90000Planning Time: 0.104 msExecution Time: 44.707 ms(8 rows)
总结
原文请点击“文章底部”阅读原文查看。
文章转载自PostgreSQL中文社区,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




