当前位置: 首页 > Skill >

MySQL

MySQL

1.0.1 2026.11.19 浏览:852
获取Skill文件查看使用说明
作者:林知许可证:
本文目录展开

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 utf8mb4
  • utf8mb4_unicode_ci for case-insensitive sorting; utf8mb4_bin for exact byte comparison
  • Collation mismatch in JOINs kills performance—ensure consistent collation across tables
  • Connection charset must match: SET NAMES utf8mb4 or connection string parameter
  • Index on utf8mb4 column larger—may hit index size limits; consider prefix index

Index Differences from PostgreSQL

  • No partial indexes—can't WHERE active = true in index definition
  • No expression indexes until MySQL 8.0.13—must use generated columns before that
  • TEXT/BLOB needs prefix length: INDEX (description(100))—without length, error
  • No INCLUDE for covering—add columns to index itself: INDEX (a, b, c) to cover c
  • Foreign keys auto-indexed only in InnoDB—verify engine before assuming

UPSERT Patterns

  • INSERT ... ON DUPLICATE KEY UPDATE—not standard SQL; needs unique key conflict
  • LAST_INSERT_ID() for auto-increment—no RETURNING clause like PostgreSQL
  • REPLACE INTO deletes then inserts—changes auto-increment ID, triggers DELETE cascade
  • Check affected rows: 1 = inserted, 2 = updated (counter-intuitive)

Locking Traps

  • SELECT ... FOR UPDATE locks rows—but gap locks may lock more than expected
  • InnoDB uses next-key locking—prevents phantom reads but can cause deadlocks
  • Lock wait timeout default 50s—innodb_lock_wait_timeout for adjustment
  • FOR UPDATE SKIP LOCKED exists in MySQL 8+—queue pattern
  • InnoDB default isolation is REPEATABLE READ, not READ COMMITTED like PostgreSQL
  • Deadlocks are expected—code must catch and retry, not just fail

GROUP BY Strictness

  • sql_mode includes ONLY_FULL_GROUP_BY by default in MySQL 5.7+
  • Non-aggregated columns must be in GROUP BY—unlike old MySQL permissive mode
  • ANY_VALUE(column) to silence error when you know values are same
  • Check sql_mode on legacy databases—may behave differently

InnoDB vs MyISAM

  • Always use InnoDB—transactions, row locking, foreign keys, crash recovery
  • MyISAM still default for some system tables—don't use for application data
  • Check engine: SHOW TABLE STATUS—convert with ALTER TABLE ... ENGINE=InnoDB
  • Mixed engines in JOINs work but lose transaction guarantees

Query Quirks

  • LIMIT offset, count different order than PostgreSQL's LIMIT count OFFSET offset
  • != and <> both work; prefer <> for SQL standard
  • No transactional DDL—ALTER TABLE commits immediately, can't rollback
  • Boolean is TINYINT(1)—TRUE/FALSE are just 1/0
  • IFNULL(a, b) instead of COALESCE for two args—though COALESCE works

Connection Management

  • wait_timeout kills idle connections—default 8 hours; pooler may not notice
  • max_connections default 151—often too low; each uses memory
  • Connection pools: don't exceed max_connections across all app instances
  • SHOW PROCESSLIST to see active connections—kill long-running with KILL <id>

Replication Awareness

  • Statement-based replication can break with non-deterministic functions—UUID(), NOW()
  • Row-based replication safer but more bandwidth—default in MySQL 8
  • Read replicas have lag—check Seconds_Behind_Master before relying on replica reads
  • Don't write to replica—usually read-only but verify

Performance

  • EXPLAIN ANALYZE only in MySQL 8.0.18+—older versions just EXPLAIN without actual times
  • Query cache removed in MySQL 8—don't rely on it; cache at application level
  • OPTIMIZE TABLE for fragmented tables—locks table; use pt-online-schema-change for big tables
  • innodb_buffer_pool_size—set to 70-80% of RAM for dedicated DB server

相关文章

更多
PHP连接MySQL数据库的三种实现方法对比

本文总结PHP连接MySQL数据库的三种API接口:MySQL扩展、Mysqli扩展和PDO。MySQL扩展已废弃且不安全;Mysqli支持面向对象及预处理语句,解决SQL注入但仅支持MySQL;PDO作为数据库抽象层,提供统一API支持多数据库切换,同样具备安全性。文章通过代码示例展示各扩展的使用方式,帮助开发者根据场景选择最优方案。

2025.06.15 907
PHP连接MySQL数据库并执行查询的完整流程解析

本文介绍使用PHP进行MySQL数据库查询的具体实现方法。核心步骤包括通过mysql_connect建立服务器连接,利用mysql_select_db选定目标数据库,借助mysql_query执行SQL语句获取结果集,并通过mysql_fetch_row遍历数据行。最后需调用mysql_free_result释放内存并关闭连接,确保资源正确回收。

2025.07.15 847
PHP PDO结果集获取方法详解:fetch、fetchAll与fetchColumn

本文详细介绍PHP中PDO扩展获取数据库结果集的三种核心方法:fetch()、fetchAll()和fetchColumn()。通过具体代码示例,解析各方法的语法参数、返回类型及适用场景,帮助开发者高效处理MySQL数据查询结果。

2026.09.13 840
php连接mysql数据库最简单的实现方法

连接数据库需要明确服务器地址、账号以及业务所用的数据库。原文从这些连接参数讲起,适合把配置错误和查询语句错误分开排查。复现旧代码前先核对运行环境采用的驱动,连通后再发一条简单查询确认选择了正确的库和字符编码。

2026.11.02 776
PHP连接MySQL数据库并以JSON格式输出

本文介绍在PHP中通过面向对象和面向过程两种方式连接MySQL数据库,并将查询结果以JSON格式输出的方法。针对PHP 5.4版本前后处理中文Unicode编码的差异,提供了具体的解决方案,包括使用JSON_UNESCAPED_UNICODE参数或自定义编码函数,确保数据正确显示。

2026.07.02 732
PHP 从 MySQL 读出时间差八小时,怎样区分两种时间列

用一个连接切换会话时区,对照 TIMESTAMP 和 DATETIME 的读取结果,并沿查询和输出定位重复时区转换。

2026.01.05 694
Skill 文件
  • SKILL.md
    • Quick Reference
    • Character Set Traps
    • Index Differences from PostgreSQL
    • UPSERT Patterns
    • Locking Traps
    • GROUP BY Strictness
    • InnoDB vs MyISAM
    • Query Quirks
    • Connection Management
    • Replication Awareness
    • Performance
  • 项目或源文件

相关 Skill

更多
MySQL 设计与使用助手 MySQL 设计与使用助手 帮助Agent为项目进行MySQL Schema设计、索引设计、查询设计,并提供场景化使用指南。当用户需要设计数据库表结构、优化查询性能、规划分库分表方案时触发。
Volcengine RDS Mysql Volcengine RDS Mysql 使用火山引擎 RDS MySQL MCP Server,帮助用户完成 RDS MySQL 相关的实例管理、数据库操作、账号管理和运维任务,可直接调用 uv run ./scripts/call_rds_mysql.py 脚本获取实时结果。
Aliyun Adb Mysql Aliyun Adb Mysql 用于通过 OpenAPI/SDK 管理阿里云 AnalyticDB for MySQL (ADB),包括资源生命周期及配置操作。
MYSQL QUERY MYSQL QUERY 自动管理SSH隧道,查询项目数据库。适用于在已配置的数据库上执行SQL查询,尤其是需要通过SSH访问的数据库。
MySQL to PostgreSQL Migration MySQL to PostgreSQL Migration 在将项目数据库层从MySQL迁移到PostgreSQL时使用此技能。覆盖SQL方言转换(MyBatis XML/原始SQL)和ORM配置等...