dbt-slow-models
Identifies the top N slowest dbt models by execution time, including bytes processed where available, to help optimize performance.
Instructions
Top N slowest models in a dbt run by execution_time, with bytes_processed when available
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| topN | No | ||
| invocationId | No | Use a specific run; default is latest |
Implementation Reference
- src/tools/dbt-runs.ts:122-149 (handler)The main handler function for dbt-slow-models. It loads run results (either from the latest run or a specific invocationId), filters to model nodes with execution_time, sorts descending, slices to topN, and returns invocation metadata plus model execution details.
export async function dbtSlowModels(args: z.infer<typeof dbtSlowModelsSchema>): Promise<unknown> { let runFile; if (args.invocationId) { const all = listRunHistory(200); const match = all.find((r) => r.invocationId === args.invocationId); if (!match) throw new Error(`Run not found for invocation_id=${args.invocationId}`); runFile = { metadata: { generated_at: match.generatedAt, invocation_id: match.invocationId }, results: match.results }; } else { const data = loadRunResults(); runFile = { metadata: data.metadata, results: data.results }; } const models = runFile.results .filter((r) => r.unique_id.startsWith("model.")) .filter((r) => typeof r.execution_time === "number") .sort((a, b) => (b.execution_time ?? 0) - (a.execution_time ?? 0)) .slice(0, args.topN); return { invocation: runFile.metadata, count: models.length, models: models.map((r) => ({ uniqueId: r.unique_id, status: r.status, executionTimeSec: r.execution_time, bytesProcessed: r.adapter_response?.bytes_processed, rowsAffected: r.adapter_response?.rows_affected, })), }; } - src/tools/dbt-runs.ts:117-120 (schema)Zod schema for dbt-slow-models: accepts topN (1-100, default 20) and optional invocationId to target a specific run.
export const dbtSlowModelsSchema = z.object({ topN: z.coerce.number().int().min(1).max(100).default(20), invocationId: z.string().optional().describe("Use a specific run; default is latest"), }); - src/index.ts:89-89 (registration)Registration of the dbt-slow-models tool with the MCP server, binding the schema and handler.
tool("dbt-slow-models", "Top N slowest models in a dbt run by execution_time, with bytes_processed when available", dbtSlowModelsSchema.shape, wrapToolHandler(dbtSlowModels)); - src/index.ts:34-34 (registration)Import of dbtSlowModelsSchema and dbtSlowModels from the dbt-runs module.
dbtSlowModelsSchema, dbtSlowModels, - src/clients/dbt-artifacts.ts:171-173 (helper)loadRunResults() helper that reads run_results.json from the dbt target directory, used by the handler when no invocationId is specified.
export function loadRunResults(): DbtRunResultsFile { return readWithCache<DbtRunResultsFile>("runResults", targetPath("run_results.json")); }