npx skills add https://github.com/mindrally/skills --skill mysql-best-practices使用 InnoDB 作为默认引擎(符合 ACID,行级锁)
仅对读密集型、非事务性工作负载考虑使用 MyISAM
对高速要求的临时表使用 MEMORY 引擎
CREATE TABLE orders ( order_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, customer_id INT UNSIGNED NOT NULL, order_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, total_amount DECIMAL(12, 2) NOT NULL, status ENUM('pending', 'processing', 'shipped', 'delivered', 'cancelled') NOT NULL DEFAULT 'pending', INDEX idx_customer (customer_id), INDEX idx_date_status (order_date, status), FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE RESTRICT ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
使用满足需求的最小数据类型
可能的情况下,优先使用 INT UNSIGNED 而非 BIGINT
财务计算使用 DECIMAL,而非 FLOAT/DOUBLE
固定值集合使用 ENUM
变长字符串使用 VARCHAR,定长使用 CHAR
始终使用 utf8mb4 字符集以获得完整的 Unicode 支持
-- 适当的数据类型选择 CREATE TABLE products ( product_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, sku VARCHAR(50) NOT NULL, name VARCHAR(255) NOT NULL, description TEXT, price DECIMAL(10, 2) NOT NULL, quantity SMALLINT UNSIGNED NOT NULL DEFAULT 0, weight DECIMAL(8, 3), is_active TINYINT(1) NOT NULL DEFAULT 1, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY uk_sku (sku) ) ENGINE=InnoDB;
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
对 InnoDB 表使用 AUTO_INCREMENT 整数主键
对于分布式系统,考虑使用存储为 BINARY(16) 的 UUID
尽可能避免使用复合主键
-- UUID 存储优化 CREATE TABLE distributed_events ( event_id BINARY(16) PRIMARY KEY, event_type VARCHAR(50) NOT NULL, payload JSON, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
-- 使用 UUID 插入 INSERT INTO distributed_events (event_id, event_type, payload) VALUES (UUID_TO_BIN(UUID()), 'user_signup', '{"user_id": 123}');
-- 使用 UUID 查询 SELECT * FROM distributed_events WHERE event_id = UUID_TO_BIN('550e8400-e29b-41d4-a716-446655440000');
对大多数查询使用 B-tree 索引(默认)
文本搜索使用 FULLTEXT 索引
地理数据使用 SPATIAL 索引
对频繁执行的查询考虑使用覆盖索引
-- 为常见查询模式创建复合索引 CREATE INDEX idx_orders_customer_date ON orders(customer_id, order_date);
-- 覆盖索引 CREATE INDEX idx_orders_covering ON orders(customer_id, order_date, status, total_amount);
-- 用于搜索的全文索引 ALTER TABLE products ADD FULLTEXT INDEX ft_name_desc (name, description);
-- 使用全文搜索 SELECT * FROM products WHERE MATCH(name, description) AGAINST('wireless bluetooth' IN NATURAL LANGUAGE MODE);
为 WHERE、JOIN、ORDER BY 和 GROUP BY 中使用的列建立索引
在复合索引中将选择性最高的列放在前面
避免单独为低基数列建立索引
监控并移除未使用的索引
-- 检查索引使用情况 SELECT table_schema, table_name, index_name, seq_in_index, column_name, cardinality FROM information_schema.STATISTICS WHERE table_schema = 'your_database' ORDER BY table_name, index_name, seq_in_index;
使用 EXPLAIN 分析查询执行计划
查找全表扫描(type: ALL)
检查是否使用了正确的索引
监控检查的行数与返回的行数
EXPLAIN FORMAT=JSON SELECT c.name, COUNT(o.order_id) AS order_count FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE c.created_at > '2024-01-01' GROUP BY c.customer_id;
在生产代码中避免使用 SELECT *
分页使用 LIMIT
可能的情况下,优先使用 JOIN 而非子查询
对重复查询使用预处理语句
-- 高效分页 SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = ? ORDER BY order_date DESC LIMIT 20 OFFSET 0;
-- 键集分页(对大偏移量更高效) SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = ? AND (order_date, order_id) < (?, ?) ORDER BY order_date DESC, order_id DESC LIMIT 20;
-- 避免:对索引列使用函数
SELECT * FROM orders WHERE YEAR(order_date) = 2024;
-- 推荐:范围比较
SELECT * FROM orders
WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01';
-- 避免:隐式类型转换
SELECT * FROM users WHERE user_id = '123'; -- user_id 是 INT
-- 推荐:正确的类型
SELECT * FROM users WHERE user_id = 123;
-- 避免:使用前导通配符的 LIKE
SELECT * FROM products WHERE name LIKE '%phone%';
-- 推荐:使用全文搜索进行文本匹配
SELECT * FROM products WHERE MATCH(name) AGAINST('phone');
对半结构化数据使用 JSON 数据类型(MySQL 5.7+)
为频繁访问的 JSON 字段创建生成列
查询时使用适当的 JSON 函数
CREATE TABLE events ( event_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, event_type VARCHAR(50) NOT NULL, payload JSON NOT NULL, -- 用于索引的生成列 user_id INT UNSIGNED AS (payload->>'$.user_id') STORED, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, INDEX idx_user_id (user_id) );
-- 查询 JSON 数据 SELECT event_id, event_type, JSON_EXTRACT(payload, '$.action') AS action FROM events WHERE JSON_EXTRACT(payload, '$.user_id') = 123;
-- 或使用 -> 操作符 SELECT * FROM events WHERE payload->'$.user_id' = 123;
对事务性表使用 InnoDB
保持事务简短以最小化锁争用
选择适当的隔离级别
优雅地处理死锁
-- 带错误处理的事务 START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1; UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
-- 检查错误并提交或回滚 COMMIT;
-- 设置隔离级别 SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
将读查询定向到副本
使用具有读写分离功能的连接池
监控复制延迟
-- 检查复制状态 SHOW SLAVE STATUS\G
-- 检查复制延迟 SELECT TIMESTAMPDIFF(SECOND, MAX(LAST_APPLIED_TRANSACTION_END_APPLY_TIMESTAMP), NOW()) AS lag_seconds FROM performance_schema.replication_applier_status_by_worker;
使用强密码和安全连接(SSL/TLS)
应用最小权限原则
使用预处理语句防止 SQL 注入
审计敏感操作
-- 创建具有有限权限的用户 CREATE USER 'app_user'@'%' IDENTIFIED BY 'secure_password'; GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'app_user'@'%'; FLUSH PRIVILEGES;
-- 要求 SSL ALTER USER 'app_user'@'%' REQUIRE SSL;
-- 查看用户权限 SHOW GRANTS FOR 'app_user'@'%';
-- 为优化器统计信息分析表
ANALYZE TABLE orders, customers, products;
-- 优化表(回收空间,整理碎片)
OPTIMIZE TABLE orders;
-- 检查表完整性
CHECK TABLE orders;
-- 查找慢查询
SELECT * FROM mysql.slow_log ORDER BY query_time DESC LIMIT 10;
-- 当前进程列表
SHOW FULL PROCESSLIST;
-- InnoDB 状态
SHOW ENGINE INNODB STATUS;
-- 表大小
SELECT
table_name,
ROUND(data_length / 1024 / 1024, 2) AS data_mb,
ROUND(index_length / 1024 / 1024, 2) AS index_mb,
table_rows
FROM information_schema.TABLES
WHERE table_schema = 'your_database'
ORDER BY data_length DESC;
# my.cnf 推荐设置
[mysqld]
# InnoDB 设置
innodb_buffer_pool_size = 70%_of_RAM
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 1
innodb_flush_method = O_DIRECT
# 连接设置
max_connections = 500
wait_timeout = 300
interactive_timeout = 300
# 查询缓存(在 MySQL 8.0+ 中已禁用)
query_cache_type = 0
# 慢查询日志
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
每周安装数
889
仓库
GitHub 星标数
42
首次出现
2026年1月25日
安全审计
安装于
opencode708
codex688
gemini-cli684
github-copilot654
cursor615
kimi-cli611
Use InnoDB as the default engine (ACID compliant, row-level locking)
Consider MyISAM only for read-heavy, non-transactional workloads
Use MEMORY engine for temporary tables with high-speed requirements
CREATE TABLE orders ( order_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, customer_id INT UNSIGNED NOT NULL, order_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, total_amount DECIMAL(12, 2) NOT NULL, status ENUM('pending', 'processing', 'shipped', 'delivered', 'cancelled') NOT NULL DEFAULT 'pending', INDEX idx_customer (customer_id), INDEX idx_date_status (order_date, status), FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE RESTRICT ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Use smallest data type that fits your needs
Prefer INT UNSIGNED over BIGINT when possible
Use DECIMAL for financial calculations, not FLOAT/DOUBLE
Use ENUM for fixed sets of values
Use VARCHAR for variable-length strings, CHAR for fixed-length
Always use utf8mb4 charset for full Unicode support
-- Appropriate data type selection CREATE TABLE products ( product_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, sku VARCHAR(50) NOT NULL, name VARCHAR(255) NOT NULL, description TEXT, price DECIMAL(10, 2) NOT NULL, quantity SMALLINT UNSIGNED NOT NULL DEFAULT 0, weight DECIMAL(8, 3), is_active TINYINT(1) NOT NULL DEFAULT 1, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY uk_sku (sku) ) ENGINE=InnoDB;
Use AUTO_INCREMENT integer primary keys for InnoDB tables
Consider UUIDs stored as BINARY(16) for distributed systems
Avoid composite primary keys when possible
-- UUID storage optimization CREATE TABLE distributed_events ( event_id BINARY(16) PRIMARY KEY, event_type VARCHAR(50) NOT NULL, payload JSON, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
-- Insert with UUID INSERT INTO distributed_events (event_id, event_type, payload) VALUES (UUID_TO_BIN(UUID()), 'user_signup', '{"user_id": 123}');
-- Query with UUID SELECT * FROM distributed_events WHERE event_id = UUID_TO_BIN('550e8400-e29b-41d4-a716-446655440000');
Use B-tree indexes (default) for most queries
Use FULLTEXT indexes for text search
Use SPATIAL indexes for geographic data
Consider covering indexes for frequently executed queries
-- Composite index for common query patterns CREATE INDEX idx_orders_customer_date ON orders(customer_id, order_date);
-- Covering index CREATE INDEX idx_orders_covering ON orders(customer_id, order_date, status, total_amount);
-- Fulltext index for search ALTER TABLE products ADD FULLTEXT INDEX ft_name_desc (name, description);
-- Search using fulltext SELECT * FROM products WHERE MATCH(name, description) AGAINST('wireless bluetooth' IN NATURAL LANGUAGE MODE);
Index columns used in WHERE, JOIN, ORDER BY, and GROUP BY
Place most selective columns first in composite indexes
Avoid indexing low-cardinality columns alone
Monitor and remove unused indexes
-- Check index usage SELECT table_schema, table_name, index_name, seq_in_index, column_name, cardinality FROM information_schema.STATISTICS WHERE table_schema = 'your_database' ORDER BY table_name, index_name, seq_in_index;
Use EXPLAIN to analyze query execution plans
Look for full table scans (type: ALL)
Check for proper index usage
Monitor rows examined vs rows returned
EXPLAIN FORMAT=JSON SELECT c.name, COUNT(o.order_id) AS order_count FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE c.created_at > '2024-01-01' GROUP BY c.customer_id;
Avoid SELECT * in production code
Use LIMIT for pagination
Prefer JOINs over subqueries when possible
Use prepared statements for repeated queries
-- Efficient pagination SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = ? ORDER BY order_date DESC LIMIT 20 OFFSET 0;
-- Keyset pagination (more efficient for large offsets) SELECT order_id, order_date, total_amount FROM orders WHERE customer_id = ? AND (order_date, order_id) < (?, ?) ORDER BY order_date DESC, order_id DESC LIMIT 20;
-- Avoid: Function on indexed column
SELECT * FROM orders WHERE YEAR(order_date) = 2024;
-- Preferred: Range comparison
SELECT * FROM orders
WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01';
-- Avoid: Implicit type conversion
SELECT * FROM users WHERE user_id = '123'; -- user_id is INT
-- Preferred: Proper types
SELECT * FROM users WHERE user_id = 123;
-- Avoid: LIKE with leading wildcard
SELECT * FROM products WHERE name LIKE '%phone%';
-- Preferred: Fulltext search for text matching
SELECT * FROM products WHERE MATCH(name) AGAINST('phone');
Use JSON data type for semi-structured data (MySQL 5.7+)
Create generated columns for frequently accessed JSON fields
Use appropriate JSON functions for queries
CREATE TABLE events ( event_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, event_type VARCHAR(50) NOT NULL, payload JSON NOT NULL, -- Generated column for indexing user_id INT UNSIGNED AS (payload->>'$.user_id') STORED, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, INDEX idx_user_id (user_id) );
-- Query JSON data SELECT event_id, event_type, JSON_EXTRACT(payload, '$.action') AS action FROM events WHERE JSON_EXTRACT(payload, '$.user_id') = 123;
-- Or using -> operator SELECT * FROM events WHERE payload->'$.user_id' = 123;
Use InnoDB for transactional tables
Keep transactions short to minimize lock contention
Choose appropriate isolation level
Handle deadlocks gracefully
-- Transaction with error handling START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1; UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
-- Check for errors and commit or rollback COMMIT;
-- Set isolation level SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Direct read queries to replicas
Use connection pooling with read/write splitting
Monitor replication lag
-- Check replication status SHOW SLAVE STATUS\G
-- Check replication lag SELECT TIMESTAMPDIFF(SECOND, MAX(LAST_APPLIED_TRANSACTION_END_APPLY_TIMESTAMP), NOW()) AS lag_seconds FROM performance_schema.replication_applier_status_by_worker;
Use strong passwords and secure connections (SSL/TLS)
Apply principle of least privilege
Use prepared statements to prevent SQL injection
Audit sensitive operations
-- Create user with limited privileges CREATE USER 'app_user'@'%' IDENTIFIED BY 'secure_password'; GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'app_user'@'%'; FLUSH PRIVILEGES;
-- Require SSL ALTER USER 'app_user'@'%' REQUIRE SSL;
-- View user privileges SHOW GRANTS FOR 'app_user'@'%';
-- Analyze tables for optimizer statistics
ANALYZE TABLE orders, customers, products;
-- Optimize tables (reclaim space, defragment)
OPTIMIZE TABLE orders;
-- Check table integrity
CHECK TABLE orders;
-- Find slow queries
SELECT * FROM mysql.slow_log ORDER BY query_time DESC LIMIT 10;
-- Current process list
SHOW FULL PROCESSLIST;
-- InnoDB status
SHOW ENGINE INNODB STATUS;
-- Table sizes
SELECT
table_name,
ROUND(data_length / 1024 / 1024, 2) AS data_mb,
ROUND(index_length / 1024 / 1024, 2) AS index_mb,
table_rows
FROM information_schema.TABLES
WHERE table_schema = 'your_database'
ORDER BY data_length DESC;
# my.cnf recommended settings
[mysqld]
# InnoDB settings
innodb_buffer_pool_size = 70%_of_RAM
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 1
innodb_flush_method = O_DIRECT
# Connection settings
max_connections = 500
wait_timeout = 300
interactive_timeout = 300
# Query cache (disabled in MySQL 8.0+)
query_cache_type = 0
# Slow query log
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 2
Weekly Installs
889
Repository
GitHub Stars
42
First Seen
Jan 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode708
codex688
gemini-cli684
github-copilot654
cursor615
kimi-cli611
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
Gemini Interactions API 指南:统一接口、智能体交互与服务器端状态管理
833 周安装
Apollo MCP 服务器:让AI代理通过GraphQL API交互的完整指南
834 周安装
智能体记忆系统构建指南:分块策略、向量存储与检索优化
835 周安装
Scrapling官方网络爬虫框架 - 自适应解析、绕过Cloudflare、Python爬虫库
836 周安装
抽奖赢家选取器 - 随机选择工具,支持CSV、Excel、Google Sheets,公平透明
838 周安装
Medusa 前端开发指南:使用 SDK、React Query 构建电商商店
839 周安装