调大work_mem减少I/O 本页介绍天翼云TeleDB数据库通过调大workmem来减少I/O的优化案例。 workmem参数为SQL中排序、hash散列操作时可用的内存,当排序、hash散列操作需要的内存超过workmem时,会将超出部分写入临时文件,当触发临时文件写时,会产生I/O,大量的临时文件写会严重影响SQL性能。 workmem默认为4MB,可以针对需要更多workmem的SQL进行会话级/语句级设置,不建议全局设置过大,避免因内存消耗过多导致的OOM。 下面是用到workmem参数调整的SQL优化案例: 1. 测试数据准备: teledb CREATE TABLE t1(f1 serial not null unique,f2 text,f3 text,f4 text,f5 text,f6 text,f7 text,f8 text,f9 text,f10 text,f11 text,f12 text) distribute by shard(f1); NOTICE: Replica identity is needed for shard table, please add to this table through "alter table" command. CREATE TABLE Time: 70.545 ms teledb CREATE TABLE t2(f1 serial not null unique,f2 text,f3 text,f4 text,f5 text,f6 text,f7 text,f8 text,f9 text,f10 text,f11 text,f12 text) distribute by shard(f1); NOTICE: Replica identity is needed for shard table, please add to this table through "alter table" command. CREATE TABLE Time: 61.913 ms teledb insert into t1 select t,md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text) from generateseries(1,1000) as t; INSERT 0 1000 Time: 48.866 ms teledb insert into t2 select t,md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text),md5(t::text) from generateseries(1,50000) as t; INSERT 0 50000 Time: 792.858 ms teledb