Restructured the node query to be much faster
ci/woodpecker/push/ociImageTag Pipeline is pending Details
ci/woodpecker/tag/ociImageTag Pipeline was successful Details

This commit is contained in:
Natty 2023-12-28 04:02:03 +01:00
parent c443079db0
commit f9fc12f2fd
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
1 changed files with 37 additions and 30 deletions

View File

@ -3,10 +3,11 @@ import {Notes} from "@/models/index.js";
import define from "../../define.js"; import define from "../../define.js";
import {ApiError} from "../../error.js"; import {ApiError} from "../../error.js";
import {getUser} from "../../common/getters.js"; import {getUser} from "../../common/getters.js";
import {makePaginationQuery} from "../../common/make-pagination-query.js";
import {generateVisibilityQuery} from "../../common/generate-visibility-query.js"; import {generateVisibilityQuery} from "../../common/generate-visibility-query.js";
import {generateMutedUserQuery} from "../../common/generate-muted-user-query.js"; import {generateMutedUserQuery} from "../../common/generate-muted-user-query.js";
import {generateBlockedUserQuery} from "../../common/generate-block-query.js"; import {generateBlockedUserQuery} from "../../common/generate-block-query.js";
import {makePaginationQuery} from "@/server/api/common/make-pagination-query.js";
import {Note} from "@/models/entities/note.js";
export const meta = { export const meta = {
tags: ["users", "notes"], tags: ["users", "notes"],
@ -66,7 +67,32 @@ export default define(meta, paramDef, async (ps, me) => {
throw e; throw e;
}); });
//#region Construct query const cte =
Notes.createQueryBuilder("n")
.select(`"n"."id"`, "id")
.andWhere("n.userId = :userId", { userId: user.id });
if (ps.withFiles || ps.fileType != null) {
cte.andWhere("n.fileIds != '{}'");
}
if (!ps.includeReplies) {
cte.andWhere("n.replyId IS NULL");
}
if (ps.includeMyRenotes === false) {
cte.andWhere(
new Brackets((qb) => {
qb.orWhere("n.userId != :userId", { userId: user.id });
qb.orWhere("n.renoteId IS NULL");
qb.orWhere("n.text IS NOT NULL");
qb.orWhere("n.fileIds != '{}'");
qb.orWhere(
'0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = n.id)',
);
}),
);
}
const query = makePaginationQuery( const query = makePaginationQuery(
Notes.createQueryBuilder("note"), Notes.createQueryBuilder("note"),
ps.sinceId, ps.sinceId,
@ -74,21 +100,14 @@ export default define(meta, paramDef, async (ps, me) => {
ps.sinceDate, ps.sinceDate,
ps.untilDate, ps.untilDate,
) )
.andWhere("note.userId = :userId", { userId: user.id }) .addCommonTableExpression(cte, "noteCte");
.innerJoinAndSelect("note.user", "user");
generateVisibilityQuery(query, me); query.andWhere(`"note"."id" IN` + query.subQuery()
if (me) { .select(`"noteCte"."id"`)
generateMutedUserQuery(query, me, user); .from("noteCte", "noteCte")
generateBlockedUserQuery(query, me); .getQuery());
}
if (ps.withFiles) {
query.andWhere("note.fileIds != '{}'");
}
if (ps.fileType != null) { if (ps.fileType != null) {
query.andWhere("note.fileIds != '{}'");
query.andWhere( query.andWhere(
new Brackets((qb) => { new Brackets((qb) => {
for (const type of ps.fileType!) { for (const type of ps.fileType!) {
@ -108,22 +127,10 @@ export default define(meta, paramDef, async (ps, me) => {
} }
} }
if (!ps.includeReplies) { generateVisibilityQuery(query, me);
query.andWhere("note.replyId IS NULL"); if (me) {
} generateMutedUserQuery(query, me, user);
generateBlockedUserQuery(query, me);
if (ps.includeMyRenotes === false) {
query.andWhere(
new Brackets((qb) => {
qb.orWhere("note.userId != :userId", { userId: user.id });
qb.orWhere("note.renoteId IS NULL");
qb.orWhere("note.text IS NOT NULL");
qb.orWhere("note.fileIds != '{}'");
qb.orWhere(
'0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)',
);
}),
);
} }
//#endregion //#endregion