Skip to content

Commit

Permalink
Miscellaneous query optimizations on appview (#1766)
Browse files Browse the repository at this point in the history
* split-up record fetching for notifs

* simplify query for author posts with media

* avoid hitting actor_handle_tgrm_idx when fetching actor by handle
  • Loading branch information
devinivy authored Oct 24, 2023
1 parent 4122109 commit 825b2a0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export const skeleton = async (

if (filter === 'posts_with_media') {
feedItemsQb = feedItemsQb
// and only your own posts/reposts
.where('post.creator', '=', actorDid)
// only your own posts
.where('type', '=', 'post')
// only posts with media
.whereExists((qb) =>
qb
Expand Down
55 changes: 33 additions & 22 deletions packages/bsky/src/api/app/bsky/notification/listNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ const skeleton = async (
}
let notifBuilder = db.db
.selectFrom('notification as notif')
.innerJoin('record', 'record.uri', 'notif.recordUri')
.innerJoin('actor as author', 'author.did', 'notif.author')
.where(notSoftDeletedClause(ref('record')))
.where(notSoftDeletedClause(ref('author')))
.where('notif.did', '=', viewer)
.where((clause) =>
clause
Expand All @@ -69,23 +65,20 @@ const skeleton = async (
),
)
.select([
'notif.author as authorDid',
'notif.recordUri as uri',
'notif.recordCid as cid',
'author.did as authorDid',
'author.handle as authorHandle',
'author.indexedAt as authorIndexedAt',
'author.takedownId as authorTakedownId',
'notif.reason as reason',
'notif.reasonSubject as reasonSubject',
'notif.sortAt as indexedAt',
'record.json as recordJson',
])

const keyset = new NotifsKeyset(ref('notif.sortAt'), ref('notif.recordCid'))
notifBuilder = paginate(notifBuilder, {
cursor,
limit,
keyset,
tryIndex: true,
})

const actorStateQuery = db.db
Expand All @@ -107,17 +100,18 @@ const skeleton = async (
}

const hydration = async (state: SkeletonState, ctx: Context) => {
const { graphService, actorService, labelService } = ctx
const { graphService, actorService, labelService, db } = ctx
const { params, notifs } = state
const { viewer } = params
const dids = notifs.map((notif) => notif.authorDid)
const uris = notifs.map((notif) => notif.uri)
const [actors, labels, bam] = await Promise.all([
const [actors, records, labels, bam] = await Promise.all([
actorService.views.profiles(dids, viewer),
getRecordMap(db, uris),
labelService.getLabelsForUris(uris),
graphService.getBlockAndMuteState(dids.map((did) => [viewer, did])),
])
return { ...state, actors, labels, bam }
return { ...state, actors, records, labels, bam }
}

const noBlockOrMutes = (state: HydrationState) => {
Expand All @@ -131,11 +125,11 @@ const noBlockOrMutes = (state: HydrationState) => {
}

const presentation = (state: HydrationState) => {
const { notifs, cursor, actors, labels, lastSeenNotifs } = state
const { notifs, cursor, actors, records, labels, lastSeenNotifs } = state
const notifications = mapDefined(notifs, (notif) => {
const author = actors[notif.authorDid]
if (!author) return undefined
const record = jsonStringToLex(notif.recordJson) as Record<string, unknown>
const record = records[notif.uri]
if (!author || !record) return undefined
const recordLabels = labels[notif.uri] ?? []
const recordSelfLabels = getSelfLabels({
uri: notif.uri,
Expand All @@ -157,6 +151,24 @@ const presentation = (state: HydrationState) => {
return { notifications, cursor }
}

const getRecordMap = async (
db: Database,
uris: string[],
): Promise<RecordMap> => {
if (!uris.length) return {}
const { ref } = db.db.dynamic
const recordRows = await db.db
.selectFrom('record')
.select(['uri', 'json'])
.where('uri', 'in', uris)
.where(notSoftDeletedClause(ref('record')))
.execute()
return recordRows.reduce((acc, { uri, json }) => {
acc[uri] = jsonStringToLex(json) as Record<string, unknown>
return acc
}, {} as RecordMap)
}

type Context = {
db: Database
actorService: ActorService
Expand All @@ -178,20 +190,19 @@ type SkeletonState = {
type HydrationState = SkeletonState & {
bam: BlockAndMuteState
actors: ActorInfoMap
records: RecordMap
labels: Labels
}

type RecordMap = { [uri: string]: Record<string, unknown> }

type NotifRow = {
indexedAt: string
cid: string
uri: string
authorDid: string
authorHandle: string | null
authorIndexedAt: string
authorTakedownId: number | null
uri: string
cid: string
reason: string
reasonSubject: string | null
recordJson: string
indexedAt: string
}

class NotifsKeyset extends TimeCidKeyset<NotifRow> {
Expand Down
8 changes: 7 additions & 1 deletion packages/bsky/src/services/actor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ export class ActorService {
qb = qb.orWhere('actor.did', 'in', dids)
}
if (handles.length) {
qb = qb.orWhere('actor.handle', 'in', handles)
qb = qb.orWhere(
'actor.handle',
'in',
handles.length === 1
? [handles[0], handles[0]] // a silly (but worthwhile) optimization to avoid usage of actor_handle_tgrm_idx
: handles,
)
}
return qb
})
Expand Down

0 comments on commit 825b2a0

Please sign in to comment.