Skip to content

Commit

Permalink
DBのパフォーマンス改善の為SQLを変更
Browse files Browse the repository at this point in the history
  • Loading branch information
emtkmkk committed Jan 15, 2024
1 parent 2738c37 commit 02efe4b
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export function generateVisibilityQuery(
// または 自分自身
.orWhere("note.userId = :meId")
// または 自分宛て
.orWhere(":meId = ANY(note.visibleUserIds)")
.orWhere(":meId = ANY(note.mentions)")
.orWhere("'{\":meId\"}' <@ (note.visibleUserIds)")
.orWhere("'{\":meId\"}' <@ (note.mentions)")
.orWhere(
new Brackets((qb) => {
qb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default define(meta, paramDef, async (ps, user) => {
new Brackets((qb) => {
qb.orWhere("note.channelId = :channelId", { channelId: channel.id })
if (!channel.description?.includes("[localOnly]")){
qb.orWhere(":channelName = ANY (note.tags)",{ channelName: normalizeForSearch(channel.name) });
qb.orWhere("'{\":channelName\"}' <@ (note.tags)",{ channelName: normalizeForSearch(channel.name) });
}
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const paramDef = {

export default define(meta, paramDef, async (ps, me) => {
const query = Users.createQueryBuilder("user").where(
":tag = ANY(user.tags)",
"'{\":tag\"}' <@ (user.tags)",
{ tag: normalizeForSearch(ps.tag) },
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default define(meta, paramDef, async (ps, user) => {
qb.where(
`((note.userId IN (${followingQuery.getQuery()})) OR (note.userId = :meId))`,
{ meId: user.id },
).orWhere(`(note.visibility = 'public') AND ((note.userHost IS NULL) OR (user.username || '@' || note."userHost" = ANY ('{"${m.recommendedInstances.join('","')}"}')))`);
).orWhere(`(note.visibility = 'public') AND ((note.userHost IS NULL) OR (user.username || '@' || note."userHost" IN ('{"${m.recommendedInstances.join('","')}"}')))`);
}),
)
.innerJoinAndSelect("note.user", "user")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default define(meta, paramDef, async (ps, user) => {
ps.sinceDate,
ps.untilDate,
)
.andWhere(`((note.userHost IS NULL) OR (user.username || '@' || note."userHost" = ANY ('{"${m.recommendedInstances.join('","')}"}')))`)
.andWhere(`((note.userHost IS NULL) OR (user.username || '@' || note."userHost" IN ('{"${m.recommendedInstances.join('","')}"}')))`)
.andWhere("(note.replyId IS NULL OR reply.userHost IS NULL)")
.innerJoinAndSelect("note.user", "user")
.leftJoinAndSelect("user.avatar", "avatar")
Expand Down Expand Up @@ -154,7 +154,7 @@ export default define(meta, paramDef, async (ps, user) => {
new Brackets((qb) => {
for (const type of ps.fileType!) {
const i = ps.fileType!.indexOf(type);
qb.orWhere(`:type${i} = ANY(note.attachedFileTypes)`, {
qb.orWhere(`'{":type${i}"}' <@ (note.attachedFileTypes)`, {
[`type${i}`]: type,
});
}
Expand All @@ -164,7 +164,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (ps.excludeNsfw) {
query.andWhere("note.cw IS NULL");
query.andWhere(
'0 = (SELECT COUNT(*) FROM drive_file df WHERE df.id = ANY(note."fileIds") AND df."isSensitive" = TRUE)',
'0 = (SELECT COUNT(*) FROM drive_file df WHERE \'{"df.id"}\' <@ (note."fileIds") AND df."isSensitive" = TRUE)',
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default define(meta, paramDef, async (ps, user) => {
new Brackets((qb) => {
for (const type of ps.fileType!) {
const i = ps.fileType!.indexOf(type);
qb.orWhere(`:type${i} = ANY(note.attachedFileTypes)`, {
qb.orWhere(`'{":type${i}"}' <@ (note.attachedFileTypes)`, {
[`type${i}`]: type,
});
}
Expand All @@ -146,7 +146,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (ps.excludeNsfw) {
query.andWhere("note.cw IS NULL");
query.andWhere(
'0 = (SELECT COUNT(*) FROM drive_file df WHERE df.id = ANY(note."fileIds") AND df."isSensitive" = TRUE)',
'0 = (SELECT COUNT(*) FROM drive_file df WHERE \'{"df.id"}\' <@ (note."fileIds") AND df."isSensitive" = TRUE)',
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default define(meta, paramDef, async (ps, user) => {
new Brackets((qb) => {
for (const type of ps.fileType!) {
const i = ps.fileType!.indexOf(type);
qb.orWhere(`:type${i} = ANY(note.attachedFileTypes)`, {
qb.orWhere(`'{":type${i}"}' <@ (note.attachedFileTypes)`, {
[`type${i}`]: type,
});
}
Expand All @@ -202,7 +202,7 @@ export default define(meta, paramDef, async (ps, user) => {
if (ps.excludeNsfw) {
query.andWhere("note.cw IS NULL");
query.andWhere(
'0 = (SELECT COUNT(*) FROM drive_file df WHERE df.id = ANY(note."fileIds") AND df."isSensitive" = TRUE)',
'0 = (SELECT COUNT(*) FROM drive_file df WHERE \'{"df.id"}\' <@ (note."fileIds") AND df."isSensitive" = TRUE)',
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/server/api/endpoints/users/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default define(meta, paramDef, async (ps, me) => {
new Brackets((qb) => {
for (const type of ps.fileType!) {
const i = ps.fileType!.indexOf(type);
qb.orWhere(`:type${i} = ANY(note.attachedFileTypes)`, {
qb.orWhere(`'{":type${i}"}' <@ (note.attachedFileTypes)`, {
[`type${i}`]: type,
});
}
Expand All @@ -115,7 +115,7 @@ export default define(meta, paramDef, async (ps, me) => {
if (ps.excludeNsfw) {
query.andWhere("note.cw IS NULL");
query.andWhere(
'0 = (SELECT COUNT(*) FROM drive_file df WHERE df.id = ANY(note."fileIds") AND df."isSensitive" = TRUE)',
'0 = (SELECT COUNT(*) FROM drive_file df WHERE \'{"df.id"}\' <@ (note."fileIds") AND df."isSensitive" = TRUE)',
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/server/api/endpoints/users/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default define(meta, paramDef, async (ps, me) => {
.getCount();

const readMessageCount = await MessagingMessages.createQueryBuilder("messaging_message")
.where(" :userId = ANY(messaging_message.reads) ", { userId: user.id })
.where(" '{\":userId\"}' <@ (messaging_message.reads) ", { userId: user.id })
.cache(CACHE_TIME)
.getCount();

Expand Down Expand Up @@ -427,7 +427,7 @@ export default define(meta, paramDef, async (ps, me) => {
.cache(CACHE_TIME)
.getCount(),
readMessageCount: await MessagingMessages.createQueryBuilder("messaging_message")
.where(" :userId = ANY(messaging_message.reads) ", { userId: user.id })
.where(" '{\":userId\"}' <@ (messaging_message.reads) ", { userId: user.id })
.andWhere("messaging_message.createdAt >= :borderDate", { borderDate: borderDate.toISOString() })
.cache(CACHE_TIME)
.getCount(),
Expand Down

0 comments on commit 02efe4b

Please sign in to comment.