MAX函数和GROUP BY 语句一起使用的一个误区

简介:
使用MAX 函数和 GROUP 的时候会有不可预料的数据被SELECT 出来。
下面举个简单的例子:
想知道每个SCOREID 的 数学成绩最高的分数。


表信息:
/*DDL Information For - test.lkscore*/
--------------------------------------

Table    Create Table                                                                  
-------  -----------------------------------------------------------------------------
lkscore  CREATE TABLE `lkscore` (                                                     
           `scoreid` int(11) DEFAULT NULL,                                            
           `chinese` int(11) DEFAULT '0',                                             
           `math` int(11) DEFAULT '0',                                                
           KEY `fk_class` (`scoreid`),                                                
           CONSTRAINT `fk_class` FOREIGN KEY (`scoreid`) REFERENCES `lkclass` (`id`)  
         ) ENGINE=InnoDB DEFAULT CHARSET=gb2312                                       




select * from lkscore;

query result(12 records)

scoreid chinese math
1 90 80
2 100 99
3 29 98
4 87 79
5 89 99
1 49 98
3 98 56
2 76 88
2 80 90
3 90 70
1 90 90
1 67 90


错误的SELECT
select scoreid,chinese,max(math) max_math from lkscore group by scoreid;

query result(5 records)

scoreid chinese max_math
1 90 98
2 100 99
3 29 98
4 87 79
5 89 99
上面的90明显不对。

方法一:

select scoreid,chinese,math max_math from 
(
select * from lkscore order by math desc
) T 
group by scoreid;

query result(5 records)

scoreid chinese max_math
1 49 98
2 100 99
3 29 98
4 87 79
5 89 99

方法二:


select * from lkscore a where a.math = (select max(math) from lkscore where scoreid = a.scoreid) order by scoreid asc;

query result(5 records)

scoreid chinese max_math
1 49 98
2 100 99
3 29 98
4 87 79
5 89 99

这个也是用MAX函数,而且还用到了相关子查询。
我们来看一下这两个的效率如何:


explain 
select scoreid,chinese,math max_math from (select * from lkscore order by math desc) T group by scoreid;

query result(2 records)

id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL (NULL) (NULL) (NULL) (NULL) 12 Using temporary; Using filesort
2 DERIVED lkscore ALL (NULL) (NULL) (NULL) (NULL) 12 Using filesort

很明显,有两个FULL TABLE SCAN。



explain 
select scoreid,chinese,math max_math from lkscore a where a.math = 
(select max(math) from lkscore where scoreid = a.scoreid) order by scoreid asc;

query result(2 records)

id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY a index (NULL) fk_class 5 (NULL) 12 Using where
2 DEPENDENT SUBQUERY lkscore ref fk_class fk_class 5 a.scoreid 1 Using where


第二个就用了KEY,子查询里只扫描了一跳记录。

很明显。在这种情况下第二个比第一个效率高点。






本文转自 david_yeung 51CTO博客,原文链接:http://blog.51cto.com/yueliangdao0608/81278,如需转载请自行联系原作者

相关文章
|
28天前
|
数据库
count(1)、count(*)、count(column)的含义、区别、执行效率
总之,`count(1)` 和 `count(*)` 通常会更常用,因为它们的执行效率较高,不涉及对具体列值的处理。而 `count(column)` 适用于统计特定列中的非空值数量。在实际使用时,可以根据情况选择适合的方式。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
15 0
|
8月前
|
SQL Oracle 关系型数据库
java实现oracle和mysql的group by分组功能|同时具备max()/min()/sum()/case when 函数等功能
java实现oracle和mysql的group by分组功能|同时具备max()/min()/sum()/case when 函数等功能
提高group by语句的效率
提高group by语句的效率
|
12月前
|
SQL 数据库
数据库sql语句(count(*)和count(字段))
数据库sql语句(count(*)和count(字段))
151 0
九、提高group by语句的效率
九、提高group by语句的效率
289 0
|
SQL 数据挖掘 Python
SQL练习:2(简单)+1(中等),常规题(group by\order by\avg...)
SQL练习:2(简单)+1(中等),常规题(group by\order by\avg...)
163 0
SQL练习:2(简单)+1(中等),常规题(group by\order by\avg...)
|
存储 关系型数据库 MySQL
一文搞清楚 MySQL count(*)、count(1)、count(col) 的区别
一文搞清楚 MySQL count(*)、count(1)、count(col) 的区别
246 0
一文搞清楚 MySQL count(*)、count(1)、count(col) 的区别
|
关系型数据库 MySQL 数据库
数据库面试题【十九、count(字段) &count(主键 id) &count(1)&count(*)的区别】
数据库面试题【十九、count(字段) &count(主键 id) &count(1)&count(*)的区别】
148 0
|
索引 SQL
Select Count 聚合函数三种不同写法区别与效率性能比较
Select Count 聚合函数三种不同写法区别与效率性能比较
423 0
Select Count 聚合函数三种不同写法区别与效率性能比较
|
SQL Go
【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例
【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例目录 0、填充数据1、使用row_number()函数对订单进行编号,按照订单时间倒序。
12428 0