mysql-patterns
できること
MySQL と MariaDB のデータベースを作成し、最適化するためのツールです。テーブルの整理、効率的なインデックスの作成、高速なクエリの書き方のためのパターンを提供します。
使う場面
- ✓テーブルの構造とインデックスを設計するとき。
- ✓データベースのクエリが遅い原因を調べるとき。
- ✓アプリケーションからデータベースへの接続を設定するとき。
スキルのリンク
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/mysql-patterns/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“「ユーザーテーブルで、名前と作成日による検索を速くするためのインデックスは、どう作ればよいですか?」”
毎週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;
…
よくある質問
この文脈での「パターン」とは何ですか?
データの整理や検索の高速化など、よくあるデータベースの課題に対する、実績のある推奨の解決方法のことです。
MySQL/MariaDBのバージョンを気にする必要があるのはなぜですか?
バージョンによってSQLコマンドの解釈に小さな違いがあるため、正しいパターンを適用するには、使っているバージョンを知っておくことが大切です。
「キーセットページネーション」とは何ですか?
データをページ単位で取得する効率的な方法で、特に大きなテーブルでは、従来の「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エージェントが誰にとっても使いやすいインターフェースを作れるように手助けします。目が見えない方や身体の動きに制限のある方など、障害のある人にとってもWebサイトやアプリが正しく動くようにします。
agent-architecture-audit
280kエージェントがすべて正しく動いているかを確認できるようにします。AIシステムの問題を見つけて修正し、期待どおりに動くようにします。
agent-eval
280kエージェントが異なるAIコーディングツールを比較できるようにします。特定のタスクにおいて、どれがより速く、安く、信頼性が高いかを測定します。
agent-harness-construction
280kエージェントがタスクの進め方をよりよく理解できるようにします。エージェントが使うツールや指示を最適化し、成功する可能性を高めます。
似ているスキル
shopify
260kShopify GraphQL APIを使い、商品、在庫、注文、顧客などのShopifyストアのデータを読み取り、更新するのをエージェントが支援します。
rest-graphql-debug
260k接続、ログイン、リクエストの形式、レスポンスを一層ずつ確認して、RESTまたはGraphQLのAPI呼び出しが失敗する理由を、エージェントが見つけるのを支援します。
har-derived-api-client
260kウェブサイトのネットワークトラフィックを一度記録し、直接のAPIクライアントを作ることで、毎回ページをクリックして操作しなくて済むようにエージェントを助けます。