MySQL Patterns
MySQL/MariaDB schema, query, transaction, pool, and ops patterns for production. Detalhes em references/.
When to Activate
- 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
Core Principles
- Version first —
SELECT VERSION(); MySQL and MariaDB diverge, never assume
- Equality-first indexes — composite order follows predicates;
EXPLAIN before adding
- Keyset over OFFSET — deep
OFFSET scans and discards; cursor + matching index
- Short transactions — deterministic lock order, no external calls inside
- Least privilege — app user gets CRUD on its DB only, TLS across hosts
Example
SELECT id, name, created_at
FROM products
WHERE (created_at, id) < (?, ?)
ORDER BY created_at DESC, id DESC
LIMIT 50;
-- index: CREATE INDEX idx_products_created_id ON products (created_at, id);
References
references/schema-indexing.md — version check, schema defaults, composite indexes, EXPLAIN signals
references/queries.md — upsert (both engines), keyset pagination, JSON, full-text
references/transactions-pools.md — locking order, deadlocks, SKIP LOCKED queues, pools
references/ops-security.md — diagnostics, replication, users/TLS, config, anti-patterns, related
Checklist
1---2name: mysql-patterns3description: "Use when designing MySQL/MariaDB schemas, debugging slow queries, locks, or pools, and configuring replication or security. Triggers on \"mysql-patterns\", \"mysql patterns\", \"patterns\"."4metadata:5 source: https://skillmd.com/skills/majinmagros/mysql-patterns6 origin: ECC7---89# MySQL Patterns1011MySQL/MariaDB schema, query, transaction, pool, and ops patterns for production. Detalhes em `references/`.1213## When to Activate1415- Designing MySQL or MariaDB tables, indexes, and constraints16- Reviewing migrations before they run on large production tables17- Debugging slow queries, lock waits, deadlocks, or connection exhaustion18- Adding keyset pagination, upserts, full-text search, JSON columns, or queues19- Configuring application connection pools, read replicas, TLS, or slow logs2021## Core Principles22231. **Version first** — `SELECT VERSION()`; MySQL and MariaDB diverge, never assume242. **Equality-first indexes** — composite order follows predicates; `EXPLAIN` before adding253. **Keyset over OFFSET** — deep `OFFSET` scans and discards; cursor + matching index264. **Short transactions** — deterministic lock order, no external calls inside275. **Least privilege** — app user gets CRUD on its DB only, TLS across hosts2829## Example3031```sql32SELECT id, name, created_at33FROM products34WHERE (created_at, id) < (?, ?)35ORDER BY created_at DESC, id DESC36LIMIT 50;37-- index: CREATE INDEX idx_products_created_id ON products (created_at, id);38```3940## References4142- `references/schema-indexing.md` — version check, schema defaults, composite indexes, `EXPLAIN` signals43- `references/queries.md` — upsert (both engines), keyset pagination, JSON, full-text44- `references/transactions-pools.md` — locking order, deadlocks, `SKIP LOCKED` queues, pools45- `references/ops-security.md` — diagnostics, replication, users/TLS, config, anti-patterns, related4647## Checklist4849- [ ] Engine/version confirmed before engine-specific syntax50- [ ] Explicit columns, matching index, `EXPLAIN` clean (no `ALL`/filesort on hot paths)51- [ ] Transactions short, locks ordered, deadlock retry bounded52- [ ] Pool recycle below `wait_timeout`; read-after-write pinned to primary53- [ ] App user least-privilege + TLS; secrets in manager, never in repo