【问题标题】:Different Performances On Postgresql Materialized View For Similar QueriesPostgresql物化视图上类似查询的不同表现
【发布时间】:2022-01-13 18:15:42
【问题描述】:

我有一个有 3 列的表; id(serial), jsonbData(jsonb), date(timestamp) 与 3.200.000 行。我从这个表创建了一个物化视图,它具有与表完全相同的列。物化视图和表在 jsonb 键和日期列上都有 btree 索引。

我对表和视图都执行此查询。 (endDate 和 startDate 是静态的):

select * from table/matView where date <= endDate and date >= startDate and jsonbData ->> 'name' = x 

物化视图和表的性能几乎相同,但是每当我更改查询以选择 randomDate 作为 endDate 和 randomDate - 180 天作为 startDate 时,表的执行时间不会改变,但物化视图的执行时间几乎是 4 倍前。 我无法弄清楚物化视图性能下降的原因是什么。表和物化视图之间的唯一区别是表上的 id 列是主键并且上面有索引。

表:

CREATE TABLE app_fraud (
    app_data_id serial4 NOT NULL,
    app_content jsonb NULL,
    created_at timestamp NULL,
    CONSTRAINT app_fraud_pkey PRIMARY KEY (app_data_id)
);

CREATE INDEX app_fraud_created_at_idx ON app_fraud USING btree (created_at);
CREATE INDEX app_fraud_expr_idx ON app_fraud USING btree (((app_content ->> 'ma_TCKN'::text)));

物化视图:

create materialized view app_fraud_mat_view as select app_data_id, app_content, created_at from app_fraud;

CREATE INDEX app_fraud_created_at_idx22 ON app_fraud_mat_view USING btree (created_at);
CREATE INDEX app_fraud_expr_idx22 ON app_fraud_mat_view USING btree (((app_content ->> 'ma_TCKN'::text)));

解释分析查询表:

explain(analyze,buffers)
select * from app_fraud where created_at <= '2020-12-19' and created_at >= '2019-01-01' and app_content ->> 'ma_TCKN' = '1611738921030';

解释物化视图的分析查询:

explain(analyze,buffers)
select * from app_fraud_mat_view where created_at <= '2020-12-19' and created_at >= '2019-01-01' and app_content ->> 'ma_TCKN' = '1611738921030';

【问题讨论】:

  • 请显示所有涉及的对象的CREATE 语句和查询的EXPLAIN (ANALYZE. BUFFERS) 输出。
  • 垫子视图的行估计值相差甚远。如果你分析它会发生什么?
  • 你的意思是分析所有物化视图?

标签: postgresql materialized-views


【解决方案1】:

按照@jjanes 的建议分析物化视图后,即使我不知道为什么但问题解决了。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-30
  • 2013-01-20
  • 2011-05-15
  • 1970-01-01
  • 2017-08-12
相关资源
最近更新 更多