对 postgresql 进行下采样 table
Downsampling a postgresql table
我有一个非常简单的数据库模型,两个表:object 和 data,以 1:n 关系链接。
每分钟,对于每个对象,都会保存一个新的数据。有趣的属性是:object_id (int), created_at (timestamp) and value(可变字符)。 Object_id和created_at作为复合PK)
我的问题是它生成了太多数据。我正在尝试找到一种有效的方法来定期对 data 进行下采样。我需要做什么:
- 上周不要碰 X
- 每个对象每 Y 分钟只保留一个值。
我不太熟悉 plpgsql,我强烈怀疑 date_trunc and/or 我的复合 PK 包含那个日期...
pgsql 9.4.10
delete from data
where
(
-- year of `created_at` is less then current year
extract('year' from created_at) < extract('year' from current_date)
or
-- year of `created_at` is equal to current year
extract('year' from created_at) = extract('year' from current_date)
and
-- number of week of `created_at` is less then current week by 3 or more
extract('week' from current_date) - extract('week' from created_at) >= 3
)
and
-- number of minutes is not a multiple of 10
extract('minute' from created_at)::int % 10 <> 0
我有一个非常简单的数据库模型,两个表:object 和 data,以 1:n 关系链接。
每分钟,对于每个对象,都会保存一个新的数据。有趣的属性是:object_id (int), created_at (timestamp) and value(可变字符)。 Object_id和created_at作为复合PK)
我的问题是它生成了太多数据。我正在尝试找到一种有效的方法来定期对 data 进行下采样。我需要做什么:
- 上周不要碰 X
- 每个对象每 Y 分钟只保留一个值。
我不太熟悉 plpgsql,我强烈怀疑 date_trunc and/or 我的复合 PK 包含那个日期...
pgsql 9.4.10
delete from data
where
(
-- year of `created_at` is less then current year
extract('year' from created_at) < extract('year' from current_date)
or
-- year of `created_at` is equal to current year
extract('year' from created_at) = extract('year' from current_date)
and
-- number of week of `created_at` is less then current week by 3 or more
extract('week' from current_date) - extract('week' from created_at) >= 3
)
and
-- number of minutes is not a multiple of 10
extract('minute' from created_at)::int % 10 <> 0