Fix for count being very slow on large Parse Classes' collections (Postgres) (#5330)

* Changed count to be approximate. Should help with postgres slowness

* refactored last commit to only fall back to estimate if no complex query

* handlign variables correctly

* Trying again because it was casting to lowercase table names which doesnt work for us/

* syntax error

* Adding quotations to pg query

* hopefully final pg fix

* Postgres will now use an approximate count unless there is a more complex query specified

* handling edge case

* Fix for count being very slow on large Parse Classes' collections in Postgres. Replicating fix for Mongo in issue 5264

* Fixed silly spelling error resulting from copying over notes

* Lint fixes

* limiting results to 1 on approximation

* suppress test that we can no longer run for postgres

* removed tests from Postgres that no longer apply

* made changes requested by dplewis

* fixed count errors

* updated package.json

* removed test exclude for pg

* removed object types from method

* test disabled for postgres

* returned type

* add estimate count test

* fix mongo test
This commit is contained in:
CoderickLamar
2019-04-08 15:59:15 -07:00
committed by Diamond Lewis
parent e396612254
commit c7eb7daeae
7 changed files with 181 additions and 57 deletions

View File

@@ -1962,17 +1962,37 @@ export class PostgresStorageAdapter implements StorageAdapter {
}
// Executes a count.
count(className: string, schema: SchemaType, query: QueryType) {
debug('count', className, query);
count(
className: string,
schema: SchemaType,
query: QueryType,
readPreference?: string,
estimate?: boolean = true
) {
debug('count', className, query, readPreference, estimate);
const values = [className];
const where = buildWhereClause({ schema, query, index: 2 });
values.push(...where.values);
const wherePattern =
where.pattern.length > 0 ? `WHERE ${where.pattern}` : '';
const qs = `SELECT count(*) FROM $1:name ${wherePattern}`;
let qs = '';
if (where.pattern.length > 0 || !estimate) {
qs = `SELECT count(*) FROM $1:name ${wherePattern}`;
} else {
qs =
'SELECT reltuples AS approximate_row_count FROM pg_class WHERE relname = $1';
}
return this._client
.one(qs, values, a => +a.count)
.one(qs, values, a => {
if (a.approximate_row_count != null) {
return +a.approximate_row_count;
} else {
return +a.count;
}
})
.catch(error => {
if (error.code !== PostgresRelationDoesNotExistError) {
throw error;
@@ -2327,6 +2347,11 @@ export class PostgresStorageAdapter implements StorageAdapter {
updateSchemaWithIndexes(): Promise<void> {
return Promise.resolve();
}
// Used for testing purposes
updateEstimatedCount(className: string) {
return this._client.none('ANALYZE $1:name', [className]);
}
}
function convertPolygonToSQL(polygon) {