-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAK: add primary index to events_pageviews table
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
28 changes: 28 additions & 0 deletions
28
pkg/embedded/ch_migrations/20240320095915_BREAK_recreate_events_pageviews_table.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
RENAME TABLE events_pageviews TO events_pageviews_old; | ||
|
||
-- Create a more optimized version of events_pageviews table. | ||
-- This version use a better order by clause and adds a partition by clause | ||
-- that can't be added to an existing table. | ||
|
||
-- It is beneficial for queries to order the primary key columns by cardinality in ascending order. | ||
-- https://clickhouse.com/docs/en/optimize/sparse-primary-indexes#summary | ||
CREATE TABLE events_pageviews ( | ||
timestamp DateTime('UTC'), | ||
domain String, | ||
path String, | ||
operating_system LowCardinality(String), | ||
browser_family LowCardinality(String), | ||
device LowCardinality(String), | ||
referrer_domain String, | ||
country_code LowCardinality(String) | ||
) | ||
ENGINE = MergeTree | ||
PRIMARY KEY (domain, path) | ||
ORDER BY (domain, path, toDate(timestamp), operating_system, browser_family, device, referrer_domain, country_code) | ||
PARTITION BY toYYYYMM(timestamp); | ||
|
||
-- Move rows to new table. | ||
INSERT INTO events_pageviews SELECT * FROM events_pageviews_old; | ||
|
||
-- Delete old table. | ||
DROP TABLE events_pageviews_old; |