我们的系统写入了大量数据(一种大数据系统)。写入性能足以满足我们的需求,但读取性能真的太慢了。
我们所有表的主键(约束)结构都相似:
timestamp(Timestamp) ; index(smallint) ; key(integer).
Run Code Online (Sandbox Code Playgroud)
一个表可以有数百万行,甚至数十亿行,而一个读请求通常是针对特定时间段(时间戳/索引)和标记的。查询返回大约 20 万行是很常见的。目前,我们每秒可以读取大约 15k 行,但我们需要快 10 倍。这是可能的,如果是,如何?
注意: PostgreSQL 是和我们的软件一起打包的,所以不同客户端的硬件是不一样的。
它是一个用于测试的虚拟机。VM 的主机是具有 24.0 GB RAM 的 Windows Server 2008 R2 x64。
Server 2008 R2 x64
2.00 GB of memory
Intel Xeon W3520 @ 2.67GHz (2 cores)
Run Code Online (Sandbox Code Playgroud)
postgresql.conf 优化shared_buffers = 512MB (default: 32MB)
effective_cache_size = 1024MB (default: 128MB)
checkpoint_segment = 32 (default: 3)
checkpoint_completion_target = 0.9 (default: 0.5)
default_statistics_target = 1000 (default: 100)
work_mem = 100MB (default: 1MB)
maintainance_work_mem = 256MB …Run Code Online (Sandbox Code Playgroud) 我有两个相当简单的查询。第一个查询
UPDATE mp_physical SET periodic_number = '' WHERE periodic_number is NULL;
Run Code Online (Sandbox Code Playgroud)
这是计划
duration: 0.125 ms plan:
Query Text: UPDATE mp_physical SET periodic_number = '' WHERE periodic_number is NULL;
Update on mp_physical (cost=0.42..7.34 rows=1 width=801)
-> Index Scan using "_I_periodic_number" on mp_physical (cost=0.42..7.34 rows=1 width=801)
Index Cond: (periodic_number IS NULL)
Run Code Online (Sandbox Code Playgroud)
第二个:
UPDATE observations_optical_temp SET designation = '' WHERE periodic_number is NULL;
Run Code Online (Sandbox Code Playgroud)
它的计划是:
duration: 2817.375 ms plan:
Query Text: UPDATE observations_optical_temp SET periodic_number = '' WHERE periodic_number is NULL;
Update on observations_optical_temp …Run Code Online (Sandbox Code Playgroud) postgresql performance index execution-plan temporary-tables postgresql-performance
使用 PostgreSQL 8.4,我试图使用 order by 和两个表的索引列查询两个包含 100 万条记录的表,并且我正在失去性能(1 列需要 30 毫秒,两列需要 5 分钟)。例如:
select r.code, r.test_code, r.sample_code, s.barcode, s.registry_date
from requests r
inner join samples s on (s.code = r.sample_code)
order by s.barcode asc , r.code asc
limit 21;
Run Code Online (Sandbox Code Playgroud)
表信息:
CREATE TABLE public.samples (
code BIGINT NOT NULL,
barcode VARCHAR(40) NOT NULL,
registry_date TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT samples_pkey PRIMARY KEY(code)
);
CREATE INDEX idx_samp_barcode ON public.samples (barcode);
CREATE INDEX idx_samp_barcode_code ON public.samples (barcode, code);
CREATE INDEX …Run Code Online (Sandbox Code Playgroud) postgresql performance postgresql-8.4 recursive postgresql-performance