關於取表中id最大值+1的select語句,哪種效率更高?


需求:取stock表中id最大值+1,作為下一個id值。

特殊情況:考慮到表中會沒有值,max(id)會返回空,因此需要用case when進行判斷。

實現一:select (case max(id) is null when true then 0 else max(id)+1 end) from stock

實現二:select (case (select count(*) from stock) when 0 then 0 else max(id)+1 end) from stock

效率分析:

實現一相對於實現二不取數量,在索引的幫助下也能快速取值,因此效率應該比實現二高。

分析之驗證:

(MySQL數據庫)

stock表中沒有數據時:

mysql> explain select (case max(id) is null when true then 0 else max(id)+1 end) from stock;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                   |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | No matching min/max row |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------------------+
1 row in set (0.00 sec)

mysql> explain select (case (select count(*) from stock) when 0 then 0 else max(id)+1 end) from stock;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra                   |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------------------+
|  1 | PRIMARY     | NULL  | NULL  | NULL          | NULL    | NULL    | NULL | NULL | No matching min/max row |
|  2 | SUBQUERY    | stock | index | NULL          | PRIMARY | 4       | NULL | 3749 | Using index             |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------------------+
2 rows in set (0.05 sec)

stock表中有數據時:

mysql> select count(*) from stock;
+----------+
| count(*) |
+----------+
|     3768 |
+----------+
1 row in set (0.00 sec)

mysql> explain select (case max(id) is null when true then 0 else max(id)+1 end) from stock;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                        |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
1 row in set (0.00 sec)

mysql> desc select (case (select count(*) from stock) when 0 then 0 else max(id)+1 end) from stock;
+----+-------------+-------+-------+---------------+---------+---------+------+------+------------------------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra                        |
+----+-------------+-------+-------+---------------+---------+---------+------+------+------------------------------+
|  1 | PRIMARY     | NULL  | NULL  | NULL          | NULL    | NULL    | NULL | NULL | Select tables optimized away |
|  2 | SUBQUERY    | stock | index | NULL          | PRIMARY | 4       | NULL | 3696 | Using index                  |
+----+-------------+-------+-------+---------------+---------+---------+------+------+------------------------------+
2 rows in set (0.00 sec)

 

分析之驗證:

oracle數據庫

MySQL版的SQL不能直接在oracle里使用,因此需要改寫成:

方案一:select nvl(max(id)+1,0) from stock;

方案二:select (case count(*) when 0 then 0 else max(id)+1 end) from stock

然后我模擬做了3744條記錄,跑解釋計划確實方案二慢.

方案一的執行計划:

SQL> select nvl(max(id)+1,0) from stock;
已用時間:  00: 00: 00.00

執行計划
----------------------------------------------------------
Plan hash value: 1547204082

--------------------------------------------------------------------------------
-----------

| Id  | Operation                  | Name         | Rows  | Bytes | Cost (%CPU)|
 Time     |

--------------------------------------------------------------------------------
-----------

|   0 | SELECT STATEMENT           |              |     1 |    13 |     2   (0)|
 00:00:01 |

|   1 |  SORT AGGREGATE            |              |     1 |    13 |            |
          |

|   2 |   INDEX FULL SCAN (MIN/MAX)| SYS_C0011050 |     1 |    13 |     2   (0)|
 00:00:01 |

--------------------------------------------------------------------------------
-----------


Note
-----
   - dynamic sampling used for this statement (level=2)

方案二的執行計划:

SQL> select (case count(*) when 0 then 0 else max(id)+1 end) from stock;
已用時間:  00: 00: 00.00

執行計划
----------------------------------------------------------
Plan hash value: 916654

--------------------------------------------------------------------------------
------

| Id  | Operation             | Name         | Rows  | Bytes | Cost (%CPU)| Time
     |

--------------------------------------------------------------------------------
------

|   0 | SELECT STATEMENT      |              |     1 |    13 |     5   (0)| 00:0
0:01 |

|   1 |  SORT AGGREGATE       |              |     1 |    13 |            |
     |

|   2 |   INDEX FAST FULL SCAN| SYS_C0011050 |  3744 | 48672 |     5   (0)| 00:0
0:01 |

--------------------------------------------------------------------------------
------


Note
-----
   - dynamic sampling used for this statement (level=2)

--2020年5月2日--


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM