mysql优化max_mysql优化max()函数

查看表结构:

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值