🦀 ClawHub
MySQL
by @ivangdavila
Write correct MySQL queries avoiding common pitfalls with character sets, indexes, and locking.
TERMINAL
clawhub install mysql📖 About This Skill
name: MySQL slug: mysql version: 1.0.1 description: Write correct MySQL queries with proper character sets, indexing, transactions, and production patterns. metadata: {"clawdbot":{"emoji":"🐬","requires":{"bins":["mysql"]},"os":["linux","darwin","win32"]}}
Quick Reference
| Topic | File |
|-------|------|
| Index design deep dive | indexes.md |
| Transactions and locking | transactions.md |
| Query optimization | queries.md |
| Production config | production.md |
Character Set Traps
utf8 is broken—only 3 bytes, can't store emoji; always use utf8mb4utf8mb4_unicode_ci for case-insensitive sorting; utf8mb4_bin for exact byte comparisonSET NAMES utf8mb4 or connection string parameterIndex Differences from PostgreSQL
WHERE active = true in index definitionINDEX (description(100))—without length, errorINDEX (a, b, c) to cover cUPSERT Patterns
INSERT ... ON DUPLICATE KEY UPDATE—not standard SQL; needs unique key conflictLAST_INSERT_ID() for auto-increment—no RETURNING clause like PostgreSQLREPLACE INTO deletes then inserts—changes auto-increment ID, triggers DELETE cascadeLocking Traps
SELECT ... FOR UPDATE locks rows—but gap locks may lock more than expectedinnodb_lock_wait_timeout for adjustmentFOR UPDATE SKIP LOCKED exists in MySQL 8+—queue patternGROUP BY Strictness
sql_mode includes ONLY_FULL_GROUP_BY by default in MySQL 5.7+ANY_VALUE(column) to silence error when you know values are sameInnoDB vs MyISAM
SHOW TABLE STATUS—convert with ALTER TABLE ... ENGINE=InnoDBQuery Quirks
LIMIT offset, count different order than PostgreSQL's LIMIT count OFFSET offset!= and <> both work; prefer <> for SQL standardALTER TABLE commits immediately, can't rollbackTINYINT(1)—TRUE/FALSE are just 1/0IFNULL(a, b) instead of COALESCE for two args—though COALESCE worksConnection Management
wait_timeout kills idle connections—default 8 hours; pooler may not noticemax_connections default 151—often too low; each uses memorySHOW PROCESSLIST to see active connections—kill long-running with KILL Replication Awareness
Seconds_Behind_Master before relying on replica readsPerformance
EXPLAIN ANALYZE only in MySQL 8.0.18+—older versions just EXPLAIN without actual timesOPTIMIZE TABLE for fragmented tables—locks table; use pt-online-schema-change for big tablesinnodb_buffer_pool_size—set to 70-80% of RAM for dedicated DB server