planetscale-query-insights-and-tags
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseQuery Insights and tags
查询洞察与标签
Purpose
目的
Use PlanetScale Insights to understand query behavior, then recommend SQLCommenter-compatible tags that make future diagnosis and Traffic Control possible. Do not change database settings or repository code without approval.
借助PlanetScale Insights了解查询行为,随后推荐兼容SQLCommenter的标签,以便未来进行问题诊断和Traffic Control操作。未经批准,请勿修改数据库设置或仓库代码。
What to inspect
检查内容
Query behavior
查询行为
For the selected database and branch, inspect:
- Top queries by total time.
- Top queries by time per execution.
- Top queries by rows read.
- Top queries by execution count.
- For Postgres, top queries by CPU usage (or
sort=cpuTimeon the Insights API).sort=percentCpuTime - Queries with errors.
- Notable queries and active anomalies.
- Query patterns affected by recent deploys.
- Query patterns attached to schema recommendations.
- For sharded Vitess databases, vindex usage for each query pattern: the
percentage of traffic using relevant vindexes and the vindex-usage trend
over time. The API exposes per-pattern and
index_usages; get the trend from the dashboard Vindexes tab or by comparing API windows. Treat missing or declining relevant-vindex usage as an indexing or routing investigation input, not as proof that a new index is required.routing_index_usages
针对选定的数据库和分支,检查以下内容:
- 总耗时最高的查询。
- 单次执行耗时最高的查询。
- 读取行数最多的查询。
- 执行次数最多的查询。
- 对于Postgres数据库,CPU使用率最高的查询(在Insights API中使用或
sort=cpuTime)。sort=percentCpuTime - 存在错误的查询。
- 值得关注的查询和活跃异常。
- 受最近部署影响的查询模式。
- 与架构建议相关的查询模式。
- 对于分片Vitess数据库,每个查询模式的vindex使用情况:使用相关vindex的流量占比,以及vindex使用情况随时间的趋势。API会暴露每个模式的和
index_usages;可从控制台的Vindexes标签页或对比不同API时间窗口获取趋势数据。将相关vindex缺失或使用占比下降的情况作为索引或路由调查的输入依据,但不能直接作为需要新增索引的证明。routing_index_usages
Insights API surface
Insights API接口
Query Insights is public API: read-only GET endpoints under
, authorized by a
service token or OAuth token with /.
organizations/{org}/databases/{db}/branches/{branch}read_databasesread_database- — aggregated statistics per query pattern over the requested window. Set the window with
/insights/from(ISO 8601) orto(for exampleperiod,1h); search SQL patterns with24h; sort server-side withqandsort— sort keys includedir,count,errorCount,rowsRead,totalTime,cpuTime,ioTime,percentTime,percentCpuTime,p50Latency,p99Latency,maxLatency, and theegressBytes/trafficControlWarningsfamily. Filter withtrafficControlThrottled(tablet_type,primary,replica) andrdonly(type,SELECT,INSERT,UPDATE); trim responses withDELETE; paginate withfields/page.per_page - — individual collected executions for a pattern (timestamps, duration, rows, username, client address, error message). Available regardless of raw query collection; raw collection adds literal parameter values to these records.
/insights/{fingerprint}returns the single-pattern aggregate;/insights/{fingerprint}/summaryfetches one execution./insights/queries/{id} - — error fingerprints with counts and messages (
/insights/errorssearches the error message; sort byq,count,lastRun, ortotalTime).timePerQuerylists the failing executions behind one error fingerprint./insights/errors/{fingerprint} - and
/insights/anomalies— anomaly windows with per-query correlation coefficients identifying which patterns moved with the anomaly./insights/anomalies/{id} - — tag keys with observed values (
/insights/tags,values_limit, andliteral_values_only/fingerprintfilters);keyspacefor a single key./insights/tags/{tag}groups the full statistics schema by one or more tag keys via the/insights/tags/summariesparameter — use it to attribute load to routes, jobs, or features without client-side aggregation.tags - — the Traffic Control budgets and rules that affect a fingerprint (Postgres).
/insights/{fingerprint}/traffic/budgets
Aggregates cover the requested window. Duration fields use names like
, with explicit share-of-window percent fields
(); both totals and percentages are reliable
for the window requested.
sum_total_duration_millissum_total_duration_percentThe response schema is shared across engines, but some fields are
engine-specific: CPU/IO durations and block-cache statistics
(, , , …) are
populated for Postgres; shard queries, keyspaces, , and
routing-index (vindex) usage are populated for Vitess.
sum_cpu_duration_millisblocks_readblock_cache_hit_ratiotablet_typeQuery Insights是公开API:在路径下的只读GET接口,通过拥有/权限的服务令牌或OAuth令牌授权访问。
organizations/{org}/databases/{db}/branches/{branch}read_databasesread_database- — 在请求的时间窗口内,按查询模式聚合的统计数据。通过
/insights/from(ISO 8601格式)或to(例如period、1h)设置时间窗口;使用24h搜索SQL模式;通过q和sort进行服务端排序——排序键包括dir、count、errorCount、rowsRead、totalTime、cpuTime、ioTime、percentTime、percentCpuTime、p50Latency、p99Latency、maxLatency,以及egressBytes/trafficControlWarnings系列。可通过trafficControlThrottled(tablet_type、primary、replica)和rdonly(type、SELECT、INSERT、UPDATE)进行过滤;使用DELETE精简响应内容;通过fields/page实现分页。per_page - — 某一模式下收集到的单次执行记录(时间戳、耗时、行数、用户名、客户端地址、错误信息)。无论是否开启原始查询收集,该接口均可用;开启原始收集后,这些记录会包含字面参数值。
/insights/{fingerprint}返回单个模式的聚合统计数据;/insights/{fingerprint}/summary获取单次执行记录。/insights/queries/{id} - — 错误指纹及其计数和信息(
/insights/errors用于搜索错误信息;可按q、count、lastRun或totalTime排序)。timePerQuery列出某一错误指纹对应的失败执行记录。/insights/errors/{fingerprint} - 和
/insights/anomalies— 异常时间窗口,以及每个查询的相关系数,用于识别哪些模式随异常变化。/insights/anomalies/{id} - — 带有已观测值的标签键(支持
/insights/tags、values_limit以及literal_values_only/fingerprint过滤);keyspace用于单个标签键。/insights/tags/{tag}通过/insights/tags/summaries参数按一个或多个标签键对完整统计架构进行分组——无需客户端聚合即可将负载归因到路由、任务或功能。tags - — 影响某一指纹的Traffic Control预算和规则(仅Postgres)。
/insights/{fingerprint}/traffic/budgets
聚合统计覆盖请求的时间窗口。耗时字段使用类似的命名,同时包含明确的窗口占比字段();请求窗口内的总计值和百分比均可靠。
sum_total_duration_millissum_total_duration_percent响应架构在不同引擎间通用,但部分字段为引擎专属:CPU/IO耗时和块缓存统计数据(、、等)仅在Postgres中填充;分片查询、keyspace、和路由索引(vindex)使用情况仅在Vitess中填充。
sum_cpu_duration_millisblocks_readblock_cache_hit_ratiotablet_typeTag coverage
标签覆盖情况
For each expensive or anomalous query, determine:
- Is it tagged?
- Which service produced it?
- Which route, job, controller, or action produced it?
- Which deployment SHA produced it?
- Is the tag cardinality safe?
- Are tags consistent across frameworks and languages?
- Use the tags API to answer these questions: shows which keys and values are present, and
/insights/tagsattributes load per tag value. In the Vitess dashboard, filter the query table with/insights/tags/summaries?tags=...and drill into query details to see tags on individual executions. Built-in query metadata and SQLCommenter tags are both valid attribution sources.tag:key:value
针对每个高耗时或异常查询,确定以下内容:
- 是否已添加标签?
- 由哪个服务生成?
- 由哪个路由、任务、控制器或操作生成?
- 由哪个部署SHA生成?
- 标签的基数是否安全?
- 不同框架和语言间的标签是否一致?
- 使用标签API回答这些问题:显示存在哪些键和值,
/insights/tags按标签值归因负载。在Vitess控制台中,使用/insights/tags/summaries?tags=...过滤查询表格,并深入查询详情查看单次执行的标签。内置查询元数据和SQLCommenter标签均为有效的归因来源。tag:key:value
Raw query collection
原始查询收集
Check whether raw query / complete query collection is enabled. On
Postgres the effective state is the cluster
parameter (per branch, dashboard Extensions tab, default ); the
database API object's field is a separate surface.
When the two differ, report the cluster parameter as the effective state
and do not describe the difference as an inconsistency. On Vitess there
is no cluster parameter; the database API's field
is the effective state.
pginsights.raw_queriesfalseinsights_raw_queriesinsights_raw_queriesReport it as a capability state, not a risk posture. Raw query collection
records literal parameter values per execution, which pattern-level Insights
data does not provide. It is the mechanism for isolating which specific
invocation of a pattern is pathological. Execution-level records are
retrievable from with or without raw collection;
raw collection adds the literal parameter values to those records.
/insights/{fingerprint}When it is disabled, the finding is a capability gap: identify the query
patterns in this assessment where pattern-level data is insufficient
(unexplained latency variance within a fingerprint, tenant- or
parameter-dependent behavior) and state that raw collection would resolve
them. State the operational property once, as fact: literal values become
visible to the observability pipeline. Where the customer's data-handling
requirements constrain this, scoped enablement (incident windows, defined
retention) and leaving collection disabled are both valid outcomes —
record the rationale rather than a default judgment in either direction.
Tags and raw collection are complementary instruments: tags attribute a
pattern to a code path; raw collection identifies the specific invocation.
Assessments should evaluate both.
检查是否开启了原始查询/完整查询收集。在Postgres中,有效状态由集群参数决定(按分支设置,可在控制台Extensions标签页查看,默认值为);数据库API对象的字段是独立的配置入口。当两者不一致时,以集群参数作为有效状态,且不要将这种差异描述为不一致。在Vitess中没有集群参数,数据库API的字段即为有效状态。
pginsights.raw_queriesfalseinsights_raw_queriesinsights_raw_queries将其作为能力状态而非风险态势进行报告。原始查询收集会记录每次执行的字面参数值,而模式级别的Insights数据不提供这些信息。它是用于隔离某一模式下具体哪个调用出现问题的机制。无论是否开启原始收集,均可从获取执行级记录;开启原始收集后,这些记录会添加字面参数值。
/insights/{fingerprint}当原始查询收集处于禁用状态时,发现的问题属于能力缺口:识别本次评估中模式级数据不足以分析的查询模式(同一指纹内延迟差异无法解释、依赖租户或参数的行为),并说明开启原始收集可解决这些问题。需客观陈述一次操作特性:字面参数值会进入可观测 pipeline。如果客户的数据处理要求对此有约束,那么仅在事件窗口内开启、定义保留期限,或者保持禁用状态都是合理的结果——需记录理由,而非默认倾向某一方向。
标签和原始收集是互补工具:标签将模式归因到代码路径;原始收集识别具体调用。评估需同时考量两者。
SQLCommenter tag schema
SQLCommenter标签架构
Recommend this baseline tag set:
- : stable app name.
application - : service or process name.
service - : production, staging, development.
environment - : normalized route template, for example
route, not/accounts/:id/orders./accounts/123/orders - : framework controller name where applicable.
controller - : framework action name where applicable.
action - : background job class or worker name.
job - : background queue.
queue - : bounded feature name for traffic classes like export, report, search, billing, checkout.
feature - : short git SHA or deploy identifier.
release_sha - : app, worker, script, agent, mcp, bi, integration.
source - : free, pro, enterprise, internal, only if bounded.
tenant_tier
Do not recommend these tags by default:
user_idrequest_idtenant_idemailsession_id- raw URL
- unbounded GraphQL operation text
- access token
- secret
If the customer needs tenant-level isolation, recommend a bounded abstraction first, such as tenant tier, cell, shard, or customer class. Tenant ID is only acceptable with explicit approval after cardinality and privacy review.
推荐以下基准标签集:
- :稳定的应用名称。
application - :服务或进程名称。
service - :生产、预发布、开发。
environment - :标准化的路由模板,例如
route,而非/accounts/:id/orders。/accounts/123/orders - :适用时的框架控制器名称。
controller - :适用时的框架操作名称。
action - :后台任务类或工作器名称。
job - :后台队列。
queue - :流量类别的限定功能名称,例如导出、报表、搜索、计费、结账。
feature - :简短的git SHA或部署标识符。
release_sha - :应用、工作器、脚本、agent、mcp、bi、集成。
source - :免费、专业、企业、内部,仅在有限范围内使用。
tenant_tier
默认不推荐以下标签:
user_idrequest_idtenant_idemailsession_id- 原始URL
- 无限制的GraphQL操作文本
- 访问令牌
- 密钥
如果客户需要租户级隔离,建议优先使用有限抽象,例如租户层级、单元、分片或客户类别。仅在经过基数和隐私审查并获得明确批准后,才可使用租户ID标签。
Cardinality rules
基数规则
Flag a tag as unsafe when:
- Values are unbounded.
- Values include IDs, UUIDs, emails, slugs, or raw paths.
- The same query pattern emits many unique tag combinations.
- The tag would make Insights or Traffic Control aggregation noisy.
Recommend normalizing at the application boundary.
当出现以下情况时,标记标签为不安全:
- 值无限制。
- 值包含ID、UUID、邮箱、slug或原始路径。
- 同一查询模式生成大量唯一标签组合。
- 标签会导致Insights或Traffic Control聚合数据产生噪声。
建议在应用边界进行标准化处理。
Analysis output
分析输出
For each top query pattern, produce:
- Fingerprint or normalized query.
- Current metrics.
- Current tags.
- Missing tags.
- Likely source in application code.
- Whether it is a schema recommendation candidate.
- Whether it is a Traffic Control candidate.
- Whether it is an application optimization candidate.
针对每个顶级查询模式,生成以下内容:
- 指纹或标准化查询语句。
- 当前指标。
- 当前标签。
- 缺失的标签。
- 在应用代码中的可能来源。
- 是否适合作为架构建议候选。
- 是否适合作为Traffic Control候选。
- 是否适合作为应用优化候选。
Recommendation classes
建议类别
Add tags
添加标签
Recommend SQLCommenter instrumentation when query attribution is weak.
当查询归因能力较弱时,建议添加SQLCommenter埋点。
Improve tag normalization
优化标签标准化
Recommend replacing high-cardinality tags with bounded values.
建议用有限值替换高基数标签。
Add Traffic Control warning budget
添加Traffic Control警告预算
For Postgres only, recommend mode budgets for expensive but important routes, jobs, analytics, exports, or third-party integrations.
warn仅针对Postgres,建议为高耗时但重要的路由、任务、分析、导出或第三方集成设置模式预算。
warnAdd schema recommendation workflow
添加架构建议流程
For Vitess, recommend turning open schema recommendations into branch/deploy-request work. For Postgres, recommend turning them into reviewed migrations against a non-production branch.
针对Vitess,建议将开放的架构建议转化为分支/部署请求工作。针对Postgres,建议将其转化为针对非生产分支的已审核迁移。
Fix code path
修复代码路径
Recommend a repository PR when the expensive query is caused by N+1, missing pagination, accidental eager load, unbounded export, broad search, or polling.
当高耗时查询由N+1查询、缺失分页、意外预加载、无限制导出、宽泛搜索或轮询导致时,建议提交仓库PR进行修复。
Safety rules
安全规则
Do not:
- Enable raw query collection.
- Add tags to code.
- Change Traffic Control budgets.
- Apply schema recommendations.
- Run production EXPLAIN ANALYZE on expensive queries.
Without explicit approval.
未经明确批准,请勿:
- 开启原始查询收集。
- 向代码中添加标签。
- 修改Traffic Control预算。
- 应用架构建议。
- 在生产环境中对高耗时查询运行EXPLAIN ANALYZE。
Output
输出内容
Return:
- Query risk table.
- Tag coverage table.
- Bad/high-cardinality tag table.
- Recommended tag schema for this application.
- Candidate Traffic Control slices.
- Candidate schema and code changes.
- Proposed changes requiring approval.
End with:
“No Insights, tag, repository, or Traffic Control changes have been applied.”
返回:
- 查询风险表。
- 标签覆盖情况表。
- 不良/高基数标签表。
- 针对该应用的推荐标签架构。
- 候选Traffic Control切片。
- 候选架构和代码变更。
- 需要批准的拟议变更。
结尾需包含:
“未应用任何Insights、标签、仓库或Traffic Control变更。”