Skip to content

Commit

Permalink
perf(backend): improve performance of timeline apis (taiyme#115)
Browse files Browse the repository at this point in the history
* ホームタイムラインの読み込みでクエリタイムアウトになるのを修正する (misskey-dev#10106)

* refactor

* perf(backend): improve performance of timeline apis

* chore(backend): local-timeline.ts

Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
Co-authored-by: taiy <53635909+taiyme@users.noreply.github.com>
Co-authored-by: cffnpwr <cffnpwr@gmail.com>
  • Loading branch information
4 people committed Mar 31, 2023
1 parent b770777 commit 4f529cb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/misc/gen-id.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ulid } from 'ulid';
import config from '@/config/index.js';
import { genAid } from './id/aid.js';
import { genMeid } from './id/meid.js';
import { genMeidg } from './id/meidg.js';
import { genObjectId } from './id/object-id.js';
import config from '@/config/index.js';

const metohd = config.id.toLowerCase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Brackets } from 'typeorm';
import { fetchMeta } from '@/misc/fetch-meta.js';
import { Followings, Notes } from '@/models/index.js';
import { activeUsersChart } from '@/services/chart/index.js';
import { genId } from '@/misc/gen-id.js';
import define from '../../define.js';
import { ApiError } from '../../error.js';
import { makePaginationQuery } from '../../common/make-pagination-query.js';
Expand Down Expand Up @@ -70,6 +71,7 @@ export default define(meta, paramDef, async (ps, user) => {

const query = makePaginationQuery(Notes.createQueryBuilder('note'),
ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
.andWhere('note.id > :minId', { minId: genId(new Date(Date.now() - (1000 * 60 * 60 * 24 * 10))) }) // 10日前まで
.andWhere(new Brackets(qb => {
qb.where(`((note.userId IN (${ followingQuery.getQuery() })) OR (note.userId = :meId))`, { meId: user.id })
.orWhere('note.visibility = \'public\'');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Brackets } from 'typeorm';
import { fetchMeta } from '@/misc/fetch-meta.js';
import { Notes, Users } from '@/models/index.js';
import { Notes } from '@/models/index.js';
import { activeUsersChart } from '@/services/chart/index.js';
import { genId } from '@/misc/gen-id.js';
import define from '../../define.js';
import { ApiError } from '../../error.js';
import { generateMutedUserQuery } from '../../common/generate-muted-user-query.js';
Expand Down Expand Up @@ -67,6 +68,7 @@ export default define(meta, paramDef, async (ps, user) => {
//#region Construct query
const query = makePaginationQuery(Notes.createQueryBuilder('note'),
ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
.andWhere('note.id > :minId', { minId: genId(new Date(Date.now() - (1000 * 60 * 60 * 24 * 10))) }) // 10日前まで
.andWhere('(note.visibility = \'public\') AND (note.userHost IS NULL)')
.innerJoinAndSelect('note.user', 'user')
.leftJoinAndSelect('user.avatar', 'avatar')
Expand All @@ -92,10 +94,11 @@ export default define(meta, paramDef, async (ps, user) => {
}

if (ps.fileType != null) {
const fileType = ps.fileType;
query.andWhere('note.fileIds != \'{}\'');
query.andWhere(new Brackets(qb => {
for (const type of ps.fileType!) {
const i = ps.fileType!.indexOf(type);
for (const type of fileType) {
const i = fileType.indexOf(type);
qb.orWhere(`:type${i} = ANY(note.attachedFileTypes)`, { [`type${i}`]: type });
}
}));
Expand Down
31 changes: 13 additions & 18 deletions packages/backend/src/server/api/endpoints/notes/timeline.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Brackets } from 'typeorm';
import { Notes, Followings } from '@/models/index.js';
import { activeUsersChart } from '@/services/chart/index.js';
import { genId } from '@/misc/gen-id.js';
import define from '../../define.js';
import { makePaginationQuery } from '../../common/make-pagination-query.js';
import { generateVisibilityQuery } from '../../common/generate-visibility-query.js';
Expand Down Expand Up @@ -48,16 +49,14 @@ export const paramDef = {

// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
const hasFollowing = (await Followings.count({
where: {
followerId: user.id,
},
take: 1,
})) !== 0;
const followees = await Followings.createQueryBuilder('following')
.select('following.followeeId')
.where('following.followerId = :followerId', { followerId: user.id })
.getMany();

//#region Construct query
const query = makePaginationQuery(Notes.createQueryBuilder('note'),
ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
.andWhere('note.id > :minId', { minId: genId(new Date(Date.now() - (1000 * 60 * 60 * 24 * 10))) }) // 10日前まで
.innerJoinAndSelect('note.user', 'user')
.leftJoinAndSelect('user.avatar', 'avatar')
.leftJoinAndSelect('user.banner', 'banner')
Expand All @@ -70,17 +69,13 @@ export default define(meta, paramDef, async (ps, user) => {
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');

if (hasFollowing) {
const followees = await Followings.createQueryBuilder('following')
.select('following.followeeId')
.where('following.followerId = :followerId', { followerId: user.id })
.getMany();
const meOrFolloweeIds = [user.id, ...followees.map(f => f.followeeId)];

query.andWhere('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds });
} else {
query.andWhere('note.userId = :meId', { meId: user.id });
}
if (followees.length > 0) {
const meOrFolloweeIds = [user.id, ...followees.map(f => f.followeeId)];

query.andWhere('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds });
} else {
query.andWhere('note.userId = :meId', { meId: user.id });
}

generateChannelQuery(query, user);
generateRepliesQuery(query, user);
Expand Down

0 comments on commit 4f529cb

Please sign in to comment.