mysql-patterns
БесплатноБез исполняемых скриптовНе проверенMySQL database patterns for query optimization, schema design, indexing, and security. Quick reference for common patterns.
от joneqian★ 32 звезды на GitHubОб этом скилле
MySQL Patterns
Quick reference for MySQL best practices. For detailed guidance, use the mysql-database-reviewer agent.
When to Activate
- Writing SQL queries or migrations
- Designing database schemas
- Troubleshooting slow queries
- Setting up connection pooling
- Implementing multi-tenant isolation
Quick Reference
Index Cheat Sheet
| Query Pattern | Index Type | Example |
|---|---|---|
WHERE col = value |
B-tree (default) | CREATE INDEX idx ON t (col) |
WHERE col > value |
B-tree | CREATE INDEX idx ON t (col) |
WHERE a = x AND b > y |
Composite | CREATE INDEX idx ON t (a, b) |
MATCH(...) AGAINST(...) |
FULLTEXT | CREATE FULLTEXT INDEX ft ON t (col) |
| Long string prefix | Prefix | CREATE INDEX idx ON t (col(50)) |
| Geographic data | SPATIAL | CREATE SPATIAL INDEX idx ON t (col) |
Data Type Quick Reference
| Use Case | Correct Type | Avoid |
|---|---|---|
| IDs | bigint unsigned |
int, random UUID as PK |
| Business code | varchar(64) |
char, text |
| Strings | varchar(n) |
text for short strings |
| Timestamps | datetime |
timestamp (2038 problem) |
| Money | decimal(10,2) |
float, double |
| Flags | tinyint |
varchar, boolean literal |
| Status | tinyint |
enum (hard to modify) |
| JSON data | json |
text for structured data |
Common Patterns
Composite Index Order:
-- Equality columns first, then range columns
CREATE INDEX idx_status_created ON t_order (status, created_at);
-- Works for: WHERE status = 'pending' AND created_at > '2024-01-01'
-- Does NOT work for: WHERE created_at > '2024-01-01' alone
Prefix Index (Long Strings):
CREATE INDEX idx_url ON t_page (url(100));
-- Index only first 100 characters
Generated Column + Index (JSON):
ALTER TABLE t_product
ADD COLUMN brand varchar(100) GENERATED ALWAYS AS (attributes->>'$.brand') STORED;
CREATE INDEX idx_brand ON t_product (brand);
UPSERT (ON DUPLICATE KEY):
INSERT INTO t_setting (user_code, `key`, `value`)
VALUES ('u123', 'theme', 'dark')
ON DUPLICATE KEY UPDATE
`value` = VALUES(`value`),
updated_at = NOW();
Cursor Pagination:
SELECT * FROM t_product WHERE id > ? ORDER BY id LIMIT 20;
-- O(1) vs OFFSET which is O(n)
Queue Processing (MySQL 8.0+):
START TRANSACTION;
SELECT * FROM t_job
WHERE status = 'pending'
ORDER BY created_at LIMIT 1
FOR UPDATE SKIP LOCKED;
-- Process job...
UPDATE t_job SET status = 'processing' WHERE id = ?;
COMMIT;
Batch Insert:
INSERT INTO t_event (user_code, action) VALUES
('u1', 'click'),
('u2', 'view'),
('u3', 'click');
-- 1 round trip instead of 3
Anti-Pattern Detection
-- Find tables without primary key
SELECT t.table_name
FROM information_schema.tables t
LEFT JOIN information_schema.key_column_usage k
ON t.table_schema = k.table_schema
AND t.table_name = k.table_name
AND k.constraint_name = 'PRIMARY'
WHERE t.table_schema = DATABASE()
AND k.constraint_name IS NULL
AND t.table_type = 'BASE TABLE';
-- Find slow queries (performance_schema)
SELECT DIGEST_TEXT, COUNT_STAR,
ROUND(AVG_TIMER_WAIT/1000000000, 2) as avg_ms
FROM performance_schema.events_statements_summary_by_digest
WHERE AVG_TIMER_WAIT > 100000000 -- > 100ms
ORDER BY AVG_TIMER_WAIT DESC LIMIT 10;
-- Find unused indexes (sys schema)
SELECT * FROM sys.schema_unused_indexes
WHERE object_schema = DATABASE();
-- Find redundant indexes
SELECT * FROM sys.schema_redundant_indexes
WHERE table_schema = DATABASE();
-- Check table fragmentation
SELECT table_name, data_free,
ROUND(data_free/(data_length+index_length+data_free)*100, 2) as frag_pct
FROM information_schema.tables
WHERE table_schema = DATABASE() AND data_free > 1000000
ORDER BY data_free DESC;
Configuration Template
-- Connection limits
SET GLOBAL max_connections = 200;
SET GLOBAL max_user_connections = 50;
-- Timeouts
SET GLOBAL wait_timeout = 300;
SET GLOBAL interactive_timeout = 600;
SET GLOBAL max_execution_time = 30000; -- 30s query timeout (MySQL 5.7.8+)
-- Slow query log
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
SET GLOBAL log_queries_not_using_indexes = 'ON';
-- InnoDB settings (my.cnf recommended)
-- innodb_buffer_pool_size = 70% of RAM
-- innodb_log_file_size = 256M
-- innodb_flush_log_at_trx_commit = 1
Table Template
CREATE TABLE `t_example` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
`example_code` varchar(64) NOT NULL COMMENT '
Установить mysql-patterns в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл mysql-patterns?
MySQL database patterns for query optimization, schema design, indexing, and security. Quick reference for common patterns.
Как установить скилл mysql-patterns?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл mysql-patterns запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
frontend-design
Guidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making…
от Anthropicwebapp-testing
Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing…
от Anthropicbrand-guidelines
Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand…
от Anthropicweb-artifacts-builder
Suite of tools for creating elaborate, multi-component claude.ai HTML artifacts using modern frontend web technologies (React, Tailwind CSS, shadcn/ui). Use…
от Anthropic