From 1a7d34dc2083f6f12a08b35167fcb30a267e62da Mon Sep 17 00:00:00 2001 From: Rishichandra Wawhal Date: Wed, 6 May 2020 11:25:04 +0530 Subject: [PATCH 1/4] in table-browse-rows, make intelligent count --- .../src/components/Services/Data/DataState.js | 2 ++ .../Data/TableBrowseRows/ViewActions.js | 24 +++++++++++++++---- .../Data/TableBrowseRows/ViewTable.js | 4 +++- .../Services/Data/TableCommon/TableHeader.js | 6 ++++- .../src/components/Services/Data/constants.js | 2 ++ 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/console/src/components/Services/Data/DataState.js b/console/src/components/Services/Data/DataState.js index 9f232887390ee..943dac4ad10dd 100644 --- a/console/src/components/Services/Data/DataState.js +++ b/console/src/components/Services/Data/DataState.js @@ -22,6 +22,8 @@ const defaultViewState = { manualTriggers: [], triggeredRow: -1, triggeredFunction: null, + estimatedCount: 0, + isCountEstimated: 0, }; const defaultPermissionsState = { diff --git a/console/src/components/Services/Data/TableBrowseRows/ViewActions.js b/console/src/components/Services/Data/TableBrowseRows/ViewActions.js index fa1f540f7149b..9eeafa80c5fcc 100644 --- a/console/src/components/Services/Data/TableBrowseRows/ViewActions.js +++ b/console/src/components/Services/Data/TableBrowseRows/ViewActions.js @@ -17,6 +17,7 @@ import { getRunSqlQuery, } from '../../../Common/utils/v1QueryUtils'; import { generateTableDef } from '../../../Common/utils/pgUtils'; +import { COUNT_LIMIT } from '../constants'; /* ****************** View actions *************/ const V_SET_DEFAULTS = 'ViewTable/V_SET_DEFAULTS'; @@ -157,9 +158,20 @@ const vMakeCountRequest = () => { }; }; -const vMakeTableRequests = () => dispatch => { - dispatch(vMakeRowsRequest()); - dispatch(vMakeCountRequest()); +const vMakeTableRequests = () => (dispatch, getState) => { + dispatch(vMakeRowsRequest()).then(() => { + const estimatedCountResult = getState().tables.view.estimatedCount[0]; + const estimatedCount = parseInt(estimatedCountResult, 10); + if (estimatedCount > COUNT_LIMIT) { + dispatch({ + type: V_COUNT_REQUEST_SUCCESS, + count: estimatedCount, + isEstimated: true, + }); + } else { + dispatch(vMakeCountRequest()); + } + }); }; const fetchManualTriggers = tableName => { @@ -572,7 +584,11 @@ const viewReducer = (tableName, currentSchema, schemas, viewState, action) => { case V_REQUEST_PROGRESS: return { ...viewState, isProgressing: action.data }; case V_COUNT_REQUEST_SUCCESS: - return { ...viewState, count: action.count }; + return { + ...viewState, + count: action.count, + isCountEstimated: action.isEstimated === true, + }; case V_EXPAND_ROW: return { ...viewState, diff --git a/console/src/components/Services/Data/TableBrowseRows/ViewTable.js b/console/src/components/Services/Data/TableBrowseRows/ViewTable.js index 9a63821006e4e..257efa49a0598 100644 --- a/console/src/components/Services/Data/TableBrowseRows/ViewTable.js +++ b/console/src/components/Services/Data/TableBrowseRows/ViewTable.js @@ -110,6 +110,7 @@ class ViewTable extends Component { triggeredFunction, location, estimatedCount, + isCountEstimated, } = this.props; // check if table exists @@ -161,7 +162,8 @@ class ViewTable extends Component { // Choose the right nav bar header thing const header = ( ${COUNT_LIMIT} ` : getReadableNumber(count) + })`; } const activeTab = tabNameMap[tabName]; diff --git a/console/src/components/Services/Data/constants.js b/console/src/components/Services/Data/constants.js index d1ae99952d57f..d2021915afd39 100644 --- a/console/src/components/Services/Data/constants.js +++ b/console/src/components/Services/Data/constants.js @@ -38,6 +38,8 @@ export const Integers = [ 'bigint', ]; +export const COUNT_LIMIT = 100000; + export const Reals = ['float4', 'float8', 'numeric']; export const Numerics = [...Integers, ...Reals]; From e4d3469ef09c620e33009efc3d44b7c56e75fb25 Mon Sep 17 00:00:00 2001 From: Rishichandra Wawhal Date: Wed, 6 May 2020 11:33:58 +0530 Subject: [PATCH 2/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b30b1c3342f7..2c7310bb0914e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ Read more about the session argument for computed fields in the [docs](https://h (Add entries here in the order of: server, console, cli, docs, others) +- console: avoid count queries for large tables (#4681) - console: add read replica support section to pro popup (#4118) - cli: list all avialable commands in root command help (fix #4623) From da2aea1b3c7d33ebe8afbf7aeab7d371d5b38f4e Mon Sep 17 00:00:00 2001 From: Aleksandra Sikora Date: Wed, 6 May 2020 11:16:38 +0200 Subject: [PATCH 3/4] simplify getting estimatedCount --- .../components/Services/Data/TableBrowseRows/ViewActions.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/console/src/components/Services/Data/TableBrowseRows/ViewActions.js b/console/src/components/Services/Data/TableBrowseRows/ViewActions.js index 9eeafa80c5fcc..ef41d593e51eb 100644 --- a/console/src/components/Services/Data/TableBrowseRows/ViewActions.js +++ b/console/src/components/Services/Data/TableBrowseRows/ViewActions.js @@ -97,7 +97,7 @@ const vMakeRowsRequest = () => { dispatch({ type: V_REQUEST_SUCCESS, data: data[0], - estimatedCount: data[1].result[1], + estimatedCount: parseInt(data[1].result[1][0], 10), }), dispatch({ type: V_REQUEST_PROGRESS, data: false }), ]); @@ -160,8 +160,7 @@ const vMakeCountRequest = () => { const vMakeTableRequests = () => (dispatch, getState) => { dispatch(vMakeRowsRequest()).then(() => { - const estimatedCountResult = getState().tables.view.estimatedCount[0]; - const estimatedCount = parseInt(estimatedCountResult, 10); + const { estimatedCount } = getState().tables.view; if (estimatedCount > COUNT_LIMIT) { dispatch({ type: V_COUNT_REQUEST_SUCCESS, From dc17fe5acfff94678a0fb604c2f087dc12d399fa Mon Sep 17 00:00:00 2001 From: Aleksandra Sikora Date: Wed, 6 May 2020 12:00:44 +0200 Subject: [PATCH 4/4] prevent doubled requests --- .../components/Services/Data/TableBrowseRows/FilterQuery.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/console/src/components/Services/Data/TableBrowseRows/FilterQuery.js b/console/src/components/Services/Data/TableBrowseRows/FilterQuery.js index c259cb4f9fd03..0aca0fce421b5 100644 --- a/console/src/components/Services/Data/TableBrowseRows/FilterQuery.js +++ b/console/src/components/Services/Data/TableBrowseRows/FilterQuery.js @@ -27,6 +27,7 @@ import Button from '../../../Common/Button/Button'; import ReloadEnumValuesButton from '../Common/Components/ReloadEnumValuesButton'; import styles from '../../../Common/FilterQuery/FilterQuery.scss'; import { getPersistedPageSize } from './localStorageUtils'; +import { isEmpty } from '../../../Common/utils/jsUtils'; const history = createHistory(); @@ -205,7 +206,7 @@ class FilterQuery extends Component { componentDidMount() { const { dispatch, tableSchema, curQuery } = this.props; const limit = getPersistedPageSize(); - if (!this.props.urlQuery) { + if (isEmpty(this.props.urlQuery)) { dispatch(setDefaultQuery({ ...curQuery, limit })); return; }