Skip to content

Usage estimator performance#8198

Open
jdolle wants to merge 8 commits into
mainfrom
usage-estimator-performance
Open

Usage estimator performance#8198
jdolle wants to merge 8 commits into
mainfrom
usage-estimator-performance

Conversation

@jdolle

@jdolle jdolle commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Background

I've noticed several spikes in IOWait duration in Clickhouse. This could be caused by a number of things, but one of them is if the read amount is excessive.

Usage estimator polls for usage data using our operations_hourly table. This table has an unoptimized order for querying a target's operation count without any filters.

Description

This adjusts the operation to use the new table that is ideal for this query.

This results in significantly fewer rows being read and a much faster response.

Before:
usage-estimator-before

After:
usage-estimator-after

@jdolle jdolle requested a review from kamilkisiela July 7, 2026 21:24
@jdolle jdolle self-assigned this Jul 7, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This change introduces conditional querying of the optimized operations_by_target_hourly table based on a configured creation date. To ensure correctness, the Zod schema default for OPERATIONS_BY_TARGET_TABLE_CREATED_AT should be changed from new Date() to a deterministic fixed date like new Date(0). Additionally, the year in the configured date 2027-06-11T00:00:00Z should be corrected to prevent queries from falling back to the unoptimized table.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread packages/services/commerce/src/environment.ts Outdated
Comment thread deployment/services/commerce.ts Outdated
Comment thread packages/services/commerce/.env.template Outdated
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
hive 11.5.0-alpha-20260709202147-674f3306da5351a67a6d7e3cb53d3ce2264401c7 npm ↗︎ unpkg ↗︎

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

🐋 This PR was built and pushed to the following Docker images:

Targets: build

Platforms: linux/amd64

Image Tags: 674f3306da5351a67a6d7e3cb53d3ce2264401c7, 674f330

Comment thread packages/services/commerce/src/usage-estimator/estimator.ts Outdated
@kamilkisiela

Copy link
Copy Markdown
Contributor

I think we could use monthly_overview instead.

The query you're optimizing is from estimateOperationsForAllTargets and it's called only by tRPC's estimateOperationsForAllTargets procedure and finally by:

usageEstimator.estimateOperationsForAllTargets(timeWindow),

The time window there looks like this:

const timeWindow = {
startTime: startOfMonth(now),
endTime: endOfMonth(now),
};

And we sum the targets by organization:

orgRecord.operations.current += operations[target] || 0;

so yeah, we could do:

SELECT
    sum(total) as total,
    organization
FROM
    monthly_overview PREWHERE date = '2026-07-01'
GROUP BY
    organization
# Elapsed: 0.053s Read: 2,650 rows (38.85 KB)

@jdolle

jdolle commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator Author

Nice. Even better.

Screenshot 2026-07-09 at 1 21 49 PM

@jdolle jdolle force-pushed the usage-estimator-performance branch from abc8319 to 38129cf Compare July 9, 2026 20:18
.number()
.min(now.getFullYear() - 1)
.max(new Date(now.setMonth(now.getMonth() + 1)).getFullYear());
.min(now.getUTCFullYear() - 1)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always use UTC to avoid timezone issues.

@jdolle jdolle enabled auto-merge (squash) July 9, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants