Skip to content

Commit

Permalink
perf(graphql sqlite): force join order and index for high cardinality…
Browse files Browse the repository at this point in the history
… tags PE-2395

This forces a join using the transaction_id index on the tags table when
dealing with high cardinality tag names (e.g. App-Name). Right now we're
just hard coding a list of those tags. In the future we will either
determine those based on periodicially refreshed counts from the DB or,
alternately, force all joins after the first tag join to use
transaction_id. That should be optimal provided users put the most
selective tag first in their queries.
  • Loading branch information
djwhitt committed May 9, 2023
1 parent 8e67793 commit 5907d37
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/database/standalone-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ const CPU_COUNT = os.cpus().length;

const STABLE_FLUSH_INTERVAL = 5;
const NEW_TX_CLEANUP_WAIT_SECS = 60 * 60 * 2;
const JOIN_LAST_TAG_NAMES = new Set(['App-Name', 'Content-Type']);
const HIGH_CARDINALITY_TAG_NAMES = new Set(['App-Name', 'Content-Type']);

function tagJoinSortPriority(tag: { name: string; values: string[] }) {
return JOIN_LAST_TAG_NAMES.has(tag.name) ? 1 : 0;
return HIGH_CARDINALITY_TAG_NAMES.has(tag.name) ? 1 : 0;
}

export function encodeTransactionGqlCursor({
Expand Down Expand Up @@ -790,10 +790,23 @@ export class StandaloneSqliteDatabaseWorker {
if (source === 'stable') {
heightSortTableAlias = tagAlias;
blockTransactionIndexSortTableAlias = tagAlias;
joinCond = {
[`${blockTransactionIndexTableAlias}.block_transaction_index`]: `${tagAlias}.block_transaction_index`,
[`${heightTableAlias}.height`]: `${tagAlias}.height`,
};
if (index === 0 || !HIGH_CARDINALITY_TAG_NAMES.has(tag.name)) {
joinCond = {
[`${blockTransactionIndexTableAlias}.block_transaction_index`]: `${tagAlias}.block_transaction_index`,
[`${heightTableAlias}.height`]: `${tagAlias}.height`,
};
} else {
const previousTagAlias = `"${index - 1}_${index - 1}"`;
joinCond = {
[`${previousTagAlias}.transaction_id`]: `${tagAlias}.transaction_id`,
};
query.where(
sql.notEq(
`${previousTagAlias}.transaction_tag_index`,
sql(`${tagAlias}.transaction_tag_index`),
),
);
}
} else {
joinCond = {
[`${txTableAlias}.id`]: `${tagAlias}.transaction_id`,
Expand Down

0 comments on commit 5907d37

Please sign in to comment.