🔥 面试 | Mysql 相关



MySQL 有什么优点?

这个问题本质上是在问 MySQL 如此流行的原因。

MySQL 主要具有下面这些优点:

  • 成熟稳定,功能完善。开源免费。
  • 文档丰富,既有详细的官方文档,又有非常多优质文章可供参考学习。
  • 开箱即用,操作简单,维护成本低。
  • 兼容性好,支持常见的操作系统,支持多种开发语言。
  • 社区活跃,生态完善。
  • 事务支持优秀, InnoDB 存储引擎默认使用 REPEATABLE-READ 并不会有任何性能损失,并且,InnoDB 实现的 REPEATABLE-READ 隔离级别其实是可以解决幻读问题发生的。支持分库分表、读写分离、高可用。

mysql 存储引擎有哪些?区别是什么?

InnoDB、MyISAM、Memory

InnoDB supports transaction, row lock, foreign key.
MyISAM don’t support transaction, but support full-text index.
Memory is stored in RAM, is suitable for temporary data.

mysql 事务的四大特性 four major features?

原子性 Atomicity
一致性 Consistency
隔离性 Isolation
持久型 Durability

索引的类型有哪些?

按约束类型:
普通索引(INDEX)
唯一索引(UNIQUE)
主键索引(PRIMARY KEY)
复合索引(联合索引 - Composite Index)
前缀索引 prefix index

按索引结构:
B+Tree索引
Hash索引
全文索引 FULLTEXT index
Spatial空间索引

索引的优点和缺点?

advantage: enhance query speed. reduce I/O
disadvantage: occupies storage, reduces insert/update performance

优点:加快查询速度、减少 I/O
缺点:占用空间、插入/更新性能下降

哪些情况下索引会失效?

  • 使用 like ‘%xxx’(前模糊匹配)using pre-fuzzy matching
  • 对索引列使用函数或计算 using functions or calculations on indexed columns
  • 隐式类型转换 implicit type conversions
  • 复合索引未遵守最左前缀法则 Composite indexes that do not follow the leftmost prefix rule
  • where 条件使用 or,部分字段没有索引 Using or in a where condition, and some field are not indexed

什么数据可以被认为是热点数据?

特点:

访问频率高:比如首页、热门文章、热销商品。
更新频率低(或可容忍延迟):适合缓存,不会频繁变动。
对性能要求高:如果每次都查数据库,会拖慢整体响应。

📌 举例:

  • 电商场景 → 爆款商品详情、库存信息
  • 新闻门户 → 热点新闻、首页推荐
  • 银行/金融 → 汇率、理财产品利率
  • 游戏 → 排行榜、角色基础信息

如何给热点数据加索引

first, we need to comfirm what is hot data. Find those accessed, updated or deleted frequently through slow query log or other tools.

Analyze which colomn is the hot query, such as message_id is the best choice for message_table.

Use the SQL:

ALTER TABLE table_name ADD INDEX index_name ();

If the query use multiple columns, We can use the composite index. But we need to avoid using too many indexes.

如何使用 EXPLAIN 分析 慢sql

EXPLAIN SELECT * FROM orders WHERE user_id = 10001 AND status = ‘PAID’;

If type = ref, it means the both indexes, that is reasonable.
If type = all, it means the query scan the all table and we need to add the index.

慢查询优化的思路?

  • 查看慢查询日志 check the slow sql logs
  • 优化 SQL(减少子查询、避免 select ) optimize SQL statements such as avoid select
  • 建索引或优化索引 Add index or optimize index
  • 表分区、分库分表 table partitioning, database sharding, table sharding
  • 缓存(如 Redis) Using cache such as Redis

项目里数据库连接池是怎么优化的?

Spring Boot projects use HikariCP as the default connection pool.

According to the Stress testing result, we tune the connection pool size, such as our system sets the maximumPoolSieze 50 and minimumIdel 10. It will avoid waiting because of less connections and excessive stress because of more connections.

行锁 vs 表锁区别?

  • 表锁:锁整张表,开销小,冲突大
  • 行锁:锁某行记录,开销大,粒度小,支持并发

table locks: lock the entire table, with low overhead and high conflict.
row locks: lock a specific row, with high overhead, low granularity and support for concurreny.

死锁产生的原因?如何解决?what causes deadlocks? how resolve?

  • 不同事务锁定资源顺序不一致 different transactions lock resources in inconsistent order
  • 长事务、锁范围过大 long transactions, excessive lock range

解决:超时回滚、合理索引、拆分事务
solution: rollback on timeout, proper indexing, splitting transactions.

MySQL 主从复制原理?

  1. 主库写 binlog
  2. 从库 I/O 线程拉取 binlog,写入 relay log
  3. 从库 SQL 线程执行 relay log,保持数据一致

  4. the master database writes binlog

  5. the slave database I/O thread pulls the binlog and writes it to the relay log.
  6. the slave database SQL thread executes the relay log , thus keep data consistency.

读写分离怎么做?

主库写、从库读
应用层中间件:MyCat、ShardingSphere
数据库代理:ProxySQL

  • master database writes, slave database reads
  • using application-layer middleware: MyCat, ShardingSphere
  • using database proxy: ProxySQL

分库分表如何处理?

垂直分库(按业务拆库)
水平分表(按 user_id 或时间分片)
需要解决的问题:跨库 join、分布式事务、全局 ID

  1. Vertical sharding (sharding by business)
  2. Horizontal sharding (sharding by user_id or time)

But still some issues have to handle: cross-database joins, distributed transactions, global IDs

常见的 MySQL 优化手段?

索引优化
SQL 优化
分表分库
缓存(Redis)
读写分离
参数调优(buffer pool, query cache)

  • Index optimization
  • SQL optimization
  • Table and database sharding
  • Cache (Redis)
  • Read-write splitting
  • Parameter tuning (buffer pool, query cache)

慢 sql 通常是多久?

mysql 提供 long_qurey_time 参数,默认值是 10s
但实际业务中,大部分互联网业务认为大于 1s 就算是 慢 SQL
对于金融、电商类高并发业务,甚至要求 SQL 在 100-500 毫秒内完成。
而报表/数据分析场景:允许 SQL 跑几秒甚至几十秒,因为主要是离线计算。

SQL 语句怎样优化

  1. 尽量不在数据库做运算
  2. 不使用select * ,使用具体需要查的字段列表
  3. 使用 insert 语句时明确插入的字段列表
  4. 避免数据类型的隐式转换,如id写成了字符串
  5. 子查询是简单SQL时尽量避免使用,因为子查询结果会被存储到临时表里,可以把子查询优化为join操作
  6. 避免使用 JOIN 关联太多表,会影响数据库性能,但JOIN加上排序、聚合、去重其实也会产生临时表,但临时表还可使用原表索引,而子查询产生的临时表无索引
  7. 批量处理操作,合并多个相同的操作,可以提高处理效率

JOIN 比子查询为什么快?

JOIN 查询的结构是扁平的,所有表都在同一层级,优化器可以对各个表的链接顺序、索引使用、访问方式进行重排和优化,而子查询是嵌套在where中的,外层依赖内层结构,优化器无法重排执行顺序。

JOIN的执行计划也更透明、更可控。