国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

mysql數據庫max()函數的作用是什么

發(fā)布時(shí)間:2021-09-14 18:07 來(lái)源:億速云 閱讀:0 作者:chen 欄目: Mysql 歡迎投稿:712375056

本篇內容介紹了“數據max()函數的作用是什么”的有關(guān)知識,在實(shí)際案例的操作過(guò)程中,不少人都會(huì )遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學(xué)有所成!

查看表結構:

mysql> show create table coupon_use_test \G
*************************** 1. row ***************************
       Table: coupon_use_test
Create Table: CREATE TABLE `coupon_use_test` (
  `id` int(11) NOT NULL DEFAULT '0',
  `user_id` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `coupon_code` varchar(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
  `status` varchar(2) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT '00',
  `use_time` datetime DEFAULT NULL,
  `remark1` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `remark2` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `remark3` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `create_user_id` varchar(128) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

測試查詢(xún)速度:

mysql> select max(create_time) from coupon_use_test;
+---------------------+
| max(create_time)    |
+---------------------+
| 2016-06-25 16:44:25 |
+---------------------+
1 row in set (2.01 sec)

查看執行計劃:

mysql> explain select max(create_time) from coupon_use_test;
+----+-------------+-----------------+------------+------+---------------+------+---------+------+---------+----------+-------+
| id | select_type | table           | partitions | type | possible_keys | key  | key_len | ref  | rows    | filtered | Extra |
+----+-------------+-----------------+------------+------+---------------+------+---------+------+---------+----------+-------+
|  1 | SIMPLE      | coupon_use_test | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 1706101 |   100.00 | NULL  |
+----+-------------+-----------------+------------+------+---------------+------+---------+------+---------+----------+-------+
1 row in set, 1 warning (0.00 sec)

創(chuàng )建create_time字段索引

mysql> alter table coupon_use_test add index idx_create_time(create_time);
Query OK, 0 rows affected (17.49 sec)
Records: 0  Duplicates: 0  Warnings: 0

再次查詢(xún):

mysql> select max(create_time) from coupon_use_test;
+---------------------+
| max(create_time)    |
+---------------------+
| 2016-06-25 16:44:25 |
+---------------------+
1 row in set (0.00 sec)

查看執行計劃:

mysql> explain select max(create_time) from coupon_use_test;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                        |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
|  1 | SIMPLE      | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | Select tables optimized away |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------------------+
1 row in set, 1 warning (0.00 sec)

索引是有序的,create_time字段加完索引之后取max(create_time)速度變快。

看到其他優(yōu)化方法,通過(guò)轉變SQL查詢(xún)方式實(shí)現

mysql> select create_time from coupon_use_test order by create_time desc limit 1;
+---------------------+
| create_time         |
+---------------------+
| 2016-06-25 16:44:25 |
+---------------------+
1 row in set (0.00 sec)

查看執行計劃:

mysql> explain select create_time from coupon_use_test order by create_time desc limit 1;
+----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+------+----------+-------------+
| id | select_type | table           | partitions | type  | possible_keys | key             | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | coupon_use_test | NULL       | index | NULL          | idx_create_time | 4       | NULL |    1 |   100.00 | Using index |
+----+-------------+-----------------+------------+-------+---------------+-----------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

欧美精品亚洲精品日韩专区| 一本之道AV不卡精品| 97久久超碰国产精品2021| 小说区 图片区 综合区免费 | 99久RE热视频这里只有精品6| 亚洲熟女综合色一区二区三区|