Skip to content

Commit 2d6f950

Browse files
committed
更新MySQL高频面试题
1 parent df5781a commit 2d6f950

File tree

2 files changed

+154
-189
lines changed

2 files changed

+154
-189
lines changed

数据库/MySQL执行计划.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,32 @@ index包括select索引列,order by主键两种情况。
184184

185185
此次查询中确切使用到的索引。
186186

187+
## ref
188+
189+
`ref` 列显示使用哪个列或常数与`key`一起从表中选择数据行。常见的值有`const``func``NULL`、具体字段名。当 `key` 列为 `NULL`,即不使用索引时。如果值是`func`,则使用的值是某个函数的结果。
190+
191+
以下SQL的执行计划`ref``const`,因为使用了组合索引`(user_id, blog_id)``where user_id = 13`中13为常量。
192+
193+
```mysql
194+
mysql> explain select blog_id from user_like where user_id = 13;
195+
+----+-------------+-----------+------------+------+---------------+------+---------+-------+------+----------+-------------+
196+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
197+
+----+-------------+-----------+------------+------+---------------+------+---------+-------+------+----------+-------------+
198+
| 1 | SIMPLE | user_like | NULL | ref | ul1,ul2 | ul1 | 4 | const | 2 | 100.00 | Using index |
199+
+----+-------------+-----------+------------+------+---------------+------+---------+-------+------+----------+-------------+
200+
```
201+
202+
而下面这个SQL的执行计划`ref`值为`NULL`,因为`key``NULL`,查询没有用到索引。
203+
204+
```mysql
205+
mysql> explain select user_id from user_like where status = 1;
206+
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
207+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
208+
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
209+
| 1 | SIMPLE | user_like | NULL | ALL | NULL | NULL | NULL | NULL | 6 | 16.67 | Using where |
210+
+----+-------------+-----------+------------+------+---------------+------+---------+------+------+----------+-------------+
211+
```
212+
187213
## rows
188214

189215
估算要找到所需的记录,需要读取的行数。评估`SQL` 性能的一个比较重要的数据,`mysql`需要扫描的行数,很直观的显示 `SQL` 性能的好坏,一般情况下 `rows` 值越小越好。
@@ -234,7 +260,7 @@ CREATE TABLE `t_orderdetail` (
234260

235261
查询的列被索引覆盖,但无法通过索引查找找到符合条件的数据,不过可以通过**索引扫描**找到符合条件的数据,也不需要回表查询数据。
236262

237-
包括两种情况:
263+
包括两种情况(组合索引为(user_id, orde))
238264

239265
- where筛选条件不符合最左前缀原则
240266

0 commit comments

Comments
 (0)