设为首页 收藏本站
查看: 785|回复: 0

[经验分享] mysql的order by与where出现的好玩事

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-10-30 08:36:50 | 显示全部楼层 |阅读模式

CREATE TABLE employees (
emp_no INT(11) NOT NULL,
birth_date DATE NOT NULL,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
gender ENUM(‘M’,’F’) NOT NULL,
hire_date DATE NOT NULL,
PRIMARY KEY (emp_no)
)
COLLATE=’utf8’
ENGINE=InnoDB;

INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10001, ‘1953-09-02’, ‘Georgi’, ‘Facello’, ‘M’, ‘1986-06-26’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10002, ‘1964-06-02’, ‘Bezalel’, ‘Simmel’, ‘F’, ‘1985-11-21’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10003, ‘1959-12-03’, ‘Parto’, ‘Bamford’, ‘M’, ‘1986-08-28’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10004, ‘1954-05-01’, ‘Chirstian’, ‘Koblick’, ‘M’, ‘1986-12-01’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10005, ‘1955-01-21’, ‘Kyoichi’, ‘Maliniak’, ‘M’, ‘1989-09-12’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10006, ‘1953-04-20’, ‘Anneke’, ‘Preusig’, ‘F’, ‘1989-06-02’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10007, ‘1957-05-23’, ‘Tzvetan’, ‘Zielinski’, ‘F’, ‘1989-02-10’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10008, ‘1958-02-19’, ‘Saniya’, ‘Kalloufi’, ‘M’, ‘1994-09-15’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (10011, ‘1972-02-29’, ‘He’, ‘Rick’, ‘M’, ‘1991-02-20’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64887, ‘1961-05-15’, ‘Rafols’, ‘Suomi’, ‘F’, ‘1987-06-05’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64888, ‘1962-05-16’, ‘Kristof’, ‘Marchegay’, ‘F’, ‘1988-11-10’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64889, ‘1953-05-08’, ‘Emdad’, ‘Pauthner’, ‘M’, ‘1985-09-07’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64890, ‘1957-08-21’, ‘Leni’, ‘Kilgore’, ‘M’, ‘1995-08-31’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64891, ‘1954-09-27’, ‘Ioana’, ‘Pepe’, ‘F’, ‘1990-08-19’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64892, ‘1964-12-05’, ‘Mayumi’, ‘Tyugu’, ‘F’, ‘1988-03-19’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64893, ‘1953-11-27’, ‘Yoshimitsu’, ‘Billawala’, ‘F’, ‘1986-06-07’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64894, ‘1955-09-19’, ‘Mori’, ‘Weedman’, ‘F’, ‘1999-01-29’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64895, ‘1962-02-24’, ‘Dietrich’, ‘Foote’, ‘M’, ‘1988-08-07’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64896, ‘1960-12-31’, ‘Ingemar’, ‘Schieder’, ‘M’, ‘1995-10-25’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64897, ‘1956-04-24’, ‘Aksel’, ‘Denna’, ‘M’, ‘1986-08-31’);
INSERT INTO employees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES (64898, ‘1961-04-13’, ‘Sudhanshu’, ‘Hutton’, ‘M’, ‘1996-02-09’);
1:实现row_number.
20151028230156889.jpg
2:取前3条:
20151028230228035.jpg
3:对first_name排序取前三条:
20151028230324751.jpg
在没有排序时得到的事正确的三条数据,但是排序后得到的是21条(全部数据),奇怪。
猜测:
(1)执行了where但是在排序时是将所有的数据都拿出来排序的
(2)没有执行where,直接排序输出了。即跳过了where.
看explain:
20151028230911128.jpg
MySQL官方手册解释:
Using where
A WHERE clause is used to restrict which rows to match against the next table or send to the client. Unless you specifically intend to fetch or examine all rows from the table, you may have something wrong in your query if the Extra value is not Using where and the table join type is ALL or index.

Using filesort
MySQL must do an extra pass to find out how to retrieve the rows in sorted order. The sort is done by going through all rows according to the join type and storing the sort key and pointer to the row for all rows that match the WHERE clause.
然后看到使用了filesort是要使用外部空间进行排序的,这里我是这样想的,sql执行是从右到左的所以应该是order by跳过了where,所以猜测是(2).
因为使用了filesort所以建立first_name索引试下:
20151028231746047.jpg
MySQL官方手册解释:
Using index
The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index.
可以看到建立了first_name索引之后没有filesort了,而且数据也正确了。
所以这里没有了filesort,数据正确所以应该是猜测(1)成立。
在高性能mysql第三版中的6.7.9节中有这样一段话:因为order by引入了文件排序,而where条件是在文件排序操作之前取值的,所以这条查询会返回表中的全部记录,解决这个问题的办法是让变量的赋值和取值发生在执行查询的同一阶段:如图:
20151028233050229.jpg
但是我还是支持做个子查询,这样就不用考虑太多了。哈哈,有点懒了。
不过高性能mysql第三版这个解说和我的想法不一致。先放着,下次深入理解。有知道同学希望讨论下。
感谢群里的大白菜和explain
对于filesort的优化可以在网上找下,这里有比较好的


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-132501-1-1.html 上篇帖子: Linux下MySQL5.6的修改字符集编码为UTF8(解决中文乱码问题) 下篇帖子: MySQL 5.7.9源码编译安装说明 mysql where
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表