mysql-patterns
Skill1 starsChanged 3 months agoMITView on GitHub
MySQL and MariaDB schema, query, indexing, transaction, replication, and connection-pool patterns for production backends.
- Reads credentials
9 other projects use a version of this, 6 of them edited.
Adopted by
Most recently taken up by
- renepf/claude-skillsedited, first seen Oct 25, 2026
- davidanthonyn/claude-skillsword for word, first seen Oct 20, 2026
- Ronnasayd/aippedited, first seen Oct 20, 2026
- anbanmv/claude-db-patternstheir own version, first seen Oct 15, 2026
- gongyijie85/dsh-eccedited, first seen Oct 13, 2026
- lintangknitto/agent-skillsedited, first seen Sep 23, 2026
Other versions9 projects use a version of this.
Word for word · 1
- davidanthonyn/claude-skills★ 0100% same
Edited · 6
- Fmarzochi/EGC★ 61100% same
- gongyijie85/dsh-ecc★ 899% same
- ronmkr/PromptBook★ 2100% same
- Ronnasayd/aipp★ 199% same
- lintangknitto/agent-skills★ 098% same
- renepf/claude-skills★ 0100% same
Wrote their own version · 2
- pekral/cursor-rules★ 10same name
- anbanmv/claude-db-patterns★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository280 other files it gives its agents.
- agent-architecture-auditSkill
- agent-evalSkill
- agent-harness-constructionSkill
- agentic-engineeringSkill
- agentic-osSkill
- agent-introspection-debuggingSkill
- agent-payment-x402Skill
- agent-sortSkill
- ai-first-engineeringSkill
- ai-regression-testingSkill
- autonomous-agent-harnessSkill
- autonomous-loopsSkill
- braindump-autoSkill
- braindumpSkill
- continuous-agent-loopSkill
- continuous-learning-v2Skill
- data-scraper-agentSkill
- enterprise-agent-opsSkill
- eval-harnessSkill
- foundation-models-on-deviceSkill
- ito-data-atlas-agentSkill
- llm-trading-agent-securitySkill
- mcp-server-patternsSkill
- mle-workflowSkill
- recsys-pipeline-architectSkill
- regex-vs-llm-structured-textSkill
- scholar-evaluationSkill
- superbraindumpSkill
- team-agent-orchestrationSkill
- carrier-relationship-managementSkill
- customer-billing-opsSkill
- energy-procurementSkill
- finance-billing-opsSkill
- inventory-demand-planningSkill
- logistics-exception-managementSkill
- prediction-market-oracle-researchSkill
- prediction-market-risk-reviewSkill
- production-schedulingSkill
- quality-nonconformanceSkill
- returns-reverse-logisticsSkill
- architecture-decision-recordsSkill
- codebase-onboardingSkill
- codehealth-mcpSkill
- code-tourSkill
- hexagonal-architectureSkill
- repo-scanSkill
- article-writingSkill
- brand-voiceSkill
- content-engineSkill
- crosspostSkill
- investor-materialsSkill
- investor-outreachSkill
- lead-intelligenceSkill
- marketing-campaignSkill
- market-researchSkill
- seoSkill
- social-publisherSkill
- x-apiSkill
- bun-runtimeSkill
- cisco-ios-patternsSkill
- The first 60 shown.
The file
Show its rules and text
We split this skill into the rules it gives an agent
20 rules. 12 are common, 0 are distinctive, and 8 are very specific to this project.
12
Common
Most of the 8 edited versions we read keep these.
0
Distinctive
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Common: line 9
Use this skill when working on MySQL or MariaDB schema design, migrations, slow-query investigation, queue-style transactions, connection …
- Common: line 25
Start by identifying the engine and version:
- Common: line 32
Keep MySQL and MariaDB guidance separate when syntax differs:
- Very specific: line 89
Use EXPLAIN before adding or changing an index:
- Common: line 109
Avoid adding indexes blindly.
- Very specific: line 136
Use the row-alias form only after confirming the target is MySQL.
- Very specific: line 155
Do not use deep OFFSET pagination on large tables.
- Common: line 160
Use JSON columns for extension data, not for fields that need heavy relational filtering or constraints.
- Common: line 188
Use external search when you need typo tolerance, complex ranking, cross-table facets, or language-specific analysis beyond built-in …
- Common: line 193
Keep transactions short and lock rows in a consistent order:
- Common: line 213
Do external API calls before opening the transaction, not inside it.
- Very specific: line 214
Add indexes for predicates used in UPDATE, DELETE, and locking reads.
- Very specific: line 239
Use SKIP LOCKED only for queue-like workloads where skipping a locked row is acceptable.
- Very specific: line 283
Keep application pool recycling below the server wait_timeout.
- Very specific: line 306
Use EXPLAIN ANALYZE only when it is safe to execute the query.
- Common: line 311
Read replicas can lag. Do not route read-your-own-write paths, checkout flows, permission checks, or idempotency-key reads to a replica …
- Common: line 323
Check the engine/version before standardizing on one command.
- Very specific: line 345
Do not grant ALL PRIVILEGES or *.* to application users.
- Common: line 378
Treat configuration values as a prompt for review, not a universal preset.
- Common: line 397
When this skill is used for review, return:
“Common” is measured: kept by at least 6 in 10 of the 8 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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;Back it with an index that matches the cursor:
CREATE INDEX idx_products_created_id ON products (created_at, id);Do not use deep OFFSET pagination on large tables; it makes the server scan
and discard rows before returning the page.
JSON Fields
Use JSON columns for extension data, not for fields that need heavy relational filtering or constraints.
CREATE TABLE events (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
payload JSON NOT NULL,
event_type VARCHAR(64)
GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(payload, '$.type'))) STORED,
KEY idx_events_type (event_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;For frequently queried JSON paths, expose a generated column and index that column. Keep foreign keys, ownership, tenancy, and lifecycle fields relational.
Full-Text Search
ALTER TABLE articles ADD FULLTEXT KEY ft_articles_title_body (title, body);
SELECT id, title, MATCH(title, body) AGAINST (? IN NATURAL LANGUAGE MODE) AS score
FROM articles
WHERE MATCH(title, body) AGAINST (? IN NATURAL LANGUAGE MODE)
ORDER BY score DESC
LIMIT 20;Use external search when you need typo tolerance, complex ranking, cross-table facets, or language-specific analysis beyond built-in full-text behavior.
Transactions
Keep transactions short and lock rows in a consistent order:
START TRANSACTION;
SELECT id, balance
FROM accounts
WHERE id IN (?, ?)
ORDER BY id
FOR UPDATE;
UPDATE accounts SET balance = balance - ? WHERE id = ?;
UPDATE accounts SET balance = balance + ? WHERE id = ?;
COMMIT;Deadlock and lock-wait checklist:
- Lock rows in a deterministic order across code paths.
- Do external API calls before opening the transaction, not inside it.
- Add indexes for predicates used in
UPDATE,DELETE, and locking reads. - On deadlock, roll back and retry the whole transaction with a bounded retry budget.
- Capture
SHOW ENGINE INNODB STATUS\Gsoon after a deadlock; it is overwritten by later events.
Queue-style worker claim:
START TRANSACTION;
SELECT id
FROM jobs
WHERE status = 'pending'
ORDER BY created_at
LIMIT 1
FOR UPDATE SKIP LOCKED;
UPDATE jobs
SET status = 'processing', started_at = CURRENT_TIMESTAMP
WHERE id = ?;
COMMIT;Use SKIP LOCKED only for queue-like workloads where skipping a locked row is
acceptable. It is not a replacement for normal transactional consistency.
Connection Pools
SQLAlchemy example:
from sqlalchemy import create_engine
engine = create_engine(
"mysql+mysqlconnector://app:secret@db.internal/app",
pool_size=10,
max_overflow=5,
pool_timeout=30,
pool_recycle=240,
pool_pre_ping=True,
connect_args={"connect_timeout": 5},
)Node.js mysql2 example:
import mysql from 'mysql2/promise';
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
enableKeepAlive: true,
keepAliveInitialDelay: 30000,
});
const [rows] = await pool.execute(
'SELECT id, total FROM orders WHERE account_id = ? LIMIT 50',
[accountId],
);Keep application pool recycling below the server wait_timeout. If the server
uses wait_timeout = 300, a pool_recycle around 240 seconds is coherent;
pool_pre_ping still helps recover from network and failover events.
Diagnostics
Useful first-pass commands:
SHOW FULL PROCESSLIST;
SHOW ENGINE INNODB STATUS\G;
SHOW VARIABLES LIKE 'slow_query_log';
SHOW VARIABLES LIKE 'long_query_time';Enable the slow log in a controlled environment:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
SET GLOBAL log_queries_not_using_indexes = 'ON';Use EXPLAIN ANALYZE only when it is safe to execute the query. It runs the
statement and can be expensive on production-sized data.
Replication
Read replicas can lag. Do not route read-your-own-write paths, checkout flows, permission checks, or idempotency-key reads to a replica immediately after a write.
-- MySQL legacy terminology, still common in existing fleets
SHOW SLAVE STATUS\G;
-- Newer terminology where supported
SHOW REPLICA STATUS\G;Check the engine/version before standardizing on one command. Monitor replica SQL thread health, IO thread health, and lag, not just whether the TCP connection is alive.
Security
CREATE USER 'app'@'%' IDENTIFIED BY 'use-a-secret-manager';
GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'app'@'%';
ALTER USER 'app'@'%' REQUIRE SSL;
SELECT user, host
FROM mysql.user
WHERE user = '';
DROP USER IF EXISTS ''@'localhost';
DROP USER IF EXISTS ''@'%';Security review points:
- Do not grant
ALL PRIVILEGESor*.*to application users. - Require TLS for application users when traffic crosses hosts or networks.
- Store credentials in the platform secret manager, not in examples, scripts, or repository files.
- Separate migration/admin users from runtime application users.
- Audit public network exposure and bind addresses before tuning performance.
Configuration
Example starting point for a dedicated database host:
[mysqld]
innodb_buffer_pool_size = 4G
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
max_connections = 300
thread_cache_size = 50
wait_timeout = 300
interactive_timeout = 300
innodb_lock_wait_timeout = 10
slow_query_log = ON
long_query_time = 1
log_queries_not_using_indexes = ON
log_bin = mysql-bin
binlog_format = ROW
binlog_expire_logs_seconds = 604800Treat configuration values as a prompt for review, not a universal preset. Size memory, connections, log retention, and durability settings from workload, hardware, backup policy, and recovery objectives.
Anti-Patterns
| Anti-Pattern | Risk | Better Pattern |
SELECT * in hot paths | Over-fetching and brittle clients | Select explicit columns |
Deep OFFSET pagination | Linear scans and slow pages | Keyset pagination |
| No index on foreign-key joins | Slow joins and lock-heavy deletes | Index FK columns intentionally |
| Long transactions | Lock waits and large undo history | Commit small units of work |
Direct DML against mysql.user | Grant-table corruption risk | Use CREATE USER, ALTER USER, DROP USER |
| Application user with admin grants | High blast radius | Least-privilege runtime user |
Pool recycle above wait_timeout | Stale pooled connections | Recycle below timeout and pre-ping |
| Replica reads after writes | Stale user-facing state | Pin read-after-write flows to primary |
Output Expectations
When this skill is used for review, return:
- Engine/version assumptions.
- Highest-risk correctness, lock, security, and migration issues.
- Exact SQL or code changes for the safe path.
- Validation plan:
EXPLAIN, migration dry run, lock/deadlock check, and rollback criteria. - Any MySQL/MariaDB syntax differences that affect the recommendation.
Related
- Skill:
postgres-patterns- PostgreSQL-specific schema and query patterns - Skill:
database-migrations- migration planning and rollout safety - Skill:
backend-patterns- API and service-layer patterns - Skill:
security-review- secret handling, auth, and least privilege - Agent:
database-reviewer- broader database review workflow
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

