From 8a7489cae849eacbf3d8decc598519a50c68c0fa Mon Sep 17 00:00:00 2001 From: Sebastijan K <58827427+sebastijankuzner@users.noreply.github.com> Date: Wed, 16 Nov 2022 10:22:59 +0100 Subject: [PATCH] merge: cherry-pick query optimizer workaround (#4726) (#4730) fix(core-database): query optimizer workaround (#4726) * fix(core-database): query optimizer workaround * Fix only for timestamp in transaction --- .../core-database/src/repositories/abstract-repository.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/core-database/src/repositories/abstract-repository.ts b/packages/core-database/src/repositories/abstract-repository.ts index 2d6382ddbd..03d105a9d1 100644 --- a/packages/core-database/src/repositories/abstract-repository.ts +++ b/packages/core-database/src/repositories/abstract-repository.ts @@ -142,7 +142,13 @@ export abstract class AbstractRepository extends private addOrderBy(queryBuilder: SelectQueryBuilder, sorting: Contracts.Search.Sorting): void { if (sorting.length) { - const column = this.queryHelper.getColumnName(this.metadata, sorting[0].property); + let column = this.queryHelper.getColumnName(this.metadata, sorting[0].property); + + // Forces PostgreSQL query optimizer to take faster route + if (this.metadata.name === "Transaction" && column === "timestamp") { + column = `${column}+0`; + } + queryBuilder.orderBy(column, sorting[0].direction === "desc" ? "DESC" : "ASC"); for (const item of sorting.slice(1)) {