> For the complete documentation index, see [llms.txt](https://bohans.gitbook.io/ji-chu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bohans.gitbook.io/ji-chu/mysql/ji-chu-zhi-shi/mysql-cha-xun-de-zhi-xing-guo-cheng/mysql-cha-xun-you-hua-qi/mysql-neng-gou-chu-li-de-you-hua-lei-xing/you-hua-countmin-he-max.md).

# 优化 COUNT()、MIN() 和 MAX()

**索引和列是否可为空通常可以帮助 MySQL 优化这类表达式。**

例如，**要找到某一列的最小值，只需要查询对应 B-tree 索引最左端的记录，MySQL 可以直接获取索引的第一行记录**。在优化器生成执行计划的时候就可以利用这一点，在 B-tree 索引中，优化器会将这个表达式作为一个**常数**对待。类似地，**如果要查找一个最大值，也只需读取 B-tree 索引的最后一条记录**。

<mark style="color:orange;">**如果 MySQL 使用了这种类型的优化，那么在 EXPLAIN 中就可以看到 “Select tables optimized away”，**</mark>它表示优化器已经从执行计划中移除了该表，并以一个常数代替。
