共计 3539 个字符,预计需要花费 9 分钟才能阅读完成。
假设有一个业务场景,需要查询用户登录记录信息,其中表结构如下:
- CREATE TABLE `tb` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `uid` int(11) NOT NULL,
- `ip` varchar(16) NOT NULL,
- `login_time` datetime,
- PRIMARY KEY (`id`),
- KEY (`uid`)
- );
再来点测试数据:
- INSERT INTO tb SELECT null, 1001, ‘192.168.1.1’, ‘2016-01-01 16:30:47’;
- INSERT INTO tb SELECT null, 1003, ‘192.168.1.153’, ‘2016-01-01 19:30:51’;
- INSERT INTO tb SELECT null, 1001, ‘192.168.1.61’, ‘2016-01-01 16:50:41’;
- INSERT INTO tb SELECT null, 1002, ‘192.168.1.31’, ‘2016-01-01 18:30:21’;
- INSERT INTO tb SELECT null, 1002, ‘192.168.1.66’, ‘2016-01-01 19:12:32’;
- INSERT INTO tb SELECT null, 1001, ‘192.168.1.81’, ‘2016-01-01 19:53:09’;
- INSERT INTO tb SELECT null, 1001, ‘192.168.1.231’, ‘2016-01-01 19:55:34’;
表数据情况:
- +––––+––––––+–––––––––––––––+–––––––––––––––––––––+
- | id | uid | ip | login_time |
- +––––+––––––+–––––––––––––––+–––––––––––––––––––––+
- | 1 | 1001 | 192.168.1.1 | 2016-01-01 16:30:47 |
- | 2 | 1003 | 192.168.1.153 | 2016-01-01 19:30:51 |
- | 3 | 1001 | 192.168.1.61 | 2016-01-01 16:50:41 |
- | 4 | 1002 | 192.168.1.31 | 2016-01-01 18:30:21 |
- | 5 | 1002 | 192.168.1.66 | 2016-01-01 19:12:32 |
- | 6 | 1001 | 192.168.1.81 | 2016-01-01 19:53:09 |
- | 7 | 1001 | 192.168.1.231 | 2016-01-01 19:55:34 |
- +––––+––––––+–––––––––––––––+–––––––––––––––––––––+
如果只需要针对用户查出其最后登录的时间,可以简单写出:
- SELECT uid, max(login_time)
- FROM tb
- GROUP BY uid;
- +––––––+–––––––––––––––––––––+
- | uid | max(login_time) |
- +––––––+–––––––––––––––––––––+
- | 1001 | 2016-01-01 19:55:34 |
- | 1002 | 2016-01-01 19:12:32 |
- | 1003 | 2016-01-01 19:30:51 |
- +––––––+–––––––––––––––––––––+
若还需要查询用户最后登录时的其他信息,就不能用这种 sql 写了:
- — 错误写法
- SELECT uid, ip, max(login_time)
- FROM tb
- GROUP BY uid;
- — 错误写法
这样的语句是 非 SQL 标准 的,虽然能够在 MySQL 数据库中执行成功,但返回的却是未知的
(如果sql_mode 开启了only_full_group_by,则不会执行成功。)
可能 ip 字段会取 uid 分组前的第一个 row 的值,显然不是所需信息
写法①
那么写一个子查询吧:
- SELECT a.uid, a.ip, a.login_time
- FROM tb a
- WHERE a.login_time in (
- SELECT max(login_time)
- FROM tb
- GROUP BY uid);
写法②
再或者换一个写法:
- SELECT a.uid, a.ip, a.login_time
- FROM tb a
- WHERE a.login_time = (
- SELECT max(login_time)
- FROM tb
- WHERE a.uid = uid);
顺便测了一下
在 5.6 以前的版本中,写法②这条 sql 在大数据量的情况下,执行计划不理想,目测性能不佳。
在 5.6 及以后的版本中,写法②这条 sql 会快很多,执行计划也有了改变
5.5.50:
- +––––+––––––––––––––––––––+–––––––+––––––+–––––––––––––––+––––––+–––––––––+––––––+––––––+–––––––––––––+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +––––+––––––––––––––––––––+–––––––+––––––+–––––––––––––––+––––––+–––––––––+––––––+––––––+–––––––––––––+
- | 1 | PRIMARY | a | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
- | 2 | DEPENDENT SUBQUERY | tb | ALL | uid | NULL | NULL | NULL | 7 | Using where |
- +––––+––––––––––––––––––––+–––––––+––––––+–––––––––––––––+––––––+–––––––––+––––––+––––––+–––––––––––––+
5.6.30:
- +––––+––––––––––––––––––––+–––––––+––––––+–––––––––––––––+––––––+–––––––––+––––––––––––+––––––+–––––––––––––+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +––––+––––––––––––––––––––+–––––––+––––––+–––––––––––––––+––––––+–––––––––+––––––––––––+––––––+–––––––––––––+
- | 1 | PRIMARY | a | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
- | 2 | DEPENDENT SUBQUERY | tb | ref | uid | uid | 4 | test.a.uid | 1 | NULL |
- +––––+––––––––––––––––––––+–––––––+––––––+–––––––––––––––+––––––+–––––––––+––––––––––––+––––––+–––––––––––––+
写法③
索性直接改成 join 性能会更加好:
- SELECT a.uid, a.ip, a.login_time
- FROM (SELECT uid, max(login_time) login_time
- FROM tb
- GROUP BY uid
- ) b JOIN tb a ON a.uid = b.uid AND a.login_time = b.login_time;
当然,结果都相同:
- +––––––+–––––––––––––––+–––––––––––––––––––––+
- | uid | ip | login_time |
- +––––––+–––––––––––––––+–––––––––––––––––––––+
- | 1003 | 192.168.1.153 | 2016-01-01 19:30:51 |
- | 1002 | 192.168.1.66 | 2016-01-01 19:12:32 |
- | 1001 | 192.168.1.231 | 2016-01-01 19:55:34 |
- +––––––+–––––––––––––––+–––––––––––––––––––––+
当然……如果要分组取最小值直接改对应函数和符号就行了。
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/142386.htm
正文完
星哥玩云-微信公众号