파일 검색 쿼리 최적화

This commit is contained in:
static
2026-01-15 15:54:45 +09:00
parent 37bd6a9315
commit b3e3671c09

View File

@@ -475,95 +475,99 @@ export const searchFiles = async (
excludeCategoryIds: number[]; excludeCategoryIds: number[];
}, },
) => { ) => {
const ctes: string[] = []; const baseQuery = db
const conditions: string[] = []; .withRecursive("directory_tree", (db) =>
db
if (filters.parentId === "root") { .selectFrom("directory")
conditions.push(`user_id = ${userId}`); .select("id")
} else { .where("user_id", "=", userId)
ctes.push(` .where((eb) => eb.val(filters.parentId !== "root")) // directory_tree will be empty if parentId is "root"
directory_tree AS ( .$if(filters.parentId !== "root", (qb) => qb.where("id", "=", filters.parentId as number))
SELECT id FROM directory WHERE user_id = ${userId} AND id = ${filters.parentId} .unionAll(
UNION ALL db
SELECT d.id FROM directory d INNER JOIN directory_tree dt ON d.parent_id = dt.id .selectFrom("directory as d")
)`); .innerJoin("directory_tree as dt", "d.parent_id", "dt.id")
conditions.push(`parent_id IN (SELECT id FROM directory_tree)`); .select("d.id"),
} ),
)
filters.includeCategoryIds.forEach((categoryId, index) => { .withRecursive("include_category_tree", (db) =>
ctes.push(` db
include_category_tree_${index} AS ( .selectFrom("category")
SELECT id FROM category WHERE user_id = ${userId} AND id = ${categoryId} .select(["id", "id as root_id"])
UNION ALL .where("id", "=", (eb) => eb.fn.any(eb.val(filters.includeCategoryIds)))
SELECT c.id FROM category c INNER JOIN include_category_tree_${index} ct ON c.parent_id = ct.id .where("user_id", "=", userId)
)`); .unionAll(
conditions.push(` db
EXISTS( .selectFrom("category as c")
SELECT 1 FROM file_category .innerJoin("include_category_tree as ct", "c.parent_id", "ct.id")
WHERE file_id = file.id .select(["c.id", "ct.root_id"]),
AND EXISTS (SELECT 1 FROM include_category_tree_${index} ct WHERE ct.id = category_id) ),
)`); )
}); .withRecursive("exclude_category_tree", (db) =>
db
if (filters.excludeCategoryIds.length > 0) { .selectFrom("category")
ctes.push(` .select("id")
exclude_category_tree AS ( .where("id", "=", (eb) => eb.fn.any(eb.val(filters.excludeCategoryIds)))
SELECT id FROM category WHERE user_id = ${userId} AND id IN (${filters.excludeCategoryIds.join(",")}) .where("user_id", "=", userId)
UNION ALL .unionAll((db) =>
SELECT c.id FROM category c INNER JOIN exclude_category_tree ct ON c.parent_id = ct.id db
)`); .selectFrom("category as c")
conditions.push(` .innerJoin("exclude_category_tree as ct", "c.parent_id", "ct.id")
NOT EXISTS( .select("c.id"),
SELECT 1 FROM file_category ),
WHERE file_id = id )
AND EXISTS (SELECT 1 FROM exclude_category_tree ct WHERE ct.id = category_id) .selectFrom("file")
)`); .selectAll("file")
} .$if(filters.parentId === "root", (qb) => qb.where("user_id", "=", userId)) // directory_tree isn't used if parentId is "root"
.$if(filters.parentId !== "root", (qb) =>
const query = ` qb.where("parent_id", "in", (eb) => eb.selectFrom("directory_tree").select("id")),
${ctes.length > 0 ? `WITH RECURSIVE ${ctes.join(",")}` : ""} )
SELECT * FROM file .where((eb) =>
WHERE ${conditions.join(" AND ")} eb.not(
`; eb.exists(
const { rows } = await sql eb
.raw<{ .selectFrom("file_category")
id: number; .whereRef("file_id", "=", "file.id")
parent_id: number | null; .where("category_id", "in", (eb) =>
user_id: number; eb.selectFrom("exclude_category_tree").select("id"),
path: string; ),
master_encryption_key_version: number; ),
encrypted_data_encryption_key: string; ),
data_encryption_key_version: Date; );
hmac_secret_key_version: number; const files =
content_hmac: string; filters.includeCategoryIds.length > 0
content_type: string; ? await baseQuery
encrypted_content_iv: string; .innerJoin("file_category", "file.id", "file_category.file_id")
encrypted_content_hash: string; .innerJoin(
encrypted_name: Ciphertext; "include_category_tree",
encrypted_created_at: Ciphertext | null; "file_category.category_id",
encrypted_last_modified_at: Ciphertext; "include_category_tree.id",
}>(query) )
.execute(db); .groupBy("file.id")
return rows.map( .having(
(file) => (eb) => eb.fn.count("include_category_tree.root_id").distinct(),
({ "=",
id: file.id, filters.includeCategoryIds.length,
parentId: file.parent_id ?? "root", )
userId: file.user_id, .execute()
path: file.path, : await baseQuery.execute();
mekVersion: file.master_encryption_key_version, return files.map((file) => ({
encDek: file.encrypted_data_encryption_key, id: file.id,
dekVersion: file.data_encryption_key_version, parentId: file.parent_id ?? "root",
hskVersion: file.hmac_secret_key_version, userId: file.user_id,
contentHmac: file.content_hmac, path: file.path,
contentType: file.content_type, mekVersion: file.master_encryption_key_version,
encContentIv: file.encrypted_content_iv, encDek: file.encrypted_data_encryption_key,
encContentHash: file.encrypted_content_hash, dekVersion: file.data_encryption_key_version,
encName: file.encrypted_name, hskVersion: file.hmac_secret_key_version,
encCreatedAt: file.encrypted_created_at, contentHmac: file.content_hmac,
encLastModifiedAt: file.encrypted_last_modified_at, contentType: file.content_type,
}) satisfies File, encContentIv: file.encrypted_content_iv,
); encContentHash: file.encrypted_content_hash,
encName: file.encrypted_name,
encCreatedAt: file.encrypted_created_at,
encLastModifiedAt: file.encrypted_last_modified_at,
}));
}; };
export const setFileEncName = async ( export const setFileEncName = async (