Last active
February 22, 2025 14:01
-
-
Save zxt-tzx/71c3009077afc6a482b70cb61ae77902 to your computer and use it in GitHub Desktop.
Drizzle helper functions: explain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { SQLWrapper } from "drizzle-orm"; | |
import { sql } from "drizzle-orm"; | |
import type { DbClient } from "@/db"; | |
export const explainAnalyze = async <T extends SQLWrapper>( | |
db: DbClient, | |
query: T, | |
) => { | |
const debugResult = await db.execute(sql`EXPLAIN ANALYZE ${query.getSQL()}`); | |
// Process each row first to handle vectors | |
const processedRows = debugResult.map((row) => { | |
const plan = row["QUERY PLAN"]; | |
if (typeof plan === "string" && plan.includes("[") && plan.includes(",")) { | |
const vectorMatch = plan.match(/\[([-\d.,e\s]+)\]/); | |
if (vectorMatch) { | |
const vectorStr = vectorMatch[0]; | |
const count = (vectorStr.match(/,/g) || []).length + 1; | |
return { | |
"QUERY PLAN": plan.replace( | |
vectorStr, | |
`[Vector with ${count} dimensions]`, | |
), | |
}; | |
} | |
} | |
return row; | |
}); | |
// Then concatenate the processed results | |
const queryPlan = processedRows.map((row) => row["QUERY PLAN"]).join("\n"); | |
// eslint-disable-next-line no-console | |
console.debug(JSON.stringify({ "QUERY PLAN": queryPlan }, null, 2)); | |
return await query; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment