Query Optimization 标志

Query Optimization

社区
nahid-sparktales
query-optimization

Make one slow query fast without guessing — capture the plan, find where the time actually goes, change one thing, and measure again on comparable data. Use when a query, endpoint or report is slow and the database is the suspect, when a plan needs reading, or when someone proposes an index with no evidence. Not for modelling decisions about tables and constraints, not an engine feature reference, and not for system-wide performance work where the database has not yet been shown to be the bottleneck.

概览

发布者nahid-sparktales
代码仓库agent-dispatcher
技能名称query-optimization
星标数
58
复刻数
5
附带文件
1
许可协议MIT
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 1 个附带文件

    模型执行任务时可读取的脚本、模板和参考资料。这些文件为只读文件,绝不会被执行。

  • 开源

    由 nahid-sparktales 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Query Optimization AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/nahid-sparktales/agent-dispatcher/tree/main/skills/database/query-optimization
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/nahid-sparktales/agent-dispatcher.git /tmp/agent-dispatcher
mkdir -p .claude/skills
cp -r /tmp/agent-dispatcher/skills/database/query-optimization .claude/skills/query-optimization
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Query Optimization,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Query Optimization。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Query Optimization 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Query optimization

An index added on a hunch is a permanent write cost bought with no evidence. The work here is measurement: the plan before, one change, the plan after, on data that resembles the data that is actually slow.

When this fires

A specific statement is slow, or a plan needs interpreting, or a proposed index needs justifying. It does not fire before the database has been shown to be where the time goes — if that is still open, measure the request end to end first. It does not fire for redesigning the tables.

Procedure

  1. Get the exact statement and its parameters. Not "the dashboard is slow" — the SQL text as the database received it, with the bind values, and where it runs from. A statement-statistics view, the ORM's query log or the application log will have it. The parameters matter: the same query can take different plans for different values.
  2. Confirm the database is the bottleneck. Compare the statement's own time against the total request time. If the query is 40ms of a 3s request, stop here and say so. Note whether the cost is one slow statement or many fast ones — an N+1 is fixed in the calling code, not by an index.
  3. Reproduce it on comparable data. Row counts, value distribution and index state have to resemble production, because a table small enough to sit in memory makes every plan look fine. If comparable data is not available, that limitation is the headline of your report, not a footnote.
  4. Capture the plan before changing anything, with actual execution and buffer statistics, and keep the output. This is your before, and without it there is no after. Where the statement modifies data, run it inside a transaction you roll back — and on a shared or production database, ask before running anything at all.
  5. Read the plan for the node that actually costs, not the top line. Work from the largest actual time, remembering that a node's reported time is per loop and multiplies by its loop count. Then compare estimated rows against actual rows at that node: a large gap means the planner's information is wrong, and fixing the information often beats fixing the query.
  6. Classify what you found before proposing anything. A sequential scan of a large table under a selective predicate points at a missing or unusable index. A huge row count discarded by a filter after an index scan points at the wrong index. A nested loop with an enormous loop count usually points at a bad estimate upstream. A sort or hash spilling to disk points at memory limits or an avoidable sort. A function evaluated per row points at the expression, not the index.
  7. Try the cheapest fix first. Remove work before adding structure: fewer columns, fewer rows, a bounded result, a join that was never needed, a repeated query collapsed into one. Then make the predicate usable by an index — a column wrapped in a function cannot use an ordinary index on that column, so either unwrap it or index the expression. Only then add or adjust an index. Materialized or denormalized copies come after that, and engine configuration last.
  8. Choose the index to match the predicate and the ordering. Equality columns come before range columns in a multicolumn index; the ordering the query needs can remove a sort. A partial index fits a query that always carries the same filter. Before creating anything, list the existing indexes and check whether one already covers the access pattern — a near-duplicate index is pure cost.
  9. Change one thing and re-measure identically. Same data, same parameters, same method, several runs. Report cold and warm separately or not at all, because the second run of anything is faster and that difference is not your fix.
  10. Check what else moved. An index changes write latency and can change plans for other statements. If you dropped or replaced one, name the queries that were using it. If the fix was a schema or configuration change, say what else it touches.
  11. Stop before applying it to a shared database. Creating, dropping or rebuilding an index on a live system takes locks and time. Hand over the statement, the expected lock behaviour, and the measured benefit, and let the decision to run it be made explicitly.

Checklist

  • Exact statement and parameters captured
  • The database was shown to be the bottleneck, with the share of total time
  • Dataset size and distribution stated, and their comparability to production assessed
  • Before plan captured with actual execution statistics and kept
  • The expensive node identified, with estimated-versus-actual rows read
  • Exactly one change made per measurement
  • Existing indexes listed before a new one was proposed
  • After plan and timings captured the same way as the before
  • Write cost and effects on other queries considered
  • Nothing applied to a shared or production database without authorization

Failure handling

  • The query is fast when you run it. Something else is the real difference — parameters, cache state, concurrency, connection setup, or the client fetching every row. Do not conclude "no problem found"; report what you measured and what still differs from the slow environment.
  • Only a small dataset is available. Say it. Results from a table that fits in memory are not transferable, and an index recommendation from one is a guess wearing a measurement's clothes.
  • Estimates are far from actuals. Refresh statistics and re-plan before touching indexes. Stale statistics produce bad plans that new indexes will not repair, and correlated columns need a different remedy from a missing index.
  • The improvement is within run-to-run noise. It is not an improvement. Run more iterations or drop the change.
  • The real fix is the data model. Say so, and do not paper over it with indexes — hand it to the schema work with the plan as evidence.
  • No access to production-like data or plans. Report the query as analyzed, not as optimized. An unmeasured change is a proposal.

Evidence to report

The statement and parameters; the before and after plans as output, not paraphrase; timings with the number of runs and the cache state; the dataset's row counts; the single change made; existing indexes considered; write-side and cross-query effects; anything left unmeasured. Keep the words honest — analyzed, changed, measured and deployed describe four different states, and only a measured before and after supports the word faster.

附带文件

技能加载后,模型会按需读取这些文件。它们仅作为可读文件提供,绝不会被执行。

常见问题

Query Optimization AI 技能有什么作用?

Make one slow query fast without guessing — capture the plan, find where the time actually goes, change one thing, and measure again on comparable data. Use when a query, endpoint or report is slow and the database is the suspect, when a plan needs reading, or when someone proposes an index with no evidence. Not for modelling decisions about tables and constraints, not an engine feature reference, and not for system-wide performance work where the database has not yet been shown to be the bottleneck.

为什么在 TypingMind 中使用 Query Optimization?

因为您只需安装一次,就能搭配任何模型使用。Query Optimization 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Query Optimization?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/nahid-sparktales/agent-dispatcher/tree/main/skills/database/query-optimization。TypingMind 会读取其中的 SKILL.md,打包相关文件,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Query Optimization?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Query Optimization 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Query Optimization 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Query Optimization AI 技能免费吗?

是的。nahid-sparktales 已在 GitHub 上根据 MIT 许可证发布了该技能。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇