Skip to content

Commit

Permalink
Tune query (#4192)
Browse files Browse the repository at this point in the history
* showReplayInPublicTimeline index

* SLOWPLAN

* a

* a

* a

* apiLogger

* a

* a

* a

* a

* a

* a

* a

* SLOWTL

* a

* x

* x

* o

* a

* o

* a

* s

* x

* x

* a
  • Loading branch information
mei23 committed Nov 24, 2022
1 parent 2da0997 commit fbf837c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
36 changes: 32 additions & 4 deletions src/server/api/common/get-timeline.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Note, { INote, packMany } from '../../../models/note';
import { ILocalUser } from '../../../models/user';
import { apiLogger } from '../logger';

export async function getPackedTimeline(me: ILocalUser | null, query: any, sort: Record<string, number>, limit: number) {
const timeline = await Note.aggregate<INote[]>([
function getStages(query: any, sort: Record<string, number>, limit: number) {
return [
{
$match: { $and: [
query,
Expand Down Expand Up @@ -46,10 +47,37 @@ export async function getPackedTimeline(me: ILocalUser | null, query: any, sort:
path: '$__user'
}
}
], // aggregates
];
}

export async function getPackedTimeline(me: ILocalUser | null, query: any, sort: Record<string, number>, limit: number, hint: string | undefined = undefined) {
const begin = performance.now();

// Query
const timeline = await Note.aggregate<INote[]>(getStages(query, sort, limit),
{
maxTimeMS: 55000,
hint,
});

return await packMany(timeline, me);
const queryEnd = performance.now();

// Pack
const packed = await packMany(timeline, me);

const packEnd = performance.now();

if (queryEnd - begin > 1000) {
apiLogger.warn(`SLOWTL: query=${(queryEnd - begin).toFixed()}, pack=${(packEnd - queryEnd).toFixed()}`);
}

return packed;
}

export async function explainTimeline(me: ILocalUser | null, query: any, sort: Record<string, number>, limit: number, hint: string | undefined = undefined) {
return await Note.aggregate<INote[]>(getStages(query, sort, limit),
{
explain: true,
hint,
}) as unknown;
}
2 changes: 2 additions & 0 deletions src/server/api/endpoints/notes/global-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export default define(meta, async (ps, user) => {

if (!m.showReplayInPublicTimeline) {
query.replyId = null;
} else {
query.replyId = { $ne: null };
}

if (hideUserIds && hideUserIds.length > 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/server/api/endpoints/notes/local-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export default define(meta, async (ps, user) => {

if (!m.showReplayInPublicTimeline) {
query.replyId = null;
} else {
query.replyId = { $ne: null };
}

if (hideUserIds && hideUserIds.length > 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/server/api/endpoints/notes/locao-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export default define(meta, async (ps, user) => {

if (!m.showReplayInPublicTimeline) {
query.replyId = null;
} else {
query.replyId = { $ne: null };
}

if (hideUserIds && hideUserIds.length > 0) {
Expand Down
8 changes: 1 addition & 7 deletions src/server/api/endpoints/notes/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import UserList from '../../../../models/user-list';
import { concat } from '../../../../prelude/array';
import { isSelfHost } from '../../../../misc/convert-host';
import { getHideRenoteUserIds } from '../../common/get-hide-renote-users';
import { oidIncludes } from '../../../../prelude/oid';
import { getPackedTimeline } from '../../common/get-timeline';
import config from '../../../../config';

Expand Down Expand Up @@ -174,13 +173,8 @@ export default define(meta, async (ps, user) => {
_id: -1
};

// どうせフィルタされるユーザーはフォローしてない扱いにして最初のuserのIXSCANの精度を少しでも高くする
const realFollowingIds = followingIds
.filter(x => !oidIncludes(hideUserIds, x))
.filter(x => !oidIncludes(hideFromHomeUsers, x));

const followQuery = [{
userId: { $in: realFollowingIds }
userId: { $in: followingIds }
}];

const visibleQuery = user == null ? [{
Expand Down

0 comments on commit fbf837c

Please sign in to comment.