热门关键字:  ubuntu  分区  Fedora  linux系统进程  函数

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

来源: 作者: 时间:2008-10-15 Tag: 点击:
使用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,子查询里只扫描了一跳记录。

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



相关文章:
SQLite3 C/C++ 开发接口简介(API函数) 二
SQLite3 使用教学
Scaling out MySQL - Hardware today and tomorro
SQLite适用的范围
sybase cursor declare
SQL语句大全精要
用户配额
decode函数
Oracle 数据库创建表空间、创建用户指定表空间
MySQL教会我使用GnuPG验证软件包
MySQL中MyISAM引擎与InnoDB引擎性能简单测试
initdb
mysqladmin在线帮助文档
CentOS 4.7 安装Oracle 9.2.0.4的一些问题
文本如何导入oracle(sqlldr 的用法)
ORA-00600: 内部错误代码,自变量: [16201], [],
分布式Oracle的database link
install a MySQL source distribution
Mysql备份脚本(未验证)
oracle 9.2.0.1 update 9.2.0.5
每小时Dump所有mysql数据库到NAS存储设备上
Oracle SCN机制解析
Account LOCKED(TIMED)
MySQL数据库5.0的my.cnf配置选项
oracle存储过程中调用其他用户的表
修改sqlplus提示符
MYSQL建表实例
一些对Mysql DBA有用的脚本
Oracle DBA 强悍挑战OS 64位 Solaris 10-真正休
[Bugzilla]由Mysql迁移到Oracle的方法