From dea378014dd45bf90f51a599b4a801e0bc22059f Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Wed, 4 Aug 2021 15:52:26 +0200 Subject: [PATCH] perf: approximate table total updated on table refresh --- src/main/ipc-handlers/tables.js | 10 +++++++ src/main/libs/clients/MySQLClient.js | 13 +++++++++ src/main/libs/clients/PostgreSQLClient.js | 13 +++++++++ .../WorkspaceExploreBarTableContext.vue | 2 +- src/renderer/components/WorkspaceTableTab.vue | 28 +++++++++++++++---- src/renderer/ipc-api/Tables.js | 4 +++ 6 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/main/ipc-handlers/tables.js b/src/main/ipc-handlers/tables.js index 8c9799c2..7716d4ac 100644 --- a/src/main/ipc-handlers/tables.js +++ b/src/main/ipc-handlers/tables.js @@ -38,6 +38,16 @@ export default (connections) => { } }); + ipcMain.handle('get-table-count', async (event, params) => { + try { + const result = await connections[params.uid].getTableApproximateCount(params); + return { status: 'success', response: result }; + } + catch (err) { + return { status: 'error', response: err.toString() }; + } + }); + ipcMain.handle('get-table-indexes', async (event, params) => { try { const result = await connections[params.uid].getTableIndexes(params); diff --git a/src/main/libs/clients/MySQLClient.js b/src/main/libs/clients/MySQLClient.js index 4deb60d2..03fe2ebf 100644 --- a/src/main/libs/clients/MySQLClient.js +++ b/src/main/libs/clients/MySQLClient.js @@ -424,6 +424,19 @@ export class MySQLClient extends AntaresCore { }); } + /** + * @param {Object} params + * @param {String} params.schema + * @param {String} params.table + * @returns {Object} table row count + * @memberof MySQLClient + */ + async getTableApproximateCount ({ schema, table }) { + const { rows } = await this.raw(`SELECT table_rows "count" FROM information_schema.tables WHERE table_name = "${table}" AND table_schema = "${schema}"`); + + return rows.length ? rows[0].count : 0; + } + /** * @param {Object} params * @param {String} params.schema diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index 01cd42e8..5bc1f6e5 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -293,6 +293,19 @@ export class PostgreSQLClient extends AntaresCore { }); } + /** + * @param {Object} params + * @param {String} params.schema + * @param {String} params.table + * @returns {Object} table row count + * @memberof PostgreSQLClient + */ + async getTableApproximateCount ({ schema, table }) { + const { rows } = await this.raw(`SELECT reltuples AS count FROM pg_class WHERE relname = '${table}'`); + + return rows.length ? rows[0].count : 0; + } + /** * @param {Object} params * @param {String} params.schema diff --git a/src/renderer/components/WorkspaceExploreBarTableContext.vue b/src/renderer/components/WorkspaceExploreBarTableContext.vue index 06a22389..dff44ef7 100644 --- a/src/renderer/components/WorkspaceExploreBarTableContext.vue +++ b/src/renderer/components/WorkspaceExploreBarTableContext.vue @@ -104,7 +104,7 @@ export default { return this.getWorkspace(this.selectedWorkspace); }, customizations () { - return this.workspace ? this.workspace.customizations : {}; + return this.workspace && this.workspace.customizations ? this.workspace.customizations : {}; } }, methods: { diff --git a/src/renderer/components/WorkspaceTableTab.vue b/src/renderer/components/WorkspaceTableTab.vue index 63e03e21..cb4f0f9d 100644 --- a/src/renderer/components/WorkspaceTableTab.vue +++ b/src/renderer/components/WorkspaceTableTab.vue @@ -112,8 +112,8 @@
{{ $t('word.results') }}: {{ results[0].rows.length | localeString }}
-
- {{ $t('word.total') }}: {{ tableInfo.rows | localeString }} ({{ $t('word.approximately') }}) +
+ {{ $t('word.total') }}: ≈ {{ approximateCount | localeString }}
{{ schema }} @@ -175,7 +175,7 @@ export default { }, filters: { localeString (val) { - if (val) + if (val !== null) return val.toLocaleString(); } }, @@ -200,7 +200,8 @@ export default { refreshInterval: null, sortParams: {}, page: 1, - pageProxy: 1 + pageProxy: 1, + approximateCount: 0 }; }, computed: { @@ -232,15 +233,15 @@ export default { hasApproximately () { return this.results.length && this.results[0].rows && - this.tableInfo && this.results[0].rows.length === this.limit && - this.results[0].rows.length < this.tableInfo.rows; + this.results[0].rows.length < this.approximateCount; } }, watch: { schema () { if (this.isSelected) { this.page = 1; + this.approximateCount = 0; this.sortParams = {}; this.getTableData(); this.lastTable = this.table; @@ -250,6 +251,7 @@ export default { table () { if (this.isSelected) { this.page = 1; + this.approximateCount = 0; this.sortParams = {}; this.getTableData(); this.lastTable = this.table; @@ -315,6 +317,20 @@ export default { this.addNotification({ status: 'error', message: err.stack }); } + if (this.results.length && this.results[0].rows.length === this.limit) { + try { // Table approximate count + const { status, response } = await Tables.getTableApproximateCount(params); + + if (status === 'success') + this.approximateCount = response; + else + this.addNotification({ status: 'error', message: response }); + } + catch (err) { + this.addNotification({ status: 'error', message: err.stack }); + } + } + this.isQuering = false; }, getTable () { diff --git a/src/renderer/ipc-api/Tables.js b/src/renderer/ipc-api/Tables.js index 25dd8d27..56e93328 100644 --- a/src/renderer/ipc-api/Tables.js +++ b/src/renderer/ipc-api/Tables.js @@ -10,6 +10,10 @@ export default class { return ipcRenderer.invoke('get-table-data', params); } + static getTableApproximateCount (params) { + return ipcRenderer.invoke('get-table-count', params); + } + static getTableIndexes (params) { return ipcRenderer.invoke('get-table-indexes', params); }