Skip to content

Commit

Permalink
perf: approximate table total updated on table refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Aug 4, 2021
1 parent 3abff36 commit dea3780
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/main/ipc-handlers/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/main/libs/clients/MySQLClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/main/libs/clients/PostgreSQLClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
28 changes: 22 additions & 6 deletions src/renderer/components/WorkspaceTableTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@
<div v-if="results.length && results[0].rows">
{{ $t('word.results') }}: <b>{{ results[0].rows.length | localeString }}</b>
</div>
<div v-if="hasApproximately || (page > 1 && tableInfo.rows)">
{{ $t('word.total') }}: <b>{{ tableInfo.rows | localeString }}</b> <small>({{ $t('word.approximately') }})</small>
<div v-if="hasApproximately || (page > 1 && approximateCount)">
{{ $t('word.total') }}: <b :title="$t('word.approximately')">≈ {{ approximateCount | localeString }}</b>
</div>
<div class="d-flex" :title="$t('word.schema')">
<i class="mdi mdi-18px mdi-database mr-1" /><b>{{ schema }}</b>
Expand Down Expand Up @@ -175,7 +175,7 @@ export default {
},
filters: {
localeString (val) {
if (val)
if (val !== null)
return val.toLocaleString();
}
},
Expand All @@ -200,7 +200,8 @@ export default {
refreshInterval: null,
sortParams: {},
page: 1,
pageProxy: 1
pageProxy: 1,
approximateCount: 0
};
},
computed: {
Expand Down Expand Up @@ -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;
Expand All @@ -250,6 +251,7 @@ export default {
table () {
if (this.isSelected) {
this.page = 1;
this.approximateCount = 0;
this.sortParams = {};
this.getTableData();
this.lastTable = this.table;
Expand Down Expand Up @@ -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 () {
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/ipc-api/Tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit dea3780

Please sign in to comment.