perf(export): optimize SQL export performance

This commit is contained in:
miracle authored and GitHub committed 2026-08-05 14:57:06 +08:00
1 parent 7fd4aef060
commit 605d20dd4f
14 files changed
+665 -293

No files matched your search

@@ -10,7 +10,6 @@ import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { copyToClipboard } from "@/lib/common/clipboard";
import { effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
import { joinExportedDdls } from "@/lib/export/ddlExport";
import { formatSqlInsert } from "@/lib/export/exportFormats";
import { translateBackendError } from "@/i18n/backend-errors";
import { sidebarStructureExportTargets } from "@/lib/sidebar/sidebarExportRuntime";
import { fetchTableDataForExport } from "@/lib/table/tableDataExport";
@@ -28,7 +27,7 @@ interface SidebarTreeExportRuntimeOptions {
export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOptions) {
const { t } = useI18n();
const { toast } = useToast();
const { addTask: addExportTask } = useExportTracker();
const { addTask: addExportTask, updateTableExportTask } = useExportTracker();
const { activeNode, connectionStore, settingsStore } = options;
async function saveFileContent(content: string, defaultFileName: string, filterName: string, filterExt: string) {
@@ -195,12 +194,7 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
}
}
function columnTypesForResultColumns(columns: string[], tableColumns: ColumnInfo[]): Array<string | undefined> {
const typesByName = new Map(tableColumns.map((column) => [column.name.toLocaleLowerCase(), column.data_type]));
return columns.map((column) => typesByName.get(column.toLocaleLowerCase()));
}
async function exportDataLegacy(format: "json" | "sql") {
async function exportDataLegacy(format: "json") {
const node = activeNode.value;
if (!node.connectionId || !node.database) return;
const connectionId = node.connectionId;
@@ -210,8 +204,7 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
try {
await connectionStore.ensureConnected(connectionId);
const tableColumns = format === "sql" ? await api.getColumns(connectionId, database, node.schema || database, node.label) : undefined;
const queryColumns = config.db_type === "neo4j" ? (tableColumns ?? (await api.getColumns(connectionId, database, node.schema || database, node.label))).map((column) => column.name) : undefined;
const queryColumns = config.db_type === "neo4j" ? (await api.getColumns(connectionId, database, node.schema || database, node.label)).map((column) => column.name) : undefined;
const effectiveDbType = effectiveDatabaseTypeForConnection(config);
const result = await fetchTableDataForExport({
databaseType: effectiveDbType,
@@ -233,25 +226,13 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
}
await api.exportQueryResultJson(outputPath, result.columns, result.rows);
toast(t("grid.exported"));
return;
}
const content = await formatSqlInsert({
databaseType: effectiveDbType,
schema: node.schema,
tableName: node.label,
columns: result.columns,
columnTypes: tableColumns ? columnTypesForResultColumns(result.columns, tableColumns) : undefined,
rows: result.rows,
});
await saveFileContent(content, `${node.label}.sql`, "SQL", "sql");
toast(t("grid.exported"));
} catch (error: any) {
toast(t("grid.exportFailed", { message: translateBackendError(t, error) }), 5000);
}
}
async function exportTableData(format: "csv" | "xlsx") {
async function exportTableData(format: "csv" | "xlsx" | "sql") {
const node = activeNode.value;
if (!node.connectionId || !node.database) return;
const connectionId = node.connectionId;
@@ -261,20 +242,20 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
let task: ExportTask | null = null;
try {
await connectionStore.ensureConnected(connectionId);
// Choose the destination before registering a background task so cancellation creates no orphan tracker entry.
let outputPath = `${node.label}.${format}`;
if (isTauriRuntime()) {
const { save } = await import("@tauri-apps/plugin-dialog");
const filterName = format === "csv" ? "CSV" : format === "xlsx" ? "Excel" : "SQL";
const path = await save({
defaultPath: outputPath,
filters: [{ name: format === "csv" ? "CSV" : "Excel", extensions: [format] }],
filters: [{ name: filterName, extensions: [format] }],
});
if (!path) return;
outputPath = path as string;
}
await connectionStore.ensureConnected(connectionId);
task = addExportTask(node.label, format, outputPath);
const currentTask = task;
const queryColumns = config.db_type === "neo4j" ? (await api.getColumns(connectionId, database, node.schema || database, node.label)).map((column) => column.name) : undefined;
@@ -290,14 +271,12 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
format,
columns: queryColumns,
batchSize: settingsStore.editorSettings.exportBatchSize,
skipCount: format === "sql",
rowLimit,
};
await api.startTableExport(request, (progress) => {
currentTask.rowsExported = progress.rowsExported;
currentTask.totalRows = progress.totalRows;
currentTask.status = progress.status;
currentTask.errorMessage = progress.errorMessage || null;
updateTableExportTask(currentTask.exportId, progress);
if (progress.status === "Done") toast(t("grid.exported"));
else if (progress.status === "Error") toast(t("grid.exportFailed", { message: translateBackendError(t, progress.errorMessage || "") }), 5000);
});
@@ -311,8 +290,8 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
}
async function exportData(format: "csv" | "json" | "sql") {
if (format === "csv") await exportTableData("csv");
else await exportDataLegacy(format);
if (format === "json") await exportDataLegacy(format);
else await exportTableData(format);
}
async function exportDataXlsx() {