Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: cache query result (#16105)

ecmadaobaabf357e5f0
diff --git a/frontend/src/composables/useExecuteSQL.ts b/frontend/src/composables/useExecuteSQL.ts
index e75900b051..118361aa68 100644
--- a/frontend/src/composables/useExecuteSQL.ts
+++ b/frontend/src/composables/useExecuteSQL.ts
@@ -97,21 +97,23 @@ const useExecuteSQL = () => {
return false;
}
+ const emptyContext = emptySQLEditorTabQueryContext();
tab.queryContext = {
- ...emptySQLEditorTabQueryContext(),
- params,
+ ...emptyContext,
status: "EXECUTING",
+ results: tab.queryContext?.results ?? emptyContext.results,
};
return true;
};
- const check = async (): Promise<SQLCheckResult> => {
+ const check = async (
+ params: SQLEditorQueryParams
+ ): Promise<SQLCheckResult> => {
const tab = tabStore.currentTab;
if (!tab) {
return { passed: false };
}
- const params = tab.queryContext?.params;
const abortController = tab.queryContext?.abortController;
if (!params) {
return { passed: false };
@@ -223,22 +225,18 @@ const useExecuteSQL = () => {
}
}
- const queryResultMap = new Map<string, SQLResultSetV1>();
+ const beginTimestampMS = Date.now();
for (const database of batchQueryDatabases) {
- queryResultMap.set(database.name, {
- error: "",
- results: [],
- advices: [],
- });
+ if (!queryContext.results.has(database.name)) {
+ queryContext.results.set(database.name, []);
+ }
}
- queryContext.results = queryResultMap;
- const fail = (database: ComposedDatabase, result: SQLResultSetV1) => {
- queryResultMap.set(database.name, {
- error: result.error,
- results: [],
- advices: result.advices,
- status: result.status,
+ const fail = (database: ComposedDatabase, resultSet: SQLResultSetV1) => {
+ queryContext.results.get(database.name)!.push({
+ params,
+ beginTimestampMS,
+ resultSet,
});
};
const abort = (error: string, advices: Advice[] = []) => {
@@ -254,13 +252,11 @@ const useExecuteSQL = () => {
const { abortController } = queryContext;
const sqlStore = useSQLStore();
- queryContext.beginTimestampMS = Date.now();
-
const checkBehavior = params.skipCheck ? "SKIP" : sqlCheckStyle.value;
let checkResult: SQLCheckResult = { passed: true };
if (checkBehavior !== "SKIP") {
try {
- checkResult = await check();
+ checkResult = await check(params);
} catch (error) {
return abort(extractGrpcErrorMessage(error));
}
@@ -368,7 +364,13 @@ const useExecuteSQL = () => {
fail(database, resultSet);
}
} else {
- queryResultMap.set(database.name, markRaw(resultSet));
+ queryContext.results.get(database.name)!.push(
+ markRaw({
+ params,
+ beginTimestampMS,
+ resultSet,
+ })
+ );
// After all the queries are executed, we update the tab with the latest query result map.
// Refresh the query history list when the query executed successfully
// (with or without warnings).
diff --git a/frontend/src/types/sqlEditor/tab.ts b/frontend/src/types/sqlEditor/tab.ts
index c7020037f6..dc2b04e893 100644
--- a/frontend/src/types/sqlEditor/tab.ts
+++ b/frontend/src/types/sqlEditor/tab.ts
@@ -24,8 +24,14 @@ export type SQLEditorTabQueryContext = {
abortController: AbortController;
status: "IDLE" | "EXECUTING";
- params: SQLEditorQueryParams;
- results: Map<string /* database or instance */, SQLResultSetV1>;
+ results: Map<
+ string /* database or instance */,
+ {
+ params: SQLEditorQueryParams;
+ beginTimestampMS: number;
+ resultSet: SQLResultSetV1;
+ }[]
+ >;
};
export type SQLEditorTab = {
diff --git a/frontend/src/utils/sqlEditor.ts b/frontend/src/utils/sqlEditor.ts
index 100d4756e8..ba4c063bda 100644
--- a/frontend/src/utils/sqlEditor.ts
+++ b/frontend/src/utils/sqlEditor.ts
@@ -20,7 +20,6 @@ import {
isValidInstanceName,
UNKNOWN_DATABASE_NAME,
} from "@/types";
-import { Engine } from "@/types/proto/v1/common";
import {
DataSourceType,
type InstanceResource,
@@ -175,13 +174,6 @@ export const emptySQLEditorTabQueryContext = (): SQLEditorTabQueryContext => ({
abortController: new AbortController(),
status: "IDLE",
results: new Map(),
- params: {
- connection: emptySQLEditorConnection(),
- engine: Engine.MYSQL,
- explain: false,
- statement: "",
- selection: null,
- },
});
export const getAdminDataSourceRestrictionOfDatabase = (
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
index baa4c28d33..fdd6cfb717 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/ResultViewV1.vue
@@ -1,7 +1,7 @@
<template>
<NConfigProvider
v-bind="naiveUIConfig"
- class="relative flex flex-col justify-start items-start p-2 pb-1 overflow-y-auto"
+ class="relative flex flex-col justify-start items-start pb-1 overflow-y-auto"
:class="dark && 'dark bg-dark-bg'"
>
<template v-if="executeParams && resultSet && !showPlaceholder">
@@ -22,7 +22,7 @@
</template>
<template v-else-if="viewMode === 'MULTI-RESULT'">
<NTabs
- type="card"
+ type="line"
size="small"
class="flex-1 flex flex-col overflow-hidden"
style="--n-tab-padding: 4px 12px"
@@ -108,7 +108,6 @@
import { Info } from "lucide-vue-next";
import { darkTheme, NConfigProvider, NTabs, NTabPane } from "naive-ui";
import { Status } from "nice-grpc-common";
-import type { PropType } from "vue";
import { computed, ref, toRef } from "vue";
import { useI18n } from "vue-i18n";
import { darkThemeOverrides } from "@/../naive-ui.config";
@@ -141,28 +140,22 @@ import { provideSQLResultViewContext } from "./context";
type ViewMode = "SINGLE-RESULT" | "MULTI-RESULT" | "EMPTY" | "ERROR";
-const props = defineProps({
- executeParams: {
- type: Object as PropType<SQLEditorQueryParams>,
- default: undefined,
- },
- database: {
- type: Object as PropType<ComposedDatabase>,
- default: undefined,
- },
- resultSet: {
- type: Object as PropType<SQLResultSetV1>,
- default: undefined,
- },
- loading: {
- type: Boolean,
- default: false,
- },
- dark: {
- type: Boolean,
- default: false,
- },
-});
+const props = withDefaults(
+ defineProps<{
+ executeParams?: SQLEditorQueryParams;
+ database?: ComposedDatabase;
+ resultSet?: SQLResultSetV1;
+ loading?: boolean;
+ dark?: boolean;
+ }>(),
+ {
+ executeParams: undefined,
+ database: undefined,
+ resultSet: undefined,
+ loading: false,
+ dark: false,
+ }
+);
const { t } = useI18n();
const policyStore = usePolicyV1Store();
diff --git a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/BatchQuerySelect.vue b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/BatchQuerySelect.vue
index 598682cc81..439a041ff9 100644
--- a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/BatchQuerySelect.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/BatchQuerySelect.vue
@@ -36,7 +36,7 @@
>
<RichDatabaseName :database="item.database" />
<InfoIcon
- v-if="isDatabaseQueryFailed(item.database)"
+ v-if="isDatabaseQueryFailed(item)"
class="ml-1 text-yellow-600 w-4 h-auto"
/>
<span
@@ -55,7 +55,7 @@
<script setup lang="ts">
import { useLocalStorage } from "@vueuse/core";
-import { head } from "lodash-es";
+import { head, last } from "lodash-es";
import { EyeIcon, EyeOffIcon, InfoIcon, XIcon } from "lucide-vue-next";
import { NButton, NTooltip } from "naive-ui";
import { storeToRefs } from "pinia";
@@ -93,8 +93,9 @@ const queriedDatabaseNames = computed(() =>
const items = computed(() => {
return queriedDatabaseNames.value.map<BatchQueryItem>((name) => {
const database = databaseStore.getDatabaseByName(name);
- const resultSet = tab.value?.queryContext?.results.get(name);
- return { database, resultSet };
+ // TODO(ed):
+ const result = last(tab.value?.queryContext?.results.get(name));
+ return { database, resultSet: result?.resultSet };
});
});
@@ -125,10 +126,12 @@ const showEmptySwitch = computed(() => {
return items.value.some((item) => isEmptyQueryItem(item));
});
-const isDatabaseQueryFailed = (database: ComposedDatabase) => {
- const resultSet = tab.value?.queryContext?.results.get(database.name || "");
+const isDatabaseQueryFailed = (item: BatchQueryItem) => {
// If there is any error in the result set, we consider the query failed.
- return resultSet?.error || resultSet?.results.find((result) => result.error);
+ return (
+ item.resultSet?.error ||
+ item.resultSet?.results.find((result) => result.error)
+ );
};
const handleCloseSingleResultView = (database: ComposedDatabase) => {
diff --git a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
index b032a56278..ccd3db6bdc 100644
--- a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
@@ -21,29 +21,57 @@
</div>
</template>
<template v-else>
- <BatchQuerySelect v-model:selected-database="selectedDatabase" />
- <template v-if="!selectedResultSet">
+ <BatchQuerySelect
+ v-model:selected-database="selectedDatabase"
+ class="mb-2"
+ />
+ <template v-if="selectedResults.length === 0">
<div
class="w-full h-full flex flex-col justify-center items-center text-sm"
>
<span>{{ $t("sql-editor.table-empty-placeholder") }}</span>
</div>
</template>
- <ResultViewV1
+ <NTabs
v-else
- class="w-full h-auto grow"
- :execute-params="executeParams"
- :database="selectedDatabase"
- :result-set="selectedResultSet"
- />
+ type="card"
+ size="small"
+ class="flex-1 flex flex-col overflow-hidden px-2"
+ style="--n-tab-padding: 4px 12px"
+ v-model:value="selectedTab"
+ >
+ <NTabPane
+ v-for="(result, i) in selectedResults"
+ :key="i"
+ :name="result.beginTimestampMS"
+ class="flex-1 flex flex-col overflow-hidden"
+ >
+ <template #tab>
+ <span>{{ tabName(result.beginTimestampMS) }}</span>
+ <Info
+ v-if="result.resultSet.error"
+ class="ml-2 text-yellow-600 w-4 h-auto"
+ />
+ </template>
+ <ResultViewV1
+ class="w-full h-auto grow"
+ :execute-params="result.params"
+ :database="selectedDatabase"
+ :result-set="result.resultSet"
+ />
+ </NTabPane>
+ </NTabs>
</template>
</div>
</template>
<script lang="ts" setup>
import { useTimestamp } from "@vueuse/core";
-import { NButton } from "naive-ui";
-import { computed, ref } from "vue";
+import dayjs from "dayjs";
+import { last } from "lodash-es";
+import { Info } from "lucide-vue-next";
+import { NButton, NTabs, NTabPane } from "naive-ui";
+import { computed, ref, watch } from "vue";
import { BBSpin } from "@/bbkit";
import { useSQLEditorTabStore } from "@/store";
import type { ComposedDatabase } from "@/types";
@@ -52,24 +80,44 @@ import BatchQuerySelect from "./BatchQuerySelect.vue";
const tabStore = useSQLEditorTabStore();
const selectedDatabase = ref<ComposedDatabase>();
+const selectedTab = ref<number>();
-const selectedResultSet = computed(() => {
- return tabStore.currentTab?.queryContext?.results.get(
- selectedDatabase.value?.name || ""
+const selectedResults = computed(() => {
+ return (
+ tabStore.currentTab?.queryContext?.results.get(
+ selectedDatabase.value?.name || ""
+ ) ?? []
);
});
-const executeParams = computed(() => tabStore.currentTab?.queryContext?.params);
+
+const tabName = (beginTimestampMS: number) => {
+ return dayjs(beginTimestampMS).format("YYYY-MM-DD HH:mm:ss");
+};
+
+watch(
+ () => selectedResults.value,
+ (results) => {
+ selectedTab.value = last(results)?.beginTimestampMS;
+ },
+ { deep: true }
+);
+
const loading = computed(
() => tabStore.currentTab?.queryContext?.status === "EXECUTING"
);
const currentTimestampMS = useTimestamp();
const queryElapsedTime = computed(() => {
- if (!loading.value) return "";
+ if (!loading.value) {
+ return "";
+ }
const tab = tabStore.currentTab;
- if (!tab) return "";
- const { queryContext } = tab;
- if (!queryContext) return "";
- const beginMS = queryContext.beginTimestampMS;
+ if (!tab) {
+ return "";
+ }
+ const beginMS = tabStore.currentTab?.queryContext?.beginTimestampMS;
+ if (!beginMS) {
+ return "";
+ }
const elapsedMS = currentTimestampMS.value - beginMS;
return `${(elapsedMS / 1000).toFixed(1)}s`;
});
diff --git a/frontend/src/views/sql-editor/EditorPanel/TerminalPanel/TerminalPanel.vue b/frontend/src/views/sql-editor/EditorPanel/TerminalPanel/TerminalPanel.vue
index 85b0bdadef..6c5b96e3b5 100644
--- a/frontend/src/views/sql-editor/EditorPanel/TerminalPanel/TerminalPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/TerminalPanel/TerminalPanel.vue
@@ -39,7 +39,7 @@
</Suspense>
<ResultViewV1
v-if="query.params && query.resultSet"
- class="flex-1 flex flex-col overflow-hidden max-h-80"
+ class="flex-1 flex flex-col overflow-hidden max-h-80 p-2"
:execute-params="query.params"
:result-set="query.resultSet"
:database="