-
Notifications
You must be signed in to change notification settings - Fork 611
fix(clickhouse): improve latest keys used queries for high volume (150M +) #4959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00e70d5
fix(clickhouse): improve clickhouse query for key logs and add new t…
mcstepp e0309b2
fix valid/error count = 0 scenario
mcstepp 3bafd09
remove identity_id from order by
mcstepp 88331f4
wrap identity_id with aggregating function since its removed from the…
mcstepp ca322a8
Merge branch 'main' into ENG-2129-high-volume-improvements
mcstepp 314292c
Merge branch 'main' into ENG-2129-high-volume-improvements
mcstepp ed2c6b3
Merge branch 'main' into ENG-2129-high-volume-improvements
Flo4604 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,30 @@ | ||
| -- Create "keys_last_used_v1" table with AggregatingMergeTree for pre-aggregated data | ||
| CREATE TABLE IF NOT EXISTS `default`.`key_last_used_v1` ( | ||
| `workspace_id` String, | ||
| `key_space_id` String, | ||
| `key_id` String, | ||
| `identity_id` String, | ||
| `time` SimpleAggregateFunction(max, Int64), | ||
| `request_id` SimpleAggregateFunction(anyLast, String), | ||
| `outcome` SimpleAggregateFunction(anyLast, LowCardinality(String)), | ||
| `tags` SimpleAggregateFunction(anyLast, Array(String)) | ||
| ) ENGINE = AggregatingMergeTree() | ||
| ORDER BY (`workspace_id`, `key_space_id`, `key_id`) | ||
| TTL toDateTime(time / 1000) + INTERVAL 90 DAY | ||
| SETTINGS index_granularity = 8192; | ||
|
|
||
| -- Create "keys_last_used_mv_v1" materialized view that pre-aggregates per key | ||
| CREATE MATERIALIZED VIEW IF NOT EXISTS `default`.`key_last_used_mv_v1` | ||
| TO `default`.`key_last_used_v1` | ||
| AS | ||
| SELECT | ||
| workspace_id, | ||
| key_space_id, | ||
| key_id, | ||
| anyLast(identity_id) as identity_id, | ||
| max(time) as time, | ||
| anyLast(request_id) as request_id, | ||
| anyLast(outcome) as outcome, | ||
| anyLast(tags) as tags | ||
| FROM `default`.`key_verifications_raw_v2` | ||
| GROUP BY workspace_id, key_space_id, key_id; | ||
This file contains hidden or 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,37 @@ | ||
| -- Materialized view to track the last verification time for each key and identity | ||
| -- This dramatically improves query performance for the dashboard's "API Requests" page | ||
| -- | ||
| -- IMPORTANT: Stores ONE row per key (latest verification regardless of outcome). | ||
| -- Uses AggregatingMergeTree for automatic aggregation during merges. | ||
| -- Can be queried by key_id OR identity_id for flexible last-used tracking. | ||
|
|
||
| -- Target table that stores the latest verification per key | ||
| CREATE TABLE IF NOT EXISTS `default`.`key_last_used_v1` ( | ||
| `workspace_id` String, | ||
| `key_space_id` String, | ||
| `key_id` String, | ||
| `identity_id` String, | ||
| `time` SimpleAggregateFunction(max, Int64), | ||
| `request_id` SimpleAggregateFunction(anyLast, String), | ||
| `outcome` SimpleAggregateFunction(anyLast, LowCardinality(String)), | ||
| `tags` SimpleAggregateFunction(anyLast, Array(String)) | ||
| ) ENGINE = AggregatingMergeTree() | ||
| ORDER BY (`workspace_id`, `key_space_id`, `key_id`) | ||
| TTL toDateTime(time / 1000) + INTERVAL 90 DAY | ||
| SETTINGS index_granularity = 8192; | ||
|
|
||
| -- Materialized view that automatically populates the table from new inserts | ||
| CREATE MATERIALIZED VIEW IF NOT EXISTS `default`.`key_last_used_mv_v1` | ||
| TO `default`.`key_last_used_v1` | ||
| AS | ||
| SELECT | ||
| workspace_id, | ||
| key_space_id, | ||
| key_id, | ||
| anyLast(identity_id) as identity_id, | ||
| max(time) as time, | ||
| anyLast(request_id) as request_id, | ||
| anyLast(outcome) as outcome, | ||
| anyLast(tags) as tags | ||
| FROM `default`.`key_verifications_raw_v2` | ||
| GROUP BY workspace_id, key_space_id, key_id; |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.