Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

タイムライン取得時にdrive_fileへのクエリ回数を減らす #10129

Closed
yszkst opened this issue Feb 26, 2023 · 12 comments · Fixed by #10133
Closed

タイムライン取得時にdrive_fileへのクエリ回数を減らす #10129

yszkst opened this issue Feb 26, 2023 · 12 comments · Fixed by #10133
Assignees
Labels
packages/backend Server side specific issue/PR 🐢Performance Efficiency related issue/PR 💚Refactor Rewriting code without changing behavior

Comments

@yszkst
Copy link
Contributor

yszkst commented Feb 26, 2023

https://github.com/misskey-dev/misskey/blob/develop/packages/backend/src/core/entities/NoteEntityService.ts#L392 から
Noteをレスポンス用に整形するために
https://github.com/misskey-dev/misskey/blob/develop/packages/backend/src/core/entities/NoteEntityService.ts#L307
drive_fileへのクエリが画像数分走る

noteテーブルに添付ファイル情報を複数カラムの配列で持っているのは扱いにくいのでdrive_fileへの中間テーブルを作ってJOINしたい。

"fileIds" _varchar NOT NULL DEFAULT '{}'::character varying[],
"attachedFileTypes" _varchar NOT NULL DEFAULT '{}'::character varying[],

手っ取り早いのは絵文字でやっている
await this.customEmojiService.prefetchEmojis(this.customEmojiService.aggregateNoteEmojis(notes));
のような処理追加ですが、配列型保持にしている理由が無ければ思い切ってテーブルを正規化した方がいい気がします。

問題なければAssigneeにしてほしいです

@syuilo
Copy link
Member

syuilo commented Feb 26, 2023

思い切ってテーブルを正規化した方がいい気がします。

既存のレコードのマイグレーションが大変かもしれないですね

@syuilo syuilo added 🐢Performance Efficiency related issue/PR packages/backend Server side specific issue/PR 💚Refactor Rewriting code without changing behavior labels Feb 26, 2023
@pantasystem
Copy link

テーブルの構造を変えない方法としては
DriveFileの内容をRedisにキャッシュしてしまうのはどうでしょうか?
DriveFileはNoteやUserに比べそこまで更新されるわけではないので、
まだ実装の容易性は高そうだと思いました。
またMisskeyの特性上基本的に同じ投稿が多数のユーザから参照される傾向があるので
キャッシュのヒット率も高くなるのではないかと思いました。

@rinsuki
Copy link
Contributor

rinsuki commented Feb 26, 2023

とりあえずpackManyでhintとして全ドライブファイルpackManyして渡すのが良いかも?

@rinsuki
Copy link
Contributor

rinsuki commented Feb 26, 2023

というかdriveFileのpackManyが適当なのを何とかした方がいいな https://github.com/misskey-dev/misskey/blob/develop/packages/backend/src/core/entities/DriveFileEntityService.ts#L261

@syuilo
Copy link
Member

syuilo commented Feb 27, 2023

drive_fileへの中間テーブルを作ってJOINしたい

ちなみにこうする場合ファイルの並び順ってどのように表現されますか?

@rinsuki
Copy link
Contributor

rinsuki commented Feb 27, 2023

@syuilo とりあえず俺のコメントのやつだけやってみていい?

@syuilo
Copy link
Member

syuilo commented Feb 27, 2023

@syuilo とりあえず俺のコメントのやつだけやってみていい?

👍

@yszkst
Copy link
Contributor Author

yszkst commented Feb 27, 2023

既存のレコードのマイグレーションが大変かもしれないですね

クエリは単純なので画像付き投稿数以上の予想外に時間がかかるというのはないと思います。

misskey=# select id, "fileIds" from note order by "createdAt" desc;
     id     |                                                                           fileIds
------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------
 9bptv53mdq | {9aqx97b2z5,9aqwi3jot3,9aqwhkwxt2,9aqwh8g2t1}
 9bp9h6ryuq | {9bp9h58cup}
 9bp9gaysuo | {9bkqw3snx6}


misskey=# select note.id, u."fileId", u."rank" from note cross join unnest(note."fileIds") with ordinality as u("fileId", "rank") order by note."createdAt" desc;
     id     |   fileId   | rank
------------+------------+------
 9bptv53mdq | 9aqx97b2z5 |    1
 9bptv53mdq | 9aqwi3jot3 |    2
 9bptv53mdq | 9aqwhkwxt2 |    3
 9bptv53mdq | 9aqwh8g2t1 |    4
 9bp9h6ryuq | 9bp9h58cup |    1
 9bp9gaysuo | 9bkqw3snx6 |    1



order byは確認のためなので本来不要

ちなみにこうする場合ファイルの並び順ってどのように表現されますか?

中間テーブルにノート内での順番としてrank を持たせます。
並び変えは取ってきた後にNodeJS側でやれます。

TypeORMで書きやすいかは未確認です。

@rinsuki
Copy link
Contributor

rinsuki commented Feb 27, 2023

これ fileIds に貼ってある index 使う方法ありますかね?一応 fileIds に array_ops のインデックスは貼ってはありますが、このインデックスだと中身がある場合のみ取得みたいなことはできなさそうなので https://www.postgresql.jp/document/14/html/gin-builtin-opclasses.html notes テーブルかたっぱしから舐めることになりませんか?

@rinsuki
Copy link
Contributor

rinsuki commented Feb 27, 2023

(あと配列からテーブルにする話は #10134 でやりたい)

@tamaina
Copy link
Contributor

tamaina commented Feb 27, 2023

わざわざ中間テーブルを作る(fileIdsの配列をノートに直接突っ込みたくない)理由がわかっていない

(packManyでファイルごとに要求しているのは直した方がいいとは思うけど)

@yszkst
Copy link
Contributor Author

yszkst commented Feb 27, 2023

なんの方法でもホームTLが絵師フォロー中なので軽くなれば助かります 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
packages/backend Server side specific issue/PR 🐢Performance Efficiency related issue/PR 💚Refactor Rewriting code without changing behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants