Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cute-otters-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'hive': patch
---

Improve performance of schema checks using conditional breaking changes by using a table with an ordering that more closely aligns with the query for total request count for a target
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,53 @@ export class OperationsReader {
.tuple([z.object({ amountOfRequests: z.string() })])
.transform(data => ensureNumber(data[0].amountOfRequests));

/**
* This is using clients_daily instead of operations_daily because the
* order by is more favorable in clients_daily since the client_name is
* right after target. However, this could be better without storing
* much more data, by creating another materialized view ordered by
* target, timestamp, then client_name.
*/
Comment on lines +1016 to +1022

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.

high

When args.excludedClients is null or empty, we do not filter by client_name. In this case, querying clients_daily is highly inefficient compared to operations_daily because:

  1. clients_daily has a much higher cardinality (more rows) since it groups by client name and version.
  2. The sorting key of clients_daily has client_name right after target (e.g., (target, client_name, timestamp)), which means ClickHouse cannot efficiently use the primary key index for the timestamp range filter when client_name is omitted.

We should conditionally select from operations_daily when no client exclusion is requested to avoid a significant performance regression for standard schema checks. Additionally, when refactoring or optimizing SQL queries, write comprehensive integration tests to verify correctness under edge cases (such as shared commits across multiple targets) to ensure correctness and prevent regressions.

    const useClientsDaily = !!(args.excludedClients && args.excludedClients.length > 0);
    const table = sql.raw(useClientsDaily ? 'clients_daily' : 'operations_daily');

    /**
     * This is using clients_daily instead of operations_daily because the
     * order by is more favorable in clients_daily since the client_name is
     * right after target. However, this could be better without storing
     * much more data, by creating another materialized view ordered by
     * target, timestamp, then client_name.
     */
References
  1. Instead of passing a boolean parameter to control query routing and adding defensive guards, derive the routing logic directly from the state of the conditions (e.g., checking if filter conditions are empty) inside the query function.
  2. When refactoring or optimizing SQL queries, write comprehensive integration tests to verify correctness under edge cases (such as shared commits across multiple targets) to ensure correctness and prevent regressions.

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.

operations_daily uses:

ORDER BY
    (target, hash, client_name, client_version, timestamp) SETTINGS 
...
GROUP BY
    target,
    hash,
    client_name,
    client_version,
    timestamp,
    expires_at

clients_daily uses:

ORDER BY
    (target, client_name, client_version, hash, timestamp) SETTINGS 
...
GROUP BY
    target,
    client_name,
    client_version,
    hash,
    timestamp,
    expires_at

There are potentially many hash. If we can filter by the client_name immediately, then this is clearly a win. Idk what this AI comment is talking about.

I'm also choosing to not use operations_daily because operations_by_target_hourly is indexed almost exactly how we want it for the non-filtered query.

if (args.excludedClients && args.excludedClients.length > 0) {
return await this.clickHouse
.query<unknown>({
queryId: 'getTotalCountForSchemaCoordinatesExcludingClients',
query: sql`
SELECT
SUM("result"."total") AS "amountOfRequests"
FROM (
SELECT
SUM("clients_daily"."total") AS "total"
FROM
"clients_daily"
PREWHERE
"clients_daily"."target" IN (${sql.array(args.targetIds, 'String')})
AND "clients_daily"."timestamp" >= toDateTime(${formatDate(args.period.from)}, 'UTC')
AND "clients_daily"."timestamp" <= toDateTime(${formatDate(args.period.to)}, 'UTC')
AND "clients_daily"."client_name" NOT IN (${sql.array(args.excludedClients, 'String')})

UNION ALL

SELECT
SUM("subscription_operations_daily"."total") AS "total"
FROM
"subscription_operations_daily"
PREWHERE
"subscription_operations_daily"."target" IN (${sql.array(args.targetIds, 'String')})
AND "subscription_operations_daily"."timestamp" >= toDateTime(${formatDate(args.period.from)}, 'UTC')
AND "subscription_operations_daily"."timestamp" <= toDateTime(${formatDate(args.period.to)}, 'UTC')
AND "subscription_operations_daily"."client_name" NOT IN (${sql.array(args.excludedClients, 'String')})
) AS "result"
`,
timeout: 10_000,
})
.then(result => TotalCountModel.parse(result.data));
}

/**
* This uses the "operations_by_target_hourly" table because there is no daily
* and even with 24 hours in a day, the table's order by will more than compensate.
*/
return await this.clickHouse
.query<unknown>({
queryId: 'getTotalCountForSchemaCoordinates',
Expand All @@ -1021,14 +1068,13 @@ export class OperationsReader {
SUM("result"."total") AS "amountOfRequests"
FROM (
SELECT
SUM("operations_daily"."total") AS "total"
SUM("operations_by_target_hourly"."total") AS "total"
FROM
"operations_daily"
"operations_by_target_hourly"
PREWHERE
"operations_daily"."target" IN (${sql.array(args.targetIds, 'String')})
AND "operations_daily"."timestamp" >= toDateTime(${formatDate(args.period.from)}, 'UTC')
AND "operations_daily"."timestamp" <= toDateTime(${formatDate(args.period.to)}, 'UTC')
${args.excludedClients ? sql`AND "operations_daily"."client_name" NOT IN (${sql.array(args.excludedClients, 'String')})` : sql``}
"operations_by_target_hourly"."target" IN (${sql.array(args.targetIds, 'String')})
AND "operations_by_target_hourly"."timestamp" >= toDateTime(${formatDate(args.period.from)}, 'UTC')
AND "operations_by_target_hourly"."timestamp" <= toDateTime(${formatDate(args.period.to)}, 'UTC')

UNION ALL

Expand All @@ -1040,7 +1086,6 @@ export class OperationsReader {
"subscription_operations_daily"."target" IN (${sql.array(args.targetIds, 'String')})
AND "subscription_operations_daily"."timestamp" >= toDateTime(${formatDate(args.period.from)}, 'UTC')
AND "subscription_operations_daily"."timestamp" <= toDateTime(${formatDate(args.period.to)}, 'UTC')
${args.excludedClients ? sql`AND "subscription_operations_daily"."client_name" NOT IN (${sql.array(args.excludedClients, 'String')})` : sql``}
) AS "result"
`,
timeout: 10_000,
Expand Down
Loading