fix: use projection-aware ORDER BY for activities queries#59
Merged
Conversation
Add intelligent ORDER BY selection for activity queries based on scope and filter context: - Tenant-scoped queries (organization/project): Use primary key order (tenant_type, tenant_name, timestamp, resource_uid) for efficient index usage since ClickHouse can seek directly to tenant data - Platform-wide queries: Select ORDER BY based on filters to leverage materialized projections: - API group filter → api_group_query_projection - Actor filter → actor_query_projection - No filter → timestamp-based order This follows the same pattern used by audit logs and ensures optimal query performance for both single-tenant and cross-tenant queries. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
scotwells
added a commit
that referenced
this pull request
Mar 10, 2026
## Summary - Adds intelligent ORDER BY selection for activity queries based on scope and filter context - Tenant-scoped queries use primary key order for efficient index usage - Platform-wide queries select ORDER BY to leverage materialized projections (api_group_query_projection, actor_query_projection) - Follows the same pattern used by audit logs for consistency ## Context The activities table has a primary key of `(tenant_type, tenant_name, timestamp, resource_uid)` and two projections for cross-tenant queries: - `api_group_query_projection`: ORDER BY (api_group, timestamp, tenant_type, tenant_name, resource_uid) - `actor_query_projection`: ORDER BY (actor_name, timestamp, tenant_type, tenant_name, resource_uid) Previously, all queries used `ORDER BY timestamp DESC, resource_uid DESC` which didn't match either the primary key or projections. ## Test plan - [x] Verified all storage package tests pass - [ ] Manual verification with tenant-scoped activity queries - [ ] Manual verification with platform-wide API group filtered queries 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Context
The activities table has a primary key of
(tenant_type, tenant_name, timestamp, resource_uid)and two projections for cross-tenant queries:api_group_query_projection: ORDER BY (api_group, timestamp, tenant_type, tenant_name, resource_uid)actor_query_projection: ORDER BY (actor_name, timestamp, tenant_type, tenant_name, resource_uid)Previously, all queries used
ORDER BY timestamp DESC, resource_uid DESCwhich didn't match either the primary key or projections.Test plan
🤖 Generated with Claude Code