日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
分布式PostgreSQL集群(Citus)官方示例-時間序列數(shù)據(jù)

本文轉(zhuǎn)載自微信公眾號「黑客下午茶」,作者為少。轉(zhuǎn)載本文請聯(lián)系黑客下午茶公眾號。

我們注重客戶提出的每個要求,我們充分考慮每一個細(xì)節(jié),我們積極的做好成都網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),我們努力開拓更好的視野,通過不懈的努力,成都創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計,小程序開發(fā),網(wǎng)站開發(fā),技術(shù)開發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。

 PostgreSQL 在時間序列工作負(fù)載中,應(yīng)用程序(例如一些實(shí)時應(yīng)用程序查詢最近的信息,同時歸檔舊信息。

  • https://docs.citusdata.com/en/v10.2/sharding/data_modeling.html#distributing-by-entity-id

為了處理這種工作負(fù)載,單節(jié)點(diǎn) PostgreSQL 數(shù)據(jù)庫通常會使用表分區(qū)將一個按時間排序的大數(shù)據(jù)表分解為多個繼承表,每個表包含不同的時間范圍。

  • https://www.postgresql.org/docs/current/static/ddl-partitioning.html

將數(shù)據(jù)存儲在多個物理表中會加速數(shù)據(jù)過期。在單個大表中,刪除行會產(chǎn)生掃描以查找要刪除的行,然后清理清空空間的成本。另一方面,刪除分區(qū)是一種與數(shù)據(jù)大小無關(guān)的快速操作。這相當(dāng)于簡單地刪除磁盤上包含數(shù)據(jù)的文件。

將數(shù)據(jù)存儲在多個物理表中會加快數(shù)據(jù)過期的速度。在一個大表中,刪除行需要掃描以找到要刪除的行,然后清空空的空間。另一方面,刪除分區(qū)是一種與數(shù)據(jù)大小無關(guān)的快速操作。這相當(dāng)于簡單地刪除磁盤上包含數(shù)據(jù)的文件。

  • https://www.postgresql.org/docs/current/static/routine-vacuuming.html

對表進(jìn)行分區(qū)還可以使每個日期范圍內(nèi)的索引更小更快。對最近數(shù)據(jù)進(jìn)行的查詢很可能對適合內(nèi)存的 hot 索引進(jìn)行操作。這加快了讀取速度。

插入也有更小的索引要更新,所以它們也更快。

在以下情況下,基于時間的分區(qū)最有意義:

  • 大多數(shù)查詢只訪問最近數(shù)據(jù)的一個非常小的子集
  • 舊數(shù)據(jù)定期過期(刪除/丟棄)

請記住,在錯誤的情況下,讀取所有這些分區(qū)對開銷的傷害大于幫助。但是,在正確的情況下,它非常有幫助。例如,保留一年的時間序列數(shù)據(jù)并定期僅查詢最近一周。

擴(kuò)展 Citus 上的時間序列數(shù)據(jù)

我們可以將單節(jié)點(diǎn)表分區(qū)技術(shù)與 Citus 的分布式分片相結(jié)合,形成一個可擴(kuò)展的時間序列數(shù)據(jù)庫。這是兩全其美的。它在 Postgres 的聲明性表分區(qū)之上特別優(yōu)雅。

例如,讓我們 distribute 和 partition 一個包含歷史 GitHub 事件數(shù)據(jù)的表。

  • GitHub 事件數(shù)據(jù)

https://examples.citusdata.com/events.csv

此 GitHub 數(shù)據(jù)集中的每條記錄代表在 GitHub 中創(chuàng)建的事件,以及有關(guān)事件的關(guān)鍵信息,例如事件類型、創(chuàng)建日期和創(chuàng)建事件的用戶。

第一步是按時間創(chuàng)建和分區(qū)(partition)表,就像我們在單節(jié)點(diǎn) PostgreSQL 數(shù)據(jù)庫中一樣:

-- declaratively partitioned table
CREATE TABLE github_events (
event_id bigint,
event_type text,
event_public boolean,
repo_id bigint,
payload jsonb,
repo jsonb,
actor jsonb,
org jsonb,
created_at timestamp
) PARTITION BY RANGE (created_at);

注意 PARTITION BY RANGE (created_at)。這告訴 Postgres 該表將由 created_at 列在有序范圍內(nèi)進(jìn)行分區(qū)。不過,我們還沒有為特定范圍創(chuàng)建任何分區(qū)。

在創(chuàng)建特定分區(qū)之前,讓我們在 Citus 中分布表。我們將按 repo_id 進(jìn)行分片,這意味著事件將被聚集到每個存儲庫的分片中。

SELECT create_distributed_table('github_events', 'repo_id');

此時 Citus 已跨工作節(jié)點(diǎn)為該表創(chuàng)建分片。在內(nèi)部,每個分片是一個表,每個分片標(biāo)識符 N 的名稱為 github_events_N。此外,Citus 傳播了分區(qū)信息,每個分片都聲明了 Partition key: RANGE (created_at)。

分區(qū)表不能直接包含數(shù)據(jù),它更像是跨分區(qū)的視圖。因此,分片還沒有準(zhǔn)備好保存數(shù)據(jù)。我們需要創(chuàng)建分區(qū)并指定它們的時間范圍,之后我們可以插入與范圍匹配的數(shù)據(jù)。

自動創(chuàng)建分區(qū)

Citus 為分區(qū)管理提供了輔助函數(shù)。我們可以使用 create_time_partitions() 創(chuàng)建一批每月分區(qū):

SELECT create_time_partitions(
table_name := 'github_events',
partition_interval := '1 month',
end_at := now() + '12 months'
);

Citus 還包括一個視圖 time_partitions,以方便地調(diào)查它創(chuàng)建的分區(qū)。

隨著時間的推移,您將需要進(jìn)行一些維護(hù)以創(chuàng)建新分區(qū)并刪除舊分區(qū)。最好設(shè)置一個定期 job 來運(yùn)行帶有 pg_cron 之類的擴(kuò)展的維護(hù)功能:

  • pg_cron

https://github.com/citusdata/pg_cron

-- set two monthly cron jobs:

-- 1. ensure we have partitions for the next 12 months

SELECT cron.schedule('create-partitions', '0 0 1 * *', $$
SELECT create_time_partitions(
table_name := 'github_events',
partition_interval := '1 month',
end_at := now() + '12 months'
)
$$);

-- 2. (optional) ensure we never have more than one year of data

SELECT cron.schedule('drop-partitions', '0 0 1 * *', $$
CALL drop_old_time_partitions(
'github_events',
now() - interval '12 months' /* older_than */
);
$$);

一旦設(shè)置了定期維護(hù),您就不必再考慮分區(qū)了,它們可以正常工作。

請注意,Postgres 中的原生分區(qū)仍然很新,并且有一些怪癖。對分區(qū)表的維護(hù)操作將獲取可能會短暫停止查詢的激進(jìn)鎖。目前在 postgres 社區(qū)中正在進(jìn)行大量工作來解決這些問題,因此預(yù)計 Postgres 中的 time 分區(qū)只會變得更好。

使用列式存儲歸檔

一些應(yīng)用程序的數(shù)據(jù)在邏輯上分為可更新的小部分和“凍結(jié)(frozen)”的較大部分。示例包括日志、點(diǎn)擊流或銷售記錄。在這種情況下,我們可以將分區(qū)與列式表存儲(在 Citus 10 中引入)結(jié)合起來壓縮磁盤上的歷史分區(qū)。Citus 柱狀表目前是僅追加的,這意味著它們不支持更新或刪除,但我們可以將它們用于不可變的歷史分區(qū)。

  • 列式表存儲

https://docs.citusdata.com/en/v10.2/admin_guide/table_management.html#columnar

分區(qū)表可以由行分區(qū)和列分區(qū)的任意組合組成。在 timestamp key 上使用范圍分區(qū)時,我們可以將最新的分區(qū)制作成行表,并定期將最新的分區(qū)滾動到另一個歷史列式分區(qū)中。

讓我們看一個例子,再次使用 GitHub 事件。我們將創(chuàng)建一個名為 github_columnar_events 的新表,以消除前面示例中的歧義。為了完全專注于列式存儲方面,我們不會分布此表。

接下來,下載示例數(shù)據(jù):

wget http://examples.citusdata.com/github_archive/github_events-2015-01-01-{0..5}.csv.gz
gzip -c -d github_events-2015-01-01-*.gz >> github_events.csv
-- our new table, same structure as the example in
-- the previous section

CREATE TABLE github_columnar_events ( LIKE github_events )
PARTITION BY RANGE (created_at);

-- create partitions to hold two hours of data each

SELECT create_time_partitions(
table_name := 'github_columnar_events',
partition_interval := '2 hours',
start_from := '2015-01-01 00:00:00',
end_at := '2015-01-01 08:00:00'
);

-- fill with sample data
-- (note that this data requires the database to have UTF8 encoding)

\COPY github_columnar_events FROM 'github_events.csv' WITH (format CSV)

-- list the partitions, and confirm they're
-- using row-based storage (heap access method)

SELECT partition, access_method
FROM time_partitions
WHERE parent_table = 'github_columnar_events'::regclass;

-- convert older partitions to use columnar storage

CALL alter_old_partitions_set_access_method(
'github_columnar_events',
'2015-01-01 06:00:00' /* older_than */,
'columnar'
);

-- the old partitions are now columnar, while the
-- latest uses row storage and can be updated

SELECT partition, access_method
FROM time_partitions
WHERE parent_table = 'github_columnar_events'::regclass;

要查看柱狀表的壓縮率,請使用 VACUUM VERBOSE。我們?nèi)齻€柱狀分區(qū)的壓縮比相當(dāng)不錯:

VACUUM VERBOSE github_columnar_events;
INFO:  statistics for "github_columnar_events_p2015_01_01_0000":
storage id: 10000000003
total file size: 4481024, total data size: 4444425
compression rate: 8.31x
total row count: 15129, stripe count: 1, average rows per stripe: 15129
chunk count: 18, containing data for dropped columns: 0, zstd compressed: 18

INFO: statistics for "github_columnar_events_p2015_01_01_0200":
storage id: 10000000004
total file size: 3579904, total data size: 3548221
compression rate: 8.26x
total row count: 12714, stripe count: 1, average rows per stripe: 12714
chunk count: 18, containing data for dropped columns: 0, zstd compressed: 18

INFO: statistics for "github_columnar_events_p2015_01_01_0400":
storage id: 10000000005
total file size: 2949120, total data size: 2917407
compression rate: 8.51x
total row count: 11756, stripe count: 1, average rows per stripe: 11756
chunk count: 18, containing data for dropped columns: 0, zstd compressed: 18

分區(qū)表 github_columnar_events 的一個強(qiáng)大之處在于它可以像普通表一樣被完整地查詢。

SELECT COUNT(DISTINCT repo_id)
FROM github_columnar_events;

只要分區(qū)鍵上有一個 WHERE 子句,它可以完全過濾到行表分區(qū)中,條目就可以被更新或刪除。

將行分區(qū)歸檔到列式存儲

當(dāng)行分區(qū)已填滿其范圍時,您可以將其歸檔到壓縮的列式存儲中。我們可以使用 pg_cron 自動執(zhí)行此操作,如下所示:

-- a monthly cron job

SELECT cron.schedule('compress-partitions', '0 0 1 * *', $$
CALL alter_old_partitions_set_access_method(
'github_columnar_events',
now() - interval '6 months' /* older_than */,
'columnar'
);
$$);

有關(guān)詳細(xì)信息,請參閱列式存儲。


當(dāng)前名稱:分布式PostgreSQL集群(Citus)官方示例-時間序列數(shù)據(jù)
網(wǎng)站地址:http://m.5511xx.com/article/cccgsdj.html