polardbx-sql
Original:🇨🇳 Chinese
Translated
Write, review, and adapt SQL for PolarDB-X 2.0 Distributed Edition (Enterprise Edition) in AUTO mode, properly handle differences between PolarDB-X and MySQL (such as partitioned tables, Global Secondary Index (GSI), Columnar Index (CCI), Sequence, distributed transactions, table groups, TTL tables, etc.). Suitable for scenarios where you need to generate SQL to run on PolarDB-X, migrate MySQL SQL to PolarDB-X, or debug PolarDB-X SQL compatibility issues.
4installs
Sourcepolardb/polardbx-skills
Added on
NPX Install
npx skill4agent add polardb/polardbx-skills polardbx-sqlTags
Translated version includes tags in frontmatterSKILL.md Content (Chinese)
View Translation Comparison →PolarDB-X SQL (Focus on MySQL Compatibility)
Objective: Generate SQL that can run correctly on PolarDB-X 2.0 Distributed Edition (Enterprise Edition) in AUTO mode, avoiding the issue of "runs on MySQL but errors on PolarDB-X".
Scope of Application
This skill only applies to the following version and mode:
- PolarDB-X 2.0 Enterprise Edition (also known as Distributed Edition) + AUTO mode database
Not applicable to:
- PolarDB-X 1.0 (DRDS 1.0)
- PolarDB-X 2.0 Standard Edition
- PolarDB-X 2.0 Enterprise Edition in DRDS mode database
Main differences between AUTO mode and DRDS mode: AUTO mode uses MySQL-compatible syntax to define partitions, while DRDS mode uses the legacy syntax. You can confirm the database mode via the following method:
PARTITION BYdbpartition/tbpartitionsql
SHOW CREATE DATABASE db_name;
-- MODE = 'auto' in the output indicates AUTO modeWorkflow (Follow Each Time You Use)
- Confirm target engine and version:
- Execute to determine the instance type:
SELECT VERSION();- Result contains and version number > 5.4.12 (e.g.,
TDDL) -> 2.0 Enterprise Edition (Distributed Edition), this skill is applicable. Parse the enterprise edition version number (e.g., 5.4.19) from it.5.7.25-TDDL-5.4.19-20251031 - Result contains and version number <= 5.4.12 (e.g.,
TDDL) -> DRDS 1.0, this skill is not applicable.5.6.29-TDDL-5.4.12-16327949 - Result contains (e.g.,
X-Cluster) -> 2.0 Standard Edition, this skill is not applicable, please use the8.0.32-X-Cluster-8.4.20-20251017skill.polardbx-standard
- Result contains
- After confirming it's 2.0 Enterprise Edition, execute to confirm it's in AUTO mode (MODE = 'auto').
SHOW CREATE DATABASE db_name; - Version number affects feature availability (e.g., NEW SEQUENCE requires 5.4.14+, CCI requires a newer version).
- Execute
- Confirm table type requirements:
- Small tables or dictionary tables -> Broadcast Table (fully replicated to each DN).
BROADCAST - Tables that don't need distribution -> Single Table (stored only on one DN).
SINGLE - Other cases -> Partitioned Table (default), select appropriate partition key and partition strategy.
- Small tables or dictionary tables -> Broadcast Table
- Use PolarDB-X safe defaults when generating SQL:
- Avoid unsupported MySQL features (stored procedures/triggers/EVENT/SPATIAL, etc.).
- Use or
KEYpartitioning instead of MySQL's AUTO_INCREMENT primary key write hotspots.HASH - Consider creating a Global Secondary Index (GSI) when querying with non-partition keys.
- Use Sequence instead of relying on AUTO_INCREMENT continuity when you need globally unique IDs.
- If the user provides MySQL SQL, perform compatibility checks:
- Replace unsupported features and provide PolarDB-X alternatives.
- Clearly mark behavioral differences and version requirements.
- When SQL is slow or errors occur, use PolarDB-X diagnostic tools:
- to view the logical execution plan.
EXPLAIN - to view the physical execution plan pushed down to DN.
EXPLAIN EXECUTE - to view shard scanning status and determine if full shard scanning exists.
EXPLAIN SHARDING - to actually execute and collect runtime statistics.
EXPLAIN ANALYZE
Quick Reference for Core Differences
- Three table types: Single Table (), Broadcast Table (
SINGLE), Partitioned Table (default); choose based on data volume and access pattern.BROADCAST - Partitioned tables: Support KEY/HASH/RANGE/LIST/RANGE COLUMNS/LIST COLUMNS/CO_HASH + sub-partitioning (49 combinations).
- Global Secondary Index (GSI): Solves the full shard scanning problem caused by non-partition key queries, supports three types: GSI / UGSI / Clustered GSI.
- Columnar Index (CCI): Hybrid row-column storage, accelerates OLAP analysis queries via .
CLUSTERED COLUMNAR INDEX - Sequence: Globally unique sequence, default type is (5.4.14+), a distributed alternative to AUTO_INCREMENT.
NEW SEQUENCE - Distributed transactions: Based on TSO global clock + MVCC + 2PC, strongly consistent by default; single-shard transactions are automatically optimized to local transactions.
- Table groups: Tables with the same partitioning rules are bound to the same table group, ensuring JOIN computation is pushed down and avoiding cross-shard data movement.
- TTL tables: Automatically expire and clean up cold data based on time columns, can cooperate with CCI to achieve hot-cold data separation.
- Unsupported MySQL features: Stored procedures/triggers/EVENT/SPATIAL/GEOMETRY/LOAD XML/HANDLER, etc.
- Does not support STRAIGHT_JOIN / NATURAL JOIN: Use standard JOIN syntax instead.
- Does not support := assignment operator: Move the logic to the application layer.
- Subqueries are not supported in HAVING/JOIN ON clauses: Rewrite subqueries as JOIN or CTE.
Reference Files (Within This Skill)
- - Table creation syntax, table types (single/broadcast/partitioned tables), partitioning strategies, sub-partitioning, partition management operations.
skills/polardbx-sql/references/create-table.md - - Creation, querying, and restrictions of Global Secondary Index (GSI/UGSI/Clustered GSI).
skills/polardbx-sql/references/gsi.md - - Creation, usage, and applicable scenarios of Columnar Index (CCI).
skills/polardbx-sql/references/cci.md - - Sequence types (NEW/GROUP/SIMPLE/TIME), creation, and usage.
skills/polardbx-sql/references/sequence.md - - Distributed transaction model, isolation levels, and notes.
skills/polardbx-sql/references/transactions.md - - Compatibility differences and development restrictions between MySQL and PolarDB-X.
skills/polardbx-sql/references/mysql-compatibility-notes.md - - Usage of EXPLAIN series commands and execution plan diagnosis.
skills/polardbx-sql/references/explain.md - - TTL table definition, cold data archiving, and cleanup scheduling.
skills/polardbx-sql/references/ttl-table.md - - Online DDL judgment, lock-free execution strategy, long transaction check, DMS lock-free changes, etc.
skills/polardbx-sql/references/online-ddl.md