没有数据的物化视图仍在加载数据

时间:2021-08-01 15:00:26

标签: sql postgresql timescaledb continuous-aggregates

我对创建物化视图 WITH NO DATA 的理解是,在我或我设置的策略刷新视图之前,不会加载任何记录。但是,当使用 timescaledb 并提供此选项时,我可以立即查询表,并且似乎正在加载记录。

我正在关注这些文档:https://docs.timescale.com/timescaledb/latest/how-to-guides/continuous-aggregates/create-a-continuous-aggregate/#create-a-continuous-aggregate

By default, views are automatically refreshed. You can adjust this by setting the WITH NO DATA option.

CREATE MATERIALIZED VIEW timescaledb_view
WITH (timescaledb.continuous) AS
/* Query */
WITH NO DATA;

然而,在访问 timescaledb_view 时,无论我运行了什么查询,它似乎都焕然一新。我是否误解了这应该如何工作?

1 个答案:

答案 0 :(得分:1)

我假设 TimescaleDB 2.x 作为问题引用了最新的文档。

连续聚合的默认行为是使用 real time aggregation feature,它用在原始超表上运行的视图查询的结果补充物化数据。因此,在空连续聚合的情况下(如问题中所示),对视图的选择将查询原始超表。

可以通过在 creation 期间将选项 timescaledb.materialized_only 设置为 true 来禁用实时聚合,例如:

CREATE MATERIALIZED VIEW timescaledb_view
WITH (timescaledb.continuous, timescaledb.materialized_only=true) AS
/* Query */
WITH NO DATA;

或通过altering the materialized view

ALTER MATERIALIZED VIEW timescaledb_view SET (timescaledb.materialized_only = true);

然后,如果没有实现,无论是手动还是通过创建的策略,对连续聚合的选择应该返回一个空结果。