mysql-patterns
它能做什么
这个工具帮助你创建和优化 MySQL 与 MariaDB 数据库。它提供了组织数据表、创建高效索引以及编写快速查询的模式。
适用场景
- ✓在设计表结构和索引时。
- ✓在排查数据库查询为什么很慢时。
- ✓在配置应用与数据库的连接时。
技能链接
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/mysql-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我怎样在 users 表中创建一个索引,让按姓名和创建日期搜索更快?”
每周 5 个新技能,1 分钟读完。
工作原理
这个工具会根据代码示例和说明给出最佳实践建议。它会展示如何定义数据表、应该使用哪些数据类型,以及怎样编写性能良好的查询。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
MySQL Patterns
Use this skill when working on MySQL or MariaDB schema design, migrations, slow-query investigation, queue-style transactions, connection pools, or production database configuration. Prefer exact version checks before applying a feature-specific pattern because MySQL and MariaDB have diverged in several SQL details.
Activation
- Designing MySQL or MariaDB tables, indexes, and constraints
- Reviewing migrations before they run on large production tables
- Debugging slow queries, lock waits, deadlocks, or connection exhaustion
- Adding keyset pagination, upserts, full-text search, JSON columns, or queues
- Configuring application connection pools, read replicas, TLS, or slow logs
Version Check
Start by identifying the engine and version:
SELECT VERSION();
SHOW VARIABLES LIKE 'version_comment';
Keep MySQL and MariaDB guidance separate when syntax differs:
- MySQL documents row aliases as the replacement for
VALUES(col)inON DUPLICATE KEY UPDATE;VALUES(col)is deprecated there. - MariaDB documents
VALUES(col)as the supported way to reference inserted values inON DUPLICATE KEY UPDATE; use it for cross-engine compatibility. SKIP LOCKEDis appropriate for queue-like work only. It skips locked rows and can return an inconsistent view, so do not use it for general accounting or integrity-sensitive reads.
Schema Defaults
CREATE TABLE orders (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
account_id BIGINT UNSIGNED NOT NULL,
status VARCHAR(32) NOT NULL,
total DECIMAL(15, 2) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
deleted_at DATETIME NULL,
PRIMARY KEY (id),
KEY idx_orders_account_status_created (account_id, status, created_at),
KEY idx_orders_active (account_id, deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Default choices:
| Use Case | Prefer | Avoid |
|---|---|---|
| Surrogate primary keys | BIGINT UNSIGNED AUTO_INCREMENT |
INT for tables that can grow beyond 2B rows |
| UUID lookup keys | BINARY(16) with conversion helpers |
VARCHAR(36) primary keys on hot tables |
| Money and exact quantities | DECIMAL(p, s) |
FLOAT or DOUBLE |
| User-facing text | utf8mb4 tables and indexes |
MySQL utf8 / utf8mb3 defaults |
| Application timestamps | DATETIME with UTC managed by the app |
Assuming DATETIME stores time zone metadata |
| Soft deletes | deleted_at DATETIME NULL plus scoped indexes |
Filtering soft-deleted rows without an index |
| Extensible status values | lookup table or constrained VARCHAR |
ENUM when values change often |
Indexing
Composite index order usually follows equality predicates first, then range or sort columns:
CREATE INDEX idx_orders_account_status_created
ON orders (account_id, status, created_at);
SELECT id, total
FROM orders
WHERE account_id = ?
AND status = 'pending'
AND created_at >= ?
ORDER BY created_at DESC
LIMIT 50;
Use EXPLAIN before adding or changing an index:
EXPLAIN
SELECT id, total
FROM orders
WHERE account_id = 123 AND status = 'pending'
ORDER BY created_at DESC
LIMIT 50;
Signals to investigate:
| Field | Risk Signal |
|---|---|
type |
ALL on a large table |
key |
NULL when a selective predicate exists |
rows |
Very high row estimate for an interactive path |
Extra |
Using temporary, Using filesort, or broad Using where |
Avoid adding indexes blindly. Each index increases write cost, migration time, backup size, and buffer-pool pressure.
Query Patterns
Upsert
Cross-engine-compatible form:
INSERT INTO user_settings (user_id, setting_key, setting_value)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE
setting_value = VALUES(setting_value),
updated_at = CURRENT_TIMESTAMP;
MySQL row-alias form:
INSERT INTO user_settings (user_id, setting_key, setting_value)
VALUES (?, ?, ?) AS new
ON DUPLICATE KEY UPDATE
setting_value = new.setting_value,
updated_at = CURRENT_TIMESTAMP;
Use the row-alias form only after confirming the target is MySQL. Use
VALUES(col) for MariaDB or mixed MySQL/MariaDB fleets.
Keyset Pagination
SELECT id, name, created_at
FROM products
WHERE (created_at, id) < (?, ?)
ORDER BY created_at DESC, id DESC
LIMIT 50;
…
常见问题
这里的“模式”(patterns)是什么意思?
模式是针对常见数据库使用问题经过验证、被推荐的解决方案,比如如何组织数据,或者如何让搜索更快。
为什么我需要关注 MySQL/MariaDB 的版本?
不同版本对 SQL 命令的理解可能会有细微差别,所以了解你正在使用的版本,才能应用正确的模式。
什么是“键集分页”(keyset pagination)?
这是一种高效的分页取数据方式,尤其适合大表。它比传统的 `offset` 方法更快。
详情
作者的原始描述
MySQL and MariaDB schema, query, indexing, transaction, replication, and connection-pool patterns for production backends. Use when designing MySQL or MariaDB schemas and indexes, or when a query, transaction, or replica lags.
repo: affaan-m/ECC
热度
- GitHub 星标
- 280k
- 排名
- #215
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
accessibility
280k帮你的智能体创建人人都能使用的界面。确保网站和应用对残障人士友好,比如失明或行动不便的用户也能顺畅使用。
agent-architecture-audit
280k让你的智能体检查一切是否正常运行。它能找出并修复 AI 系统中的问题,确保系统按预期工作。
agent-eval
280k帮你的智能体比较不同的 AI 编程工具。它能衡量哪个工具在特定任务上更快、更便宜、更可靠。
agent-harness-construction
280k让你的智能体更好地理解如何完成任务。它会优化智能体使用的工具和指令,提高成功的几率。