mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
trends: read directly from the tags table instead of doing an inner join on events
This commit is contained in:
parent
ece18c9bd8
commit
48a4e30e38
2 changed files with 11 additions and 6 deletions
|
|
@ -54,6 +54,12 @@ export async function up(db: Kysely<any>): Promise<void> {
|
|||
.ifNotExists()
|
||||
.columns(['value', 'name', 'kind', 'pubkey', 'created_at desc', 'event_id asc'])
|
||||
.execute();
|
||||
await db.schema
|
||||
.createIndex('nostr_tags_trends')
|
||||
.on('nostr_tags')
|
||||
.ifNotExists()
|
||||
.columns(['created_at', 'name', 'kind'])
|
||||
.execute();
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
|
|
|
|||
|
|
@ -22,24 +22,23 @@ export async function getTrendingTagValues(
|
|||
): Promise<{ value: string; authors: number; uses: number }[]> {
|
||||
let query = kysely
|
||||
.selectFrom('nostr_tags')
|
||||
.innerJoin('nostr_events', 'nostr_events.id', 'nostr_tags.event_id')
|
||||
.select(({ fn }) => [
|
||||
'nostr_tags.value',
|
||||
fn.agg<number>('count', ['nostr_events.pubkey']).distinct().as('authors'),
|
||||
fn.agg<number>('count', ['nostr_tags.pubkey']).distinct().as('authors'),
|
||||
fn.countAll<number>().as('uses'),
|
||||
])
|
||||
.where('nostr_tags.name', 'in', tagNames)
|
||||
.groupBy('nostr_tags.value')
|
||||
.orderBy((c) => c.fn.agg('count', ['nostr_events.pubkey']).distinct(), 'desc');
|
||||
.orderBy((c) => c.fn.agg('count', ['nostr_tags.pubkey']).distinct(), 'desc');
|
||||
|
||||
if (filter.kinds) {
|
||||
query = query.where('nostr_events.kind', 'in', filter.kinds);
|
||||
query = query.where('nostr_tags.kind', 'in', filter.kinds);
|
||||
}
|
||||
if (typeof filter.since === 'number') {
|
||||
query = query.where('nostr_events.created_at', '>=', filter.since);
|
||||
query = query.where('nostr_tags.created_at', '>=', filter.since);
|
||||
}
|
||||
if (typeof filter.until === 'number') {
|
||||
query = query.where('nostr_events.created_at', '<=', filter.until);
|
||||
query = query.where('nostr_tags.created_at', '<=', filter.until);
|
||||
}
|
||||
if (typeof filter.limit === 'number') {
|
||||
query = query.limit(filter.limit);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue