tidb-sql
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTiDB SQL (MySQL-compat-focused)
TiDB SQL(聚焦MySQL兼容性)
Goal: generate SQL that runs correctly on TiDB by default, and avoid "works on MySQL but breaks on TiDB" constructs.
目标:生成默认可在TiDB上正确运行的SQL,避免出现「在MySQL上可行但在TiDB上失效」的语法结构。
Workflow (use every time)
工作流程(每次必用)
- Identify the target engine and version:
- Run
SELECT VERSION(); - If the result contains , treat it as TiDB and parse the version (needed for feature gates like Vector / Foreign Key).
TiDB - If connecting to TiDB Cloud, ensure the client enables SSL with certificate + identity verification (see ).
skills/tidb-sql/references/tidb-cloud-ssl.md
- Run
- Ask 2 quick capability questions if the request depends on them:
- "Do you have TiFlash?" (needed for vector indexes)
- "Is this TiDB Cloud Starter/Essential in a supported region for Full-Text Search?" (availability is limited)
- Generate SQL using TiDB-safe defaults:
- Avoid unsupported MySQL features (procedures/triggers/events/UDF/GEOMETRY/SPATIAL, etc.)
- Treat views as read-only
- Treat primary key changes as migration/rebuild work
- If the user provides MySQL SQL, do a compatibility pass:
- Replace unsupported features with TiDB alternatives
- Call out behavior differences and version prerequisites explicitly
- If SQL is slow or fails unexpectedly, use TiDB-native diagnostics:
- Use for structured plans and operator trees.
EXPLAIN FORMAT = "tidb_json" - Use to compare
EXPLAIN ANALYZEvsestRows(it executes the query).actRows - If the plan looks wrong, consider to refresh statistics.
ANALYZE TABLE ...
- Use
- 识别目标引擎及版本:
- 执行
SELECT VERSION(); - 如果结果包含,则按TiDB处理并解析版本(部分特性如Vector/外键需依赖版本)。
TiDB - 若连接TiDB Cloud,确保客户端通过证书+身份验证启用SSL(详见)。
skills/tidb-sql/references/tidb-cloud-ssl.md
- 执行
- 若请求依赖以下能力,先快速询问两个问题:
- "您是否使用TiFlash?"(向量索引需要依赖它)
- "您使用的TiDB Cloud Starter/Essential是否在支持全文搜索的区域?"(该特性的可用范围有限)
- 使用TiDB安全默认值生成SQL:
- 避免使用TiDB不支持的MySQL特性(存储过程/触发器/事件/UDF/GEOMETRY/SPATIAL等)
- 将视图视为只读
- 主键变更需按迁移/重建流程处理
- 如果用户提供MySQL SQL,执行兼容性检查:
- 用TiDB替代方案替换不支持的特性
- 明确指出行为差异和版本前提条件
- 若SQL运行缓慢或意外失败,使用TiDB原生诊断工具:
- 使用获取结构化执行计划和算子树。
EXPLAIN FORMAT = "tidb_json" - 使用对比
EXPLAIN ANALYZE与estRows(该命令会实际执行查询)。actRows - 如果执行计划不符合预期,考虑执行刷新统计信息。
ANALYZE TABLE ...
- 使用
High-signal differences (keep in mind)
关键差异点(需牢记)
- Vector: TiDB supports /
VECTORtypes and vector functions/indexes; MySQL does not.VECTOR(D) - No GEOMETRY/SPATIAL: avoid , spatial functions, and
GEOMETRYindexes.SPATIAL - No procedures / functions / triggers / events: move logic to the application layer or an external scheduler.
- Full-text search (TiDB feature): use TiDB full-text search SQL when available; don't assume MySQL works everywhere.
FULLTEXT - Views are read-only: no against views.
UPDATE/INSERT/DELETE - Foreign keys: supported in TiDB v6.6.0+; otherwise, don't rely on FK enforcement.
- Primary key changes are restricted: assume "create new table + backfill + swap" for PK changes.
- AUTO_RANDOM: prefer over
AUTO_RANDOMfor write-hotspot avoidance when appropriate.AUTO_INCREMENT - Transactions: TiDB supports pessimistic and optimistic modes; handle optimistic failures in application logic.
COMMIT
- 向量类型:TiDB支持/
VECTOR类型及向量函数/索引;MySQL不支持。VECTOR(D) - 无GEOMETRY/SPATIAL支持:避免使用、空间函数及
GEOMETRY索引。SPATIAL - 无存储过程/函数/触发器/事件支持:将相关逻辑迁移至应用层或外部调度器。
- 全文搜索(TiDB特性):在可用情况下使用TiDB全文搜索SQL;不要假设MySQL的在所有环境都可行。
FULLTEXT - 视图为只读:不能对视图执行操作。
UPDATE/INSERT/DELETE - 外键:TiDB v6.6.0及以上版本支持;低于该版本请勿依赖外键约束。
- 主键变更受限:主键变更需采用「创建新表+数据回填+切换表」的方式。
- AUTO_RANDOM:在合适场景下,优先使用替代
AUTO_RANDOM以避免写入热点。AUTO_INCREMENT - 事务:TiDB支持乐观和悲观两种模式;需在应用逻辑中处理乐观事务的失败情况。
COMMIT
Use these references (inside this skill)
参考文档(本技能内)
- - VECTOR types, functions, vector index DDL, and query patterns.
skills/tidb-sql/references/vector.md - - Full-text search SQL patterns and availability gotchas.
skills/tidb-sql/references/full-text-search.md - -
skills/tidb-sql/references/auto-random.mdrules, DDL patterns, and restrictions.AUTO_RANDOM - - pessimistic vs optimistic mode and session/global knobs.
skills/tidb-sql/references/transactions.md - - other "MySQL vs TiDB" differences that commonly break SQL.
skills/tidb-sql/references/mysql-compatibility-notes.md - - EXPLAIN / EXPLAIN ANALYZE usage, tidb_json and dot formats.
skills/tidb-sql/references/explain.md - - FLASHBACK TABLE/DATABASE and FLASHBACK CLUSTER recovery playbooks.
skills/tidb-sql/references/flashback.md - - TiDB Cloud SSL verification requirements and client flags.
skills/tidb-sql/references/tidb-cloud-ssl.md
- - VECTOR类型、函数、向量索引DDL及查询模式。
skills/tidb-sql/references/vector.md - - 全文搜索SQL模式及可用性注意事项。
skills/tidb-sql/references/full-text-search.md - -
skills/tidb-sql/references/auto-random.md规则、DDL模式及限制。AUTO_RANDOM - - 悲观与乐观模式及会话/全局参数。
skills/tidb-sql/references/transactions.md - - 其他常见的「MySQL vs TiDB」差异点,这些差异常导致SQL执行失败。
skills/tidb-sql/references/mysql-compatibility-notes.md - - EXPLAIN/EXPLAIN ANALYZE的用法、tidb_json及dot格式。
skills/tidb-sql/references/explain.md - - FLASHBACK TABLE/DATABASE及FLASHBACK CLUSTER恢复指南。
skills/tidb-sql/references/flashback.md - - TiDB Cloud SSL验证要求及客户端参数。
skills/tidb-sql/references/tidb-cloud-ssl.md