Skip to content

fix: use projection-aware ORDER BY for activities queries#59

Merged
scotwells merged 1 commit intomainfrom
fix/projection-aware-order-by
Feb 21, 2026
Merged

fix: use projection-aware ORDER BY for activities queries#59
scotwells merged 1 commit intomainfrom
fix/projection-aware-order-by

Conversation

@scotwells
Copy link
Copy Markdown
Contributor

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

  • 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

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 scotwells merged commit b42ac2f into main Feb 21, 2026
3 checks passed
@scotwells scotwells deleted the fix/projection-aware-order-by branch February 21, 2026 18:26
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant