mysql max() 函數的需掃描where條件過濾后的所有行:
在測試環境中重現:
測試版本:Server version: 5.1.58-log MySQL Community Server (GPL)
testtable表中的索引
mysql> show index from testtable;
+-----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| testtable | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | |
| testtable | 1 | key_number | 1 | number | A | 2 | NULL | NULL | YES | BTREE | |
+-----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
對比的sql為:
select sql_no_cache max(id) from testtable where number=98;
select sql_no_cache id from testtable where number=98 order by id desc limit 1;
查看執行計划:
mysql> explain select sql_no_cache max(id) from testtable where number=98;
+----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+
| 1 | SIMPLE | testtable | ref | key_number | key_number | 5 | const | 4 | Using where; Using index |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
mysql> explain select sql_no_cache id from testtable where number=98 order by id desc limit 1;
+----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+
| 1 | SIMPLE | testtable | ref | key_number | key_number | 5 | const | 4 | Using where; Using index |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
執行計划顯示完全一樣。
其中,number為98 對應的記錄有4行:
mysql> select count(*) from testtable where number=98;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
執行前查看innodb_rows_read
#innodb_rows_read 從InnoDB表讀取的行數
mysql> show global status like 'innodb_rows_read';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Innodb_rows_read | 1022 |
+------------------+-------+
1 row in set (0.00 sec)
執行sql1
mysql> select sql_no_cache max(id) from testtable where number=98;
+---------+
| max(id) |
+---------+
| 13 |
+---------+
1 row in set (0.00 sec)
執行后查看innodb_rows_read,發現innodb_rows_read增加了4,即number為98 對應的記錄有4行
mysql> show global status like 'innodb_rows_read';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Innodb_rows_read | 1026 |
+------------------+-------+
1 row in set (0.00 sec)
執行sql2
mysql> select sql_no_cache id from testtable where number=98 order by id desc limit 1;
+----+
| id |
+----+
| 13 |
+----+
1 row in set (0.00 sec)
執行后查看innodb_rows_read,發現innodb_rows_read增加了1
mysql> show global status like 'innodb_rows_read';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Innodb_rows_read | 1027 |
+------------------+-------+
1 row in set (0.00 sec)
測試得出:
select sql_no_cache max(id) from testtable where number=98;
需要讀取 number=98 的所有行,才能得到最大的id
select sql_no_cache id from testtable where number=98 order by id desc limit 1;
由於id是主鍵,number是第二索引,只需掃描1行即可得到最大的id
請慎用max()函數,特別是頻繁執行的sql,若需用到可轉化為測試中的 order by id desc limit 1
因為往往min()或者max()函數往往會造成全表掃描