|
根据a,b group by,取出 c的最小值,最大值,同时取出c的最小值和最大值的时间
类似于
这样的数据
a b 最小值 最小值的时间 最大值 最大值的时间
1 | 1 | 1 | 2009-06-22 15:04:55 3 | 2009-06-22 15:05:16
1 | 2 | 1 | 2009-06-22 15:05:47 6 | 2009-06-22 15:06:06
为了实现上面的功能
我用如下两种方式,就是 把最大值,最小值的 的方式进行连接 ,效率一定是低的
方法如下:
mysql> select t1.a,
-> t1.b,
-> t1.c '最小值',
-> t1.d '取得最小值的时间',
-> t2.c '最大值',
-> t2.d '取得最大值的时间'
-> from
-> (select a,b,c,d from (select a,b,c,d from test order by a,b,c) t group by a,b) t1,
-> (select a,b,c,d from (select a,b,c,d from test order by a,b,c desc) t group by a,b) t2
-> where t1.a = t2.a
-> and t1.b = t2.b;
+------+------+--------+---------------------+--------+---------------------+
| a | b | 最小值 | 取得最小值的时间 | 最大值 | 取得最大值的时间 |
+------+------+--------+---------------------+--------+---------------------+
| 1 | 1 | 1 | 2009-06-22 15:04:55 | 3 | 2009-06-22 15:05:16 |
| 1 | 2 | 1 | 2009-06-22 15:05:47 | 6 | 2009-06-22 15:06:06 |
+------+------+--------+---------------------+--------+---------------------+
2 rows in set (0.00 sec)
mysql> select t1.a,
-> t1.b,
-> t1.c '最小值',
-> t1.d '取得最小值的时间',
-> t2.c '最大值',
-> t2.d '取得最大值的时间'
-> from (select a, b, c, d from test where (a, b, c) in (select a, b, min(c) from test group by a, b)) t1,
-> (select a, b, c, d from test where (a, b, c) in (select a, b, max(c) from test group by a, b)) t2
-> where t1.a = t2.a
-> and t1.b = t2.b;
+------+------+--------+---------------------+--------+---------------------+
| a | b | 最小值 | 取得最小值的时间 | 最大值 | 取得最大值的时间 |
+------+------+--------+---------------------+--------+---------------------+
| 1 | 1 | 1 | 2009-06-22 15:04:55 | 3 | 2009-06-22 15:05:16 |
| 1 | 2 | 1 | 2009-06-22 15:05:47 | 6 | 2009-06-22 15:06:06 |
+------+------+--------+---------------------+--------+---------------------+
2 rows in set (0.00 sec)
看了一下执行计划
mysql> explain
-> select t1.a,
-> t1.b,
-> t1.c '最小值',
-> t1.d '取得最小值的时间',
-> t2.c '最大值',
-> t2.d '取得最大值的时间'
-> from (select a, b, c, d from test where (a, b, c) in (select a, b, min(c) from test group by a, b)) t1,
-> (select a, b, c, d from test where (a, b, c) in (select a, b, max(c) from test group by a, b)) t2
-> where t1.a = t2.a
-> and t1.b = t2.b;
+----+--------------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2 | |
| 1 | PRIMARY | <derived4> | ALL | NULL | NULL | NULL | NULL | 2 | Using where; Using join buffer |
| 4 | DERIVED | test | ALL | NULL | NULL | NULL | NULL | 6 | Using where |
| 5 | DEPENDENT SUBQUERY | test | ALL | NULL | NULL | NULL | NULL | 6 | Using temporary; Using filesort |
| 2 | DERIVED | test | ALL | NULL | NULL | NULL | NULL | 6 | Using where |
| 3 | DEPENDENT SUBQUERY | test | ALL | NULL | NULL | NULL | NULL | 6 | Using temporary; Using filesort |
+----+--------------------+------------+------+---------------+------+---------+------+------+---------------------------------+
6 rows in set (0.01 sec)
mysql> explain
-> select t1.a,
-> t1.b,
-> t1.c '最小值',
-> t1.d '取得最小值的时间',
-> t2.c '最大值',
-> t2.d '取得最大值的时间'
-> from
-> (select a,b,c,d from (select a,b,c,d from test order by a,b,c) t group by a,b) t1,
-> (select a,b,c,d from (select a,b,c,d from test order by a,b,c desc) t group by a,b) t2
-> where t1.a = t2.a
-> and t1.b = t2.b;
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2 | |
| 1 | PRIMARY | <derived4> | ALL | NULL | NULL | NULL | NULL | 2 | Using where; Using join buffer |
| 4 | DERIVED | <derived5> | ALL | NULL | NULL | NULL | NULL | 6 | Using temporary; Using filesort |
| 5 | DERIVED | test | ALL | NULL | NULL | NULL | NULL | 6 | Using filesort |
| 2 | DERIVED | <derived3> | ALL | NULL | NULL | NULL | NULL | 6 | Using temporary; Using filesort |
| 3 | DERIVED | test | ALL | NULL | NULL | NULL | NULL | 6 | Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
6 rows in set (0.00 sec)
我觉得相对于oracle 而言 这两种方法效率太差了
当时偶水平有限 ,
那位兄弟帮帮忙 ,有没有 一次性就可以取出这样的结果出来
不要扫描表那么多次
谢谢了 |
|