🦀 ClawHub
MariaDB
by @ivangdavila
Write efficient MariaDB queries with proper indexing, temporal tables, and clustering.
TERMINAL
clawhub install mariadb📖 About This Skill
name: MariaDB description: Write efficient MariaDB queries with proper indexing, temporal tables, and clustering. metadata: {"clawdbot":{"emoji":"🦭","requires":{"bins":["mariadb"]},"os":["linux","darwin","win32"]}}
Character Set
utf8mb4 for tables and connections—full Unicode including emojiutf8mb4_unicode_ci for proper linguistic sorting, utf8mb4_bin for byte comparisonSET NAMES utf8mb4 or in connection stringIndexing
INDEX (description(100))(a, b) serves WHERE a=? but not WHERE b=?SHOW INDEXSequences
CREATE SEQUENCE seq_name for guaranteed unique IDs across tablesNEXT VALUE FOR seq_name to get next—survives transaction rollbackSETVAL(seq_name, n) to reset—useful for migrationsSystem Versioning (Temporal Tables)
ALTER TABLE t ADD SYSTEM VERSIONING to track all historical changesFOR SYSTEM_TIME AS OF '2024-01-01 00:00:00' queries past stateFOR SYSTEM_TIME BETWEEN start AND end for change historyrow_start and row_end store validity periodJSON Handling
JSON_VALUE(col, '$.key') extracts scalar, returns NULL if not foundJSON_QUERY(col, '$.obj') extracts object/array with quotes preservedJSON_TABLE() converts JSON array to rows—powerful for unnestingJSON_VALID() before insert if column isn't strictly typedGalera Cluster
wsrep_sync_wait = 1 before critical reads—ensures node is syncedwsrep_cluster_size should be odd number—avoids split-brainWindow Functions
ROW_NUMBER() OVER (PARTITION BY x ORDER BY y) for ranking within groupsLAG(col, 1) OVER (ORDER BY date) for previous row valueSUM(amount) OVER (ORDER BY date ROWS UNBOUNDED PRECEDING) for running totalWITH cte AS (...) for readable complex queriesThread Pool
thread_handling=pool-of-threads—better than thread-per-connectionthread_pool_size = CPU cores for CPU-bound, higher for I/O-boundSHOW STATUS LIKE 'Threadpool%'Storage Engines
SHOW TABLE STATUS WHERE Name='table'Locking
SELECT ... FOR UPDATE locks rows until commitLOCK TABLES t WRITE for DDL-like exclusive access—blocks all other sessionsinnodb_lock_wait_timeout default 50s—lower for interactive appsQuery Optimization
EXPLAIN ANALYZE for actual execution times (10.1+)optimizer_trace for deep dive: SET optimizer_trace='enabled=on'FORCE INDEX (idx) when optimizer chooses wrong indexSTRAIGHT_JOIN to force join order—last resortBackup and Recovery
mariadb-dump --single-transaction for consistent backup without locksmariadb-backup for hot InnoDB backup—incremental supportedmysqlbinlog binlog.000001 | mariadbCommon Errors
max_connections or use connection poolSHOW ENGINE INNODB STATUSON DUPLICATE KEY UPDATE