Skip to content

Commit

Permalink
Greatly optimize article field value lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Dec 8, 2024
1 parent c4d5733 commit 2745e53
Showing 1 changed file with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,35 @@ export class PartitionedFeedArticleFieldStoreService {
}>
> {
const oneMonthAgo = dayjs().subtract(1, "month").toISOString();
const results = await this.connection.execute(
`SELECT field_hashed_value` +
` FROM ${this.TABLE_NAME}` +
` WHERE ${
olderThanOneMonth ? `created_at <= ?` : `created_at > ?`
} AND feed_id = ? AND field_name = 'id' AND field_hashed_value IN (${ids
.map(() => "?")
.join(", ")})`,
[oneMonthAgo, feedId, ...ids]
);

return results;
if (ids.length < 60) {
return this.connection.execute(
`SELECT field_hashed_value` +
` FROM ${this.TABLE_NAME}` +
` WHERE ${
olderThanOneMonth ? `created_at <= ?` : `created_at > ?`
} AND feed_id = ? AND field_name = 'id' AND field_hashed_value IN (${ids
.map(() => "?")
.join(", ")})`,
[oneMonthAgo, feedId, ...ids]
);
} else {
const parenthesized = ids.map(() => `(?)`).join(",");

const result = await this.connection.execute(
`SELECT field_hashed_value` +
` FROM ${this.TABLE_NAME}` +
` INNER JOIN (VALUES ${parenthesized}) vals(v) ON (
field_hashed_value = v)` +
` WHERE ${
olderThanOneMonth ? `created_at <= ?` : `created_at > ?`
} AND feed_id = ? AND field_name = 'id'` +
``,
[...ids, oneMonthAgo, feedId]
);

return result;
}
}

async someFieldsExist(
Expand Down

0 comments on commit 2745e53

Please sign in to comment.