Skip to content

Commit

Permalink
ui: show data when max size reached for schema insight
Browse files Browse the repository at this point in the history
Previously, when the sql api returned a max size reached
error, we were just showing the error, but not the data
that was also being returned.

This commit updates the Insights Schema page
with the new behaviour.

The query to retrieve the drop recommendations was returning
all indexes and the ui was doing the filtering. This commit
also changes the query to only return the indexes with a
drop recommendation, resulting in a lot less data being sent
with the sql api and causing less size limit reached.
Then the ui just needs to decided the type of drop.

Part Of: cockroachdb#96184
Release note (ui change): Still show data on the console
(with a warning) for Schema Insights when we reach a
"max size exceed" error from the sql api.
  • Loading branch information
maryliag committed Feb 22, 2023
1 parent 18c6917 commit 8b4e0c9
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 1,223 deletions.
70 changes: 43 additions & 27 deletions pkg/ui/workspaces/cluster-ui/src/api/schemaInsightsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
executeInternalSql,
LONG_TIMEOUT,
sqlResultsAreEmpty,
sqlApiErrorMessage,
LARGE_RESULT_SIZE,
SqlApiResponse,
formatApiResult,
} from "./sqlApi";
import {
InsightRecommendation,
Expand Down Expand Up @@ -128,23 +130,37 @@ function createIndexRecommendationsToSchemaInsight(
return results;
}

// This query have an ORDER BY for the cases where we reach the limit of the sql-api
// and want to return the most used ones as a priority.
const dropUnusedIndexQuery: SchemaInsightQuery<ClusterIndexUsageStatistic> = {
name: "DropIndex",
query: `SELECT
us.table_id,
us.index_id,
us.last_read,
ti.created_at,
ti.index_name,
t.name as table_name,
t.parent_id as database_id,
t.database_name,
t.schema_name,
(SELECT value FROM crdb_internal.cluster_settings WHERE variable = 'sql.index_recommendation.drop_unused_duration') AS unused_threshold
FROM "".crdb_internal.index_usage_statistics AS us
JOIN "".crdb_internal.table_indexes as ti ON us.index_id = ti.index_id AND us.table_id = ti.descriptor_id
JOIN "".crdb_internal.tables as t ON t.table_id = ti.descriptor_id and t.name = ti.descriptor_name
WHERE t.database_name != 'system' AND ti.index_type != 'primary';`,
query: `WITH cs AS (
SELECT value
FROM crdb_internal.cluster_settings
WHERE variable = 'sql.index_recommendation.drop_unused_duration'
)
SELECT * FROM (SELECT us.table_id,
us.index_id,
us.last_read,
us.total_reads,
ti.created_at,
ti.index_name,
t.name as table_name,
t.parent_id as database_id,
t.database_name,
t.schema_name,
cs.value as unused_threshold,
cs.value::interval as interval_threshold,
now() - COALESCE(us.last_read AT TIME ZONE 'UTC', COALESCE(ti.created_at, '0001-01-01')) as unused_interval
FROM "".crdb_internal.index_usage_statistics AS us
JOIN "".crdb_internal.table_indexes as ti
ON us.index_id = ti.index_id AND us.table_id = ti.descriptor_id
JOIN "".crdb_internal.tables as t
ON t.table_id = ti.descriptor_id and t.name = ti.descriptor_name
CROSS JOIN cs
WHERE t.database_name != 'system' AND ti.index_type != 'primary')
WHERE unused_interval > interval_threshold
ORDER BY total_reads DESC;`,
toSchemaInsight: clusterIndexUsageStatsToSchemaInsight,
};

Expand Down Expand Up @@ -181,26 +197,22 @@ const schemaInsightQueries: SchemaInsightQuery<SchemaInsightResponse>[] = [

// getSchemaInsights makes requests over the SQL API and transforms the corresponding
// SQL responses into schema insights.
export async function getSchemaInsights(): Promise<InsightRecommendation[]> {
export async function getSchemaInsights(): Promise<
SqlApiResponse<InsightRecommendation[]>
> {
const request: SqlExecutionRequest = {
statements: schemaInsightQueries.map(insightQuery => ({
sql: insightQuery.query,
})),
execute: true,
max_result_size: LARGE_RESULT_SIZE,
timeout: LONG_TIMEOUT,
};
const result = await executeInternalSql<SchemaInsightResponse>(request);
if (result.error) {
throw new Error(
`Error while retrieving insights information: ${sqlApiErrorMessage(
result.error.message,
)}`,
);
}

const results: InsightRecommendation[] = [];
if (sqlResultsAreEmpty(result)) {
// No data.
return results;
return formatApiResult([], result.error, "retrieving insights information");
}
result.execution.txn_results.map(txn_result => {
// Note: txn_result.statement values begin at 1, not 0.
Expand All @@ -210,5 +222,9 @@ export async function getSchemaInsights(): Promise<InsightRecommendation[]> {
results.push(...insightQuery.toSchemaInsight(txn_result));
}
});
return results;
return formatApiResult(
results,
result.error,
"retrieving insights information",
);
}
235 changes: 0 additions & 235 deletions pkg/ui/workspaces/cluster-ui/src/api/stmtInsightsApi.ts

This file was deleted.

Loading

0 comments on commit 8b4e0c9

Please sign in to comment.