Глава 28. Секционирование данных по году и месяцу
В этом сценарии описывается, как хранить данные в хранилище в виде секционированной аналитической таблицы. Для шагов ниже предполагается, что данные хранятся в таблице-куче axe_heap_table.
Чтобы секционировать данные по году и месяцу на сервере Postgres Pro AXE:
Создайте хранилище для данных.
Например, создайте локальное хранилище:
SELECT axe.create_storage( 'axe_storage', 'file:///tmp/axe_storage/' );Создайте секционированную аналитическую таблицу и скопируйте в неё данные из таблицы-кучи.
Например:
CREATE TABLE public.axe_archive_table ( id int4 NULL, "name" text NULL, price numeric(10,2) NULL, created_at timestamp NULL, year int4 NULL, month int4 NULL ) USING axe WITH ( storage = 'axe_storage', partitioned_by = 'year, month' );INSERT INTO public.axe_archive_table (id, "name", price, created_at, year, month) SELECT id, "name", price, created_at, EXTRACT(year FROM created_at)::int, EXTRACT(month FROM created_at)::int FROM public.axe_heap_table;Данные, скопированные в секционированную аналитическую таблицу, разделяются на несколько Parquet-файлов по столбцам секций, и эти файлы хранятся как листья дерева каталогов. За подробной информацией о Hive-секционировании обратитесь к Главе 23.
В этом примере столбцы секции
yearиmonthзаполняются из столбцаcreated_at, когда данные копируются в аналитическую таблицу, поэтому Parquet-файлы группируются в каталоги в форматеyear=2024/month=01/.Проверьте, что данные разделены по секциям.
Например:
SELECT year, month, count(*) AS rows FROM public.axe_archive_table GROUP BY year, month ORDER BY year, month;
Примечание
Секции не требуется создавать вручную — они создаются автоматически каждый раз, когда новые значения столбцов секции вставляются в аналитическую таблицу.
Chapter 28. Partitioning Data by Year and Month
This scenario describes how to store data in a storage as a partitioned analytical table. For the steps below, it is assumed that the data is stored in the axe_heap_table heap table.
To partition the data by year and month on the Postgres Pro AXE server:
Create a storage for the data.
For example, create a local storage:
SELECT axe.create_storage( 'axe_storage', 'file:///tmp/axe_storage/' );Create a partitioned analytical table and copy the data from the heap table to it.
For example:
CREATE TABLE public.axe_archive_table ( id int4 NULL, "name" text NULL, price numeric(10,2) NULL, created_at timestamp NULL, year int4 NULL, month int4 NULL ) USING axe WITH ( storage = 'axe_storage', partitioned_by = 'year, month' );INSERT INTO public.axe_archive_table (id, "name", price, created_at, year, month) SELECT id, "name", price, created_at, EXTRACT(year FROM created_at)::int, EXTRACT(month FROM created_at)::int FROM public.axe_heap_table;The data copied to a partitioned analytical table is split into multiple Parquet files based on partition columns, and these files are stored in the leaves of a directory tree. For more information about Hive partitioning, refer to Chapter 23.
In this example, the
yearandmonthpartition columns are filled from thecreated_atcolumn when the data is copied to the analytical table, so Parquet files are grouped into directories in theyear=2024/month=01/format.Check that the data is split by partitions.
For example:
SELECT year, month, count(*) AS rows FROM public.axe_archive_table GROUP BY year, month ORDER BY year, month;
Note
You do not need to create partitions manually — they are created automatically each time new partition column values are inserted into the analytical table.