Speed-up a few hot queries on pds and bsky ()

* hit index for notif unread count in appview

* fix index usage on pds account lookup by handle

* add index to blob tempkey column to support some hot queries
This commit is contained in:
devin ivy 2023-08-25 11:09:15 -04:00 committed by GitHub
parent 19d2bdc457
commit d28c9ab3de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 9 deletions
packages
bsky/src/api/app/bsky/notification
pds/src

@ -1,3 +1,4 @@
import { sql } from 'kysely'
import { InvalidRequestError } from '@atproto/xrpc-server'
import { Server } from '../../../../lexicon'
import { countAll, notSoftDeletedClause } from '../../../../db/util'
@ -22,15 +23,12 @@ export default function (server: Server, ctx: AppContext) {
.innerJoin('record', 'record.uri', 'notification.recordUri')
.where(notSoftDeletedClause(ref('actor')))
.where(notSoftDeletedClause(ref('record')))
// Ensure to hit notification_did_sortat_idx, handling case where lastSeenNotifs is null.
.where('notification.did', '=', requester)
.where((inner) =>
inner
.where('actor_state.lastSeenNotifs', 'is', null)
.orWhereRef(
'notification.sortAt',
'>',
'actor_state.lastSeenNotifs',
),
.where(
'notification.sortAt',
'>',
sql`coalesce(${ref('actor_state.lastSeenNotifs')}, ${''})`,
)
.executeTakeFirst()

@ -0,0 +1,13 @@
import { Kysely } from 'kysely'
export async function up(db: Kysely<unknown>): Promise<void> {
await db.schema
.createIndex('blob_tempkey_idx')
.on('blob')
.column('tempKey')
.execute()
}
export async function down(db: Kysely<unknown>): Promise<void> {
await db.schema.dropIndex('blob_tempkey_idx').execute()
}

@ -63,3 +63,4 @@ export * as _20230808T172813122Z from './20230808T172813122Z-repo-rev'
export * as _20230810T203412859Z from './20230810T203412859Z-action-duration'
export * as _20230818T134357818Z from './20230818T134357818Z-runtime-flags'
export * as _20230824T182048120Z from './20230824T182048120Z-remove-post-hierarchy'
export * as _20230825T142507884Z from './20230825T142507884Z-blob-tempkey-idx'

@ -37,7 +37,13 @@ export class AccountService {
if (handleOrDid.startsWith('did:')) {
return qb.where('did_handle.did', '=', handleOrDid)
} else {
return qb.where('did_handle.handle', '=', handleOrDid)
// lower() is a little hack to avoid using the handle trgm index here, which is slow. not sure why it was preferring
// the handle trgm index over the handle unique index. in any case, we end-up using did_handle_handle_lower_idx instead, which is fast.
return qb.where(
sql`lower(${ref('did_handle.handle')})`,
'=',
handleOrDid,
)
}
})
.selectAll('user_account')