mysql数据库max()函数的作用是什么-创新互联
本篇内容介绍了“mysql数据库max()函数的作用是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
10余年的安国网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整安国建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“安国网站设计”,“安国网站推广”以来,每个客户项目都认真落实执行。查看表结构:
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)
测试查询速度:
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)
创建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
再次查询:
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)速度变快。
看到其他优化方法,通过转变SQL查询方式实现
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)
“mysql数据库max()函数的作用是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联-成都网站建设公司网站,小编将为大家输出更多高质量的实用文章!
文章名称:mysql数据库max()函数的作用是什么-创新互联
标题路径:http://myzitong.com/article/dspdih.html