在我们的 PostgreSQL 9.4.4 数据库中,我们有一个每天接收大约 60 万条新记录的表。每天,每晚,我们都会从表中执行一些 ETL 导出。如果在导出之前没有分析过,真的很慢。如果我们运行ANALYZE,速度会更快,因为规划器使用具有我们查询的字段的多列索引。解决慢查询问题的首选方法是什么?我看到三个选项:
ANALYZE 在出口之前,第二个选项要求我们指定每个表的自动清理/分析设置,因为默认设置不适用于大表。即使我们正确设置了它,也有一些边缘情况,当它仍然会引起麻烦时。例如:
autovacuum_analyze_scale_factor = 0.1
Run Code Online (Sandbox Code Playgroud)
现在是默认值 - 所以如果我们的表有大约 2500 万条记录,它会在 250 万条记录后分析,这对我们来说不够频繁(我们每天有大约 60 万笔新交易)。但是,如果我们将其设置为 0.02(约 500k 条记录),则可能会发生这样的情况,对于有许多事务(例如 900k)的一天,我们将在 500k 之后运行 ANALYZE,但 400k 将保持未分析状态,这将影响查询性能。
表结构:
Table "public.bet_transactions"
Column | Type | Modifiers
----------------------------+-----------------------------+---------------------------------------------------------------
id | integer | not null default nextval('bet_transactions_id_seq'::regclass)
account_id | integer | not null
amount_cents | integer | not null default 0
money_amount_cents | integer | not null default 0
bonus_amount_cents | integer | not …Run Code Online (Sandbox Code Playgroud)