Skip to content

Commit

Permalink
perf(graphql): correct GQL query performance issue
Browse files Browse the repository at this point in the history
When ANS-104 support was added, the order by statements were adjusted to
not specify the stables to use for order (column numbers were used
instead). When filtering by tags and querying by stable data, this
introduced a performance regression. In that case, it's important to
sort by the height in tag tables for the best performance. This change
reintroduces sorting by table alias and field instead of column number.
  • Loading branch information
djwhitt committed Jul 28, 2023
1 parent c21cc16 commit e31a3dc
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/database/standalone-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,7 @@ export class StandaloneSqliteDatabaseWorker {
let tagIndexColumn: string;
let heightSortTableAlias: string;
let blockTransactionIndexSortTableAlias: string;
let dataItemSortTableAlias: string | undefined = undefined;
let maxDbHeight = Infinity;

if (source === 'stable_txs') {
Expand Down Expand Up @@ -1299,6 +1300,7 @@ export class StandaloneSqliteDatabaseWorker {
if (index === 0) {
heightSortTableAlias = tagAlias;
blockTransactionIndexSortTableAlias = tagAlias;
dataItemSortTableAlias = tagAlias;
joinCond = {
[`${blockTransactionIndexTableAlias}.block_transaction_index`]: `${tagAlias}.block_transaction_index`,
[`${heightTableAlias}.height`]: `${tagAlias}.height`,
Expand Down Expand Up @@ -1406,7 +1408,13 @@ export class StandaloneSqliteDatabaseWorker {
),
);
}
query.orderBy('1 DESC, 2 DESC, 3 DESC');
let orderBy = `${heightSortTableAlias}.height DESC, ${blockTransactionIndexSortTableAlias}.block_transaction_index DESC`;
if (source === 'stable_items' && dataItemSortTableAlias !== undefined) {
orderBy += `, ${dataItemSortTableAlias}.data_item_id DESC`;
} else {
orderBy += `, 3 DESC`;
}
query.orderBy(orderBy);
} else {
if (
cursorHeight != undefined &&
Expand Down Expand Up @@ -1440,7 +1448,13 @@ export class StandaloneSqliteDatabaseWorker {
),
);
}
query.orderBy('1 ASC, 2 ASC, 3 ASC');
let orderBy = `${heightSortTableAlias}.height ASC, ${blockTransactionIndexSortTableAlias}.block_transaction_index ASC`;
if (source === 'stable_items' && dataItemSortTableAlias !== undefined) {
orderBy += `, ${dataItemSortTableAlias}.data_item_id ASC`;
} else {
orderBy += `, 3 ASC`;
}
query.orderBy(orderBy);
}
}

Expand Down

0 comments on commit e31a3dc

Please sign in to comment.