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

Avoid N+1 selects from user table when packing many entities #13910

Closed
1 task done
zyoshoka opened this issue May 30, 2024 · 0 comments · Fixed by #13911
Closed
1 task done

Avoid N+1 selects from user table when packing many entities #13910

zyoshoka opened this issue May 30, 2024 · 0 comments · Fixed by #13911
Labels
packages/backend Server side specific issue/PR 🐢Performance Efficiency related issue/PR

Comments

@zyoshoka
Copy link
Contributor

Summary

エンティティを packMany する際、単純に packmap している .*EntityService が複数あります。

@bindThis
public packMany(
reports: any[],
) {
return Promise.all(reports.map(x => this.pack(x)));
}

この際 pack 内で UserEntityService.pack が呼び出されている場合、キャッシュが効かない最悪の場合には packMany に渡される配列の要素数回 user テーブルからの SELECT クエリが走ることになります。

return await awaitAll({
id: report.id,
createdAt: this.idService.parse(report.id).date.toISOString(),
comment: report.comment,
resolved: report.resolved,
reporterId: report.reporterId,
targetUserId: report.targetUserId,
assigneeId: report.assigneeId,
reporter: this.userEntityService.pack(report.reporter ?? report.reporterId, null, {
schema: 'UserDetailedNotMe',
}),
targetUser: this.userEntityService.pack(report.targetUser ?? report.targetUserId, null, {
schema: 'UserDetailedNotMe',
}),
assignee: report.assigneeId ? this.userEntityService.pack(report.assignee ?? report.assigneeId, null, {
schema: 'UserDetailedNotMe',
}) : null,
forwarded: report.forwarded,
});

#13550UserEntityService.packMany が最適化されたので、このようなケースでは単純に map するのではなく、前段で packMany を呼び出して1回のクエリで済ませた上でそれを pack に渡すほうがより適切だと思われます。

Purpose

パフォーマンスの向上

Do you want to implement this feature yourself?

  • Yes, I will implement this by myself and send a pull request
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant