mysql-patterns
Skill này làm gì
Công cụ này giúp bạn tạo và tối ưu cơ sở dữ liệu MySQL và MariaDB. Nó cung cấp các mẫu để tổ chức bảng, tạo chỉ mục hiệu quả và viết truy vấn nhanh.
Khi nào nên dùng
- ✓Khi thiết kế cấu trúc bảng và chỉ mục.
- ✓Khi tìm hiểu vì sao truy vấn cơ sở dữ liệu chậm.
- ✓Khi cấu hình kết nối của ứng dụng tới cơ sở dữ liệu.
Liên kết skill
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/mysql-patterns/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Làm sao để tạo một chỉ mục giúp tìm kiếm theo tên và ngày tạo nhanh hơn trong bảng users của tôi?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Công cụ đưa ra các thực hành tốt nhất dựa trên ví dụ code và giải thích. Nó chỉ cách định nghĩa bảng, chọn kiểu dữ liệu và viết truy vấn có hiệu năng tốt.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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;
…
Câu hỏi thường gặp
'Mẫu' trong ngữ cảnh này là gì?
Mẫu là các giải pháp đã được chứng minh và khuyến nghị cho những vấn đề thường gặp khi dùng cơ sở dữ liệu, như tổ chức dữ liệu hoặc làm tìm kiếm nhanh hơn.
Vì sao tôi cần quan tâm đến phiên bản MySQL/MariaDB?
Các phiên bản khác nhau có thể hiểu lệnh SQL hơi khác nhau, vì vậy cần biết bạn đang dùng phiên bản nào để áp dụng đúng mẫu.
'Keyset pagination' là gì?
Đây là cách lấy dữ liệu theo từng trang một cách hiệu quả, đặc biệt với bảng lớn. Nó nhanh hơn các phương pháp truyền thống dùng 'offset'.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 280k
- Vị trí xếp hạng
- #215
- Giấy phép
- MIT
- Cập nhật
- 15/10/2026
Bình luận
- …
Thêm skill từ kho mã này
accessibility
280kGiúp agent của bạn tạo giao diện mà ai cũng dùng được, đảm bảo website và app hoạt động tốt với người khuyết tật như người mù hoặc khó vận động.
agent-architecture-audit
280kGiúp agent của bạn kiểm tra xem mọi thứ có hoạt động đúng không, đồng thời tìm và sửa các lỗi trong hệ thống AI để chúng chạy như mong đợi.
agent-eval
280kGiúp agent của bạn so sánh các công cụ lập trình AI khác nhau, đo xem công cụ nào nhanh hơn, rẻ hơn và đáng tin cậy hơn cho từng tác vụ.
agent-harness-construction
280kGiúp agent của bạn hiểu rõ hơn cách thực hiện tác vụ, tối ưu các công cụ và chỉ dẫn mà agent dùng để tăng khả năng thành công.
Skill tương tự
shopify
260kGiúp agent của bạn đọc và cập nhật dữ liệu cửa hàng Shopify như sản phẩm, tồn kho, đơn hàng và khách hàng qua Shopify GraphQL API.
rest-graphql-debug
260kGiúp agent của bạn tìm lý do một lệnh gọi API REST hoặc GraphQL thất bại, kiểm tra kết nối, đăng nhập, định dạng yêu cầu và phản hồi theo từng lớp.
har-derived-api-client
260kGiúp agent của bạn ghi lại lưu lượng mạng của một website một lần, rồi xây một client API trực tiếp, để không phải nhấp qua trang mỗi lần.