以智能方式在db中进行数据离散化

对于我的未来项目,我有一个ClickHouse数据库。这个数据库是由RabbitsMQ提供的一些微服务本身提供的。

数据如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Configurationactivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</RelativeLayout>

<Button
    android:id="@+id/celtic_cross"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Celtic Cross"
    android:textSize="24sp" />

<Button
    android:id="@+id/future_love_spread"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Future Partner Spread"
    android:textSize="24sp" />

因此对于数百个不同的nodekey,我每10分钟就有一个值。

我需要有另一个表,其中包含每小时的值的总和或平均值(取决于nodekey类型)...

我的第一个想法只是使用crontab ... 但是数据并没有以流体流动的形式出现,有时微服务会添加2-3个新值,或者有时会是数周的数据出现……而我很少需要大批插入多年的新数据... >

目前,我只有数百个nodekey,但是项目将不断增长。

所以,我认为使用crontab或遍历数据库更新数据不是一个好主意...

我还有其他选择吗?

yichaohao 回答:以智能方式在db中进行数据离散化

仅创建视图怎么样?

create view myview as
select 
    toStartOfHour(datetime) date_hour,nodekey,sum(value) sum_value
from mytable
group by 
    toStartOfHour(datetime),nodekey

此方法的优点是您不必担心刷新数据。查询视图时,实际上是在访问底层实时数据。缺点是,当您的数据集变得非常大时,它可能无法很好地缩放(查询视图的查询往往会变慢)。

一个中间选项是使用materialized view,它将保留数据。如果我正确理解Clickhouse文档,则在修改源表中的数据时,物化视图会自动更新,这似乎与您要查找的内容接近(但是您需要使用适当的引擎,这可能会影响性能插入的内容)。

本文链接:https://www.f2er.com/2982297.html

大家都在问