Skip to content

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

perf: sql editor (part3) (#17242)

ecmadao7366578dd1b8
diff --git a/frontend/package.json b/frontend/package.json
index 485a31c24a..906a40ccf9 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -29,6 +29,7 @@
"@markdoc/markdoc": "0.5.2",
"@soerenmartius/vue3-clipboard": "0.1.2",
"@tanstack/vue-table": "8.21.3",
+ "@tanstack/vue-virtual": "^3.13.12",
"@vueuse/core": "12.8.2",
"bootstrap": "5.3.6",
"crypto-js": "^4.2.0",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index af00273a00..0d8083d70e 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -49,6 +49,9 @@ importers:
'@tanstack/vue-table':
specifier: 8.21.3
+ '@tanstack/vue-virtual':
+ specifier: ^3.13.12
'@vueuse/core':
specifier: 12.8.2
version: 12.8.2([email protected])
@@ -2235,12 +2238,20 @@ packages:
resolution: {integrity: sha512-ldZXEhOBb8Is7xLs01fR3YEc3DERiz5silj8tnGkFZytt1abEvl/GhUmCE0PMLaMPTa3Jk4HbKmRlHmu+gCftg==}
engines: {node: '>=12'}
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-1YBOJfRHV4sXUmWsFSf5rQor4Ss82G8dQWLRbnk3GA4jeP8hQt1hxXh0tmflpC0dz3VgEv/1+qwPyLeWkQuPFA==}
+
'@tanstack/[email protected]':
resolution: {integrity: sha512-rusRyd77c5tDPloPskctMyPLFEQUeBzxdQ+2Eow4F7gDPlPOB1UnnhzfpdvqZ8ZyX2rRNGmqNnQWm87OI2OQPw==}
engines: {node: '>=12'}
peerDependencies:
vue: '>=3.2'
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-vhF7kEU9EXWXh+HdAwKJ2m3xaOnTTmgcdXcF2pim8g4GvI7eRrk2YRuV5nUlZnd/NbCIX4/Ja2OZu5EjJL06Ww==}
+ peerDependencies:
+ vue: ^2.7.0 || ^3.0.0
+
'@trivago/[email protected]':
resolution: {integrity: sha512-fYDQA9e6yTNmA13TLVSA+WMQRc5Bn/c0EUBditUHNfMMxN7M82c38b1kEggVE3pLpZ0FwkwJkUEKMiOi52JXFA==}
engines: {node: '>18.12'}
@@ -8104,11 +8115,18 @@ snapshots:
'@tanstack/[email protected]': {}
+ '@tanstack/[email protected]': {}
+
dependencies:
'@tanstack/table-core': 8.21.3
vue: 3.5.18([email protected])
+ dependencies:
+ '@tanstack/virtual-core': 3.13.12
+ vue: 3.5.18([email protected])
+
dependencies:
'@babel/generator': 7.27.3
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataBlock.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataBlock.vue
deleted file mode 100644
index 1647b20269..0000000000
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataBlock.vue
+++ /dev/null
@@ -1,83 +0,0 @@
-<template>
- <NScrollbar ref="scrollbarRef" class="space-y-5 m-2">
- <div
- v-for="(row, rowIndex) of rows"
- :key="`rows-${rowIndex + offset}`"
- class="font-mono"
- >
- <p
- class="font-medium text-gray-500 dark:text-gray-300 overflow-hidden whitespace-nowrap"
- >
- ******************************** {{ rowIndex + offset + 1 }}. row
- ********************************
- </p>
- <div class="py-2 px-3 bg-gray-50 dark:bg-gray-700 rounded-sm">
- <div
- v-for="header in table.getFlatHeaders()"
- :key="header.index"
- class="flex items-center text-gray-500 dark:text-gray-300 text-sm"
- >
- <div class="min-w-[7rem] text-left flex items-center font-medium">
- {{ header.column.columnDef.header }}
- <MaskingReasonPopover
- v-if="getMaskingReason && getMaskingReason(header.index)"
- :reason="getMaskingReason(header.index)"
- class="ml-0.5 shrink-0"
- />
- <SensitiveDataIcon
- v-else-if="isSensitiveColumn(header.index)"
- class="ml-0.5 shrink-0"
- />
- :
- </div>
- <div class="flex-1">
- <TableCell
- :table="table"
- :value="
- row.getVisibleCells()[header.index].getValue() as RowValue
- "
- :keyword="keyword"
- :set-index="setIndex"
- :row-index="offset + rowIndex"
- :col-index="header.index"
- :column-type="getColumnType(header)"
- />
- </div>
- </div>
- </div>
- </div>
- </NScrollbar>
- <div v-if="rows.length === 0" class="text-center w-full my-12 textinfolabel">
- {{ $t("sql-editor.no-data-available") }}
- </div>
-</template>
-
-<script setup lang="ts">
-import type { Table } from "@tanstack/vue-table";
-import { NScrollbar } from "naive-ui";
-import { computed, watch, ref } from "vue";
-import type { QueryRow, RowValue } from "@/types/proto-es/v1/sql_service_pb";
-import TableCell from "./DataTable/TableCell.vue";
-import MaskingReasonPopover from "./DataTable/common/MaskingReasonPopover.vue";
-import SensitiveDataIcon from "./DataTable/common/SensitiveDataIcon.vue";
-import { getColumnType } from "./DataTable/common/utils";
-import { useSQLResultViewContext } from "./context";
-
-const props = defineProps<{
- table: Table<QueryRow>;
- setIndex: number;
- offset: number;
- isSensitiveColumn: (index: number) => boolean;
- getMaskingReason?: (index: number) => any;
-}>();
-
-const { keyword } = useSQLResultViewContext();
-const scrollbarRef = ref<InstanceType<typeof NScrollbar>>();
-
-const rows = computed(() => props.table.getRowModel().rows);
-
-watch(
- () => props.offset,
- () => scrollbarRef.value?.scrollTo({ top: 0 })
-);
-</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
index a47657945a..69042b8536 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/TableCell.vue
@@ -1,7 +1,7 @@
<!-- eslint-disable vue/no-v-html -->
<template>
<div
- class="relative px-2 py-1"
+ class="px-2 py-1 flex items-center"
:class="classes"
@click="handleClick"
@dblclick="showDetail"
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/VirtualDataTable.vue
similarity index 55%
rename from frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
rename to frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/VirtualDataTable.vue
index 0bea73e5e4..d585bcca9c 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/DataTable.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/VirtualDataTable.vue
@@ -8,7 +8,7 @@
>
<table
ref="tableRef"
- class="relative border-collapse w-full table-auto -mx-px"
+ class="relative border-collapse w-full -mx-px table-fixed"
v-bind="tableResize.getTableProps()"
>
<thead
@@ -88,41 +88,98 @@
</tr>
</thead>
<tbody>
- <tr
- v-for="(row, rowIndex) of rows"
- :key="`${setIndex}-${rowIndex}`"
- class="group"
- :data-row-index="offset + rowIndex"
- >
- <td
- v-for="(cell, columnIndex) of row.getVisibleCells()"
- :key="`${setIndex}-${rowIndex}-${columnIndex}`"
- class="relative max-w-[50vw] p-0 text-sm dark:text-gray-100 leading-5 whitespace-nowrap break-all border-x border-b border-block-border dark:border-zinc-500 group-even:bg-gray-100/50 dark:group-even:bg-gray-700/50"
- :data-col-index="columnIndex"
- >
- <TableCell
- :table="table"
- :value="cell.getValue<RowValue>()"
- :keyword="keyword"
- :set-index="setIndex"
- :row-index="offset + rowIndex"
- :col-index="columnIndex"
- :allow-select="true"
- :column-type="getColumnTypeByIndex(columnIndex)"
- :class="{
- 'ml-3': columnIndex === 0 && !selectionDisabled,
- }"
- />
+ <!-- Virtual scrolling container -->
+ <tr>
+ <td :colspan="columns.length" class="!p-0 !border-0">
<div
- v-if="columnIndex === 0 && !selectionDisabled"
- class="absolute inset-y-0 left-0 w-3 cursor-pointer bg-accent/5 dark:bg-white/10 hover:bg-accent/10 dark:hover:bg-accent/40"
- :class="{
- '!bg-accent/10 dark:!bg-accent/40':
- selectionState.columns.length === 0 &&
- selectionState.rows.includes(offset + rowIndex),
+ ref="scrollerRef"
+ class="relative w-full"
+ :style="{
+ height: `${virtualizer.getTotalSize()}px`,
}"
- @click.prevent.stop="selectRow(offset + rowIndex)"
- ></div>
+ >
+ <div
+ class="absolute top-0 left-0 w-full"
+ :style="{
+ transform: `translateY(${virtualItems[0]?.start ?? 0}px)`,
+ }"
+ >
+ <table
+ class="w-full border-collapse table-fixed"
+ :style="{ tableLayout: 'fixed' }"
+ >
+ <colgroup>
+ <col
+ v-for="(_, index) in columns"
+ :key="`col-${index}`"
+ v-bind="tableResize.getColumnProps(index)"
+ />
+ </colgroup>
+ <tbody>
+ <tr
+ v-for="virtualRow in virtualItems"
+ :key="`${setIndex}-${virtualRow.index}`"
+ class="group"
+ :data-row-index="offset + virtualRow.index"
+ :style="{
+ height: `${virtualRow.size}px`,
+ }"
+ >
+ <td
+ v-for="(cell, columnIndex) of rows[
+ virtualRow.index
+ ].getVisibleCells()"
+ :key="`${setIndex}-${virtualRow.index}-${columnIndex}`"
+ class="relative max-w-[50vw] !p-0 text-sm dark:text-gray-100 leading-5 whitespace-nowrap break-all border-x border-b border-block-border dark:border-zinc-500 group-even:bg-gray-100/50 dark:group-even:bg-gray-700/50"
+ :data-col-index="columnIndex"
+ :style="{
+ height: `${virtualRow.size}px`,
+ }"
+ >
+ <div
+ :class="[
+ 'h-full flex items-center',
+ columnIndex === 0 && !selectionDisabled
+ ? 'ml-3'
+ : '',
+ ]"
+ >
+ <TableCell
+ :table="table"
+ :value="cell.getValue<RowValue>()"
+ :keyword="keyword"
+ :set-index="setIndex"
+ :row-index="offset + virtualRow.index"
+ :col-index="columnIndex"
+ :allow-select="true"
+ :column-type="getColumnTypeByIndex(columnIndex)"
+ :class="{
+ // 'ml-3': columnIndex === 0 && !selectionDisabled,
+ 'h-full': true,
+ 'w-full': true,
+ }"
+ />
+ </div>
+ <div
+ v-if="columnIndex === 0 && !selectionDisabled"
+ class="absolute inset-y-0 left-0 w-3 cursor-pointer bg-accent/5 dark:bg-white/10 hover:bg-accent/10 dark:hover:bg-accent/40"
+ :class="{
+ '!bg-accent/10 dark:!bg-accent/40':
+ selectionState.columns.length === 0 &&
+ selectionState.rows.includes(
+ offset + virtualRow.index
+ ),
+ }"
+ @click.prevent.stop="
+ selectRow(offset + virtualRow.index)
+ "
+ ></div>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ </div>
</td>
</tr>
</tbody>
@@ -138,6 +195,7 @@
<script lang="ts" setup>
import type { Table } from "@tanstack/vue-table";
+import { useVirtualizer } from "@tanstack/vue-virtual";
import { NEmpty } from "naive-ui";
import { computed, nextTick, onMounted, ref, watch } from "vue";
import {
@@ -176,6 +234,7 @@ const {
} = useSelectionContext();
const containerRef = ref<HTMLDivElement>();
const tableRef = ref<HTMLTableElement>();
+const scrollerRef = ref<HTMLDivElement>();
const tableResize = useTableColumnWidthLogic({
tableRef,
@@ -214,12 +273,34 @@ const existBinaryValue = (columnIndex: number) => {
return false;
};
+// Virtual scrolling setup
+const virtualizer = useVirtualizer(
+ computed(() => ({
+ count: rows.value.length,
+ getScrollElement: () => containerRef.value ?? null,
+ estimateSize: () => 35, // Estimated row height
+ overscan: 10, // Number of items to render outside visible area
+ }))
+);
+
+const virtualItems = computed(() => virtualizer.value.getVirtualItems());
+
onMounted(() => {
nextTick(() => {
tableResize.reset();
});
});
+// Re-initialize column widths when data changes
+watch(
+ () => [rows.value.length, columns.value.length],
+ () => {
+ nextTick(() => {
+ tableResize.reset();
+ });
+ }
+);
+
const scrollTo = (x: number, y: number) => {
containerRef.value?.scroll(x, y);
};
@@ -229,6 +310,7 @@ watch(
() => {
// When the offset changed, we need to reset the scroll position.
scrollTo(0, 0);
+ virtualizer.value.scrollToOffset(0);
}
);
</script>
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/index.ts b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/index.ts
index 5e544303ab..ca1fe87a6a 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/index.ts
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/DataTable/index.ts
@@ -1,3 +1,3 @@
-import DataTable from "./DataTable.vue";
+import VirtualDataTable from "./VirtualDataTable.vue";
-export default DataTable;
+export default VirtualDataTable;
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
index 39017617b5..15bfd27bbe 100644
--- a/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/SingleResultViewV1.vue
@@ -122,7 +122,7 @@
</div>
<div class="flex-1 w-full flex flex-col overflow-y-auto">
- <DataBlock
+ <VirtualDataBlock
v-if="state.vertical"
:table="table"
:set-index="setIndex"
@@ -130,7 +130,7 @@
:is-sensitive-column="isSensitiveColumn"
:get-masking-reason="getMaskingReason"
/>
- <DataTable
+ <VirtualDataTable
v-else
:table="table"
:set-index="setIndex"
@@ -258,12 +258,12 @@ import {
instanceV1HasStructuredQueryResult,
isNullOrUndefined,
} from "@/utils";
-import DataBlock from "./DataBlock.vue";
-import DataTable from "./DataTable";
+import VirtualDataTable from "./DataTable/VirtualDataTable.vue";
import { provideSelectionContext } from "./DataTable/common/selection-logic";
import EmptyView from "./EmptyView.vue";
import ErrorView from "./ErrorView";
import SelectionCopyTooltips from "./SelectionCopyTooltips.vue";
+import VirtualDataBlock from "./VirtualDataBlock.vue";
import { useSQLResultViewContext } from "./context";
// Using conversion function from common-conversions.ts
diff --git a/frontend/src/views/sql-editor/EditorCommon/ResultView/VirtualDataBlock.vue b/frontend/src/views/sql-editor/EditorCommon/ResultView/VirtualDataBlock.vue
new file mode 100644
index 0000000000..ee569f8ff0
--- /dev/null
+++ b/frontend/src/views/sql-editor/EditorCommon/ResultView/VirtualDataBlock.vue
@@ -0,0 +1,133 @@
+<template>
+ <div ref="containerRef" class="h-full overflow-auto">
+ <div
+ class="relative"
+ :style="{
+ height: `${virtualizer.getTotalSize()}px`,
+ }"
+ >
+ <div
+ class="absolute top-0 left-0 w-full"
+ :style="{
+ transform: `translateY(${virtualItems[0]?.start ?? 0}px)`,
+ }"
+ >
+ <div
+ v-for="virtualRow in virtualItems"
+ :key="`row-${offset + virtualRow.index}`"
+ class="font-mono mb-5 mx-2"
+ :style="{
+ height: `${virtualRow.size}px`,
+ }"
+ >
+ <p
+ class="font-medium text-gray-500 dark:text-gray-300 overflow-hidden whitespace-nowrap"
+ >
+ ********************************
+ {{ offset + virtualRow.index + 1 }}. row
+ ********************************
+ </p>
+ <div class="py-2 px-3 bg-gray-50 dark:bg-gray-700 rounded-sm">
+ <div
+ v-for="header in columnHeaders"
+ :key="`${virtualRow.index}-${header.index}`"
+ class="flex items-center text-gray-500 dark:text-gray-300 text-sm"
+ >
+ <div class="min-w-[7rem] text-left flex items-center font-medium">
+ {{ header.column.columnDef.header }}
+ <MaskingReasonPopover
+ v-if="getMaskingReason && getMaskingReason(header.index)"
+ :reason="getMaskingReason(header.index)"
+ class="ml-0.5 shrink-0"
+ />
+ <SensitiveDataIcon
+ v-else-if="isSensitiveColumn(header.index)"
+ class="ml-0.5 shrink-0"
+ />
+ :
+ </div>
+ <div class="flex-1">
+ <TableCell
+ :table="table"
+ :value="
+ rows[virtualRow.index]
+ .getVisibleCells()
+ [header.index].getValue() as RowValue
+ "
+ :keyword="keyword"
+ :set-index="setIndex"
+ :row-index="offset + virtualRow.index"
+ :col-index="header.index"
+ :column-type="getColumnType(header)"
+ />
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div
+ v-if="rows.length === 0"
+ class="text-center w-full my-12 textinfolabel"
+ >
+ {{ $t("sql-editor.no-data-available") }}
+ </div>
+ </div>
+</template>
+
+<script setup lang="ts">
+import type { Table } from "@tanstack/vue-table";
+import { useVirtualizer } from "@tanstack/vue-virtual";
+import { computed, ref, watch } from "vue";
+import type { QueryRow, RowValue } from "@/types/proto-es/v1/sql_service_pb";
+import TableCell from "./DataTable/TableCell.vue";
+import MaskingReasonPopover from "./DataTable/common/MaskingReasonPopover.vue";
+import SensitiveDataIcon from "./DataTable/common/SensitiveDataIcon.vue";
+import { getColumnType } from "./DataTable/common/utils";
+import { useSQLResultViewContext } from "./context";
+
+const props = defineProps<{
+ table: Table<QueryRow>;
+ setIndex: number;
+ offset: number;
+ isSensitiveColumn: (index: number) => boolean;
+ getMaskingReason?: (index: number) => any;
+}>();
+
+const { keyword } = useSQLResultViewContext();
+const containerRef = ref<HTMLDivElement>();
+
+const rows = computed(() => props.table.getRowModel().rows);
+
+// Cache column headers to avoid recalculation
+const columnHeaders = computed(() => props.table.getFlatHeaders());
+
+// Calculate the height of each row block
+const estimateRowHeight = () => {
+ // Estimate based on number of columns
+ // Each column is roughly 28px, plus padding and header
+ const columnCount = columnHeaders.value.length;
+ return 60 + columnCount * 28; // Header + columns
+};
+
+// Virtual scrolling setup
+const virtualizer = useVirtualizer(
+ computed(() => ({
+ count: rows.value.length,
+ getScrollElement: () => containerRef.value ?? null,
+ estimateSize: estimateRowHeight,
+ overscan: 3, // Fewer items in overscan since each item is larger
+ }))
+);
+
+const virtualItems = computed(() => virtualizer.value.getVirtualItems());
+
+// Reset scroll position when offset changes
+watch(
+ () => props.offset,
+ () => {
+ containerRef.value?.scrollTo({ top: 0 });
+ virtualizer.value.scrollToOffset(0);
+ }
+);
+</script>
diff --git a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/DatabaseQueryContext.vue b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/DatabaseQueryContext.vue
index e953ad2707..1f0b8fd8d3 100644
--- a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/DatabaseQueryContext.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/DatabaseQueryContext.vue
@@ -38,9 +38,9 @@
</template>
<script lang="tsx" setup>
-import { useTimestamp } from "@vueuse/core";
+import { useIntervalFn } from "@vueuse/core";
import { NButton } from "naive-ui";
-import { computed, watch } from "vue";
+import { computed, ref, watch } from "vue";
import { BBSpin } from "@/bbkit";
import { useExecuteSQL } from "@/composables/useExecuteSQL";
import { useSQLEditorTabStore } from "@/store";
@@ -61,7 +61,26 @@ const tabStore = useSQLEditorTabStore();
const loading = computed(() => props.context.status === "EXECUTING");
-const currentTimestampMS = useTimestamp();
+// Update every 1 second instead of every frame
+const currentTimestampMS = ref(Date.now());
+const { pause, resume } = useIntervalFn(() => {
+ currentTimestampMS.value = Date.now();
+}, 1000);
+
+// Start/stop the timer based on loading state
+watch(
+ loading,
+ (isLoading) => {
+ if (isLoading) {
+ currentTimestampMS.value = Date.now();
+ resume();
+ } else {
+ pause();
+ }
+ },
+ { immediate: true }
+);
+
const queryElapsedTime = computed(() => {
if (!loading.value) {
return "";
diff --git a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
index 0226c656e3..4d8e921af1 100644
--- a/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/ResultPanel/ResultPanel.vue
@@ -14,7 +14,7 @@
>
<NTabPane
v-for="(context, i) in queryContexts"
- :key="i"
+ :key="context.id"
:name="context.id"
class="flex-1 flex flex-col overflow-hidden"
>
@@ -34,7 +34,7 @@
/>
<BBSpin v-if="context.status === 'EXECUTING'" :size="10" />
<XIcon
- v-if="queryContexts.length > 1"
+ v-if="hasMultipleContexts"
class="text-gray-400 w-4 h-auto hover:text-gray-600"
@click.stop="handleCloseTab(context.id)"
/>
@@ -44,7 +44,9 @@
</NTooltip>
</template>
<BBAttention
- v-if="i === 0 && !isMatchedDataSource(context)"
+ v-if="
+ i === 0 && batchModeDataSourceType && !isMatchedDataSource(context)
+ "
type="warning"
class="mb-2"
>
@@ -87,7 +89,6 @@
<script lang="ts" setup>
import dayjs from "dayjs";
-import { head } from "lodash-es";
import { CircleAlertIcon, XIcon } from "lucide-vue-next";
import type { DropdownOption } from "naive-ui";
import { NTabs, NTabPane, NTooltip, NDropdown } from "naive-ui";
@@ -195,42 +196,75 @@ const handleContextMenuClose = () => {
contextMenuState.value = undefined;
};
-const isBatchQuery = computed(
- () =>
- Array.from(tabStore.currentTab?.databaseQueryContexts?.keys() || [])
- .length > 1
-);
+// Cache the batch query state to avoid recreating arrays
+const isBatchQuery = computed(() => {
+ const contexts = tabStore.currentTab?.databaseQueryContexts;
+ if (!contexts) return false;
+ return contexts.size > 1;
+});
+// Memoize query contexts to avoid multiple accesses
const queryContexts = computed(() => {
- const contexts = tabStore.currentTab?.databaseQueryContexts?.get(
- selectedDatabase.value?.name ?? ""
+ if (!selectedDatabase.value?.name) return undefined;
+ return tabStore.currentTab?.databaseQueryContexts?.get(
+ selectedDatabase.value.name
);
- return contexts;
});
-const tabName = (context: SQLEditorDatabaseQueryContext) => {
- switch (context.status) {
- case "PENDING":
- return t("sql-editor.pending-query");
- case "EXECUTING":
- return t("sql-editor.executing-query");
- default:
- return dayjs(context.beginTimestampMS).format("YYYY-MM-DD HH:mm:ss");
+// Cache whether we have multiple contexts for close button visibility
+const hasMultipleContexts = computed(() => {
+ return (queryContexts.value?.length ?? 0) > 1;
+});
+
+// Memoize tab names to avoid recalculation
+const tabNamesMap = computed(() => {
+ const map = new Map<string, string>();
+ if (!queryContexts.value) return map;
+
+ for (const context of queryContexts.value) {
+ let name: string;
+ switch (context.status) {
+ case "PENDING":
+ name = t("sql-editor.pending-query");
+ break;
+ case "EXECUTING":
+ name = t("sql-editor.executing-query");
+ break;
+ default:
+ name = dayjs(context.beginTimestampMS).format("YYYY-MM-DD HH:mm:ss");
+ }
+ map.set(context.id, name);
}
+ return map;
+});
+
+const tabName = (context: SQLEditorDatabaseQueryContext) => {
+ return tabNamesMap.value.get(context.id) ?? "";
};
+// Cache data sources for better performance
+const dataSourcesMap = computed(() => {
+ if (!selectedDatabase.value?.instanceResource.dataSources) {
+ return new Map();
+ }
+ return new Map(
+ selectedDatabase.value.instanceResource.dataSources.map((ds) => [ds.id, ds])
+ );
+});
+
const dataSourceInContext = (context: SQLEditorDatabaseQueryContext) => {
const dataSourceId = context.params.connection.dataSourceId;
- return selectedDatabase.value?.instanceResource.dataSources.find(
- (ds) => ds.id === dataSourceId
- );
+ return dataSourcesMap.value.get(dataSourceId);
};
+// Memoize batch mode check
+const batchModeDataSourceType = computed(() => {
+ if (!tabStore.isInBatchMode) return null;
+ return tabStore.currentTab?.batchQueryContext?.dataSourceType ?? null;
+});
+
const isMatchedDataSource = (context: SQLEditorDatabaseQueryContext) => {
- if (!tabStore.isInBatchMode) {
- return true;
- }
- const mode = tabStore.currentTab?.batchQueryContext?.dataSourceType;
+ const mode = batchModeDataSourceType.value;
if (!mode) {
return true;
}
@@ -238,12 +272,12 @@ const isMatchedDataSource = (context: SQLEditorDatabaseQueryContext) => {
if (!dataSource) {
return true;
}
-
return dataSource.type === mode;
};
+// Only watch the first item's ID to minimize reactive dependencies
watch(
- () => head(queryContexts.value)?.id,
+ () => queryContexts.value?.[0]?.id,
(id) => {
selectedTab.value = id;
},