不要随随便便的distinct和order by
相关查询非常慢,通过程序拿到了相关sql
explain
explain SELECT DISTINCT(o.orders_id), o.oa_order_id, customers_email_address, o.order_type, ot.text AS total_value, o.track_number, o.date_purchased, o.orders_status, o.specialOperate, o.isSpecialParent, o.pay_ip, o.supply_id, o.products_center_id, o.split_code, o.is_import, o.shipDays,o.delivery_country,o.use_coupon ,o.payment_method FROM orders AS o LEFT JOIN orders_total AS ot ON ot.orders_id=o.orders_id AND ot.class='ot_total' WHERE 1 AND o.is_delete = 0 AND o.date_purchased >= '2013-09-30 10:00:00' AND (o.specialOperate = 0 OR o.isSpecialParent=1) ORDER BY date_purchased DESC, orders_id DESC LIMIT 0, 20;
+----+-------------+-------+-------+----------------------------------+----------------------------+---------+----------------------+--------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+----------------------------------+----------------------------+---------+----------------------+--------+----------------------------------------------+
| 1 | SIMPLE | o | range | date_purchased | date_purchased | 9 | NULL | 606632 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | ot | ref | idx_orders_total_orders_id,class | idx_orders_total_orders_id | 4 | banggood.o.orders_id | 19 | |
+----+-------------+-------+-------+----------------------------------+----------------------------+---------+----------------------+--------+----------------------------------------------+
2 rows in set (0.05 sec)
发现索引使用正常,执行状态中发现有Copying to tmp table on disk状态,执行时间超过50s。
使用profiling发现Copying to tmp table on disk占用了大部分性能。
仔细查看该语句并和开发讨论,发现distinct和ORDER BY date_purchased DESC, orders_id DESC中,distinct关键字可以省略,而且ORDER BY date_purchased DESC, orders_id DESC可以去掉后面的orders_id desc(开发对多个字段排序不理解).
去掉后,再次explain
mysql> EXPLAIN
-> SELECT o.orders_id, o.oa_order_id, customers_email_address, o.order_type, ot.text AS total_value, o.track_number, o.date_purchased, o.orders_status, o.specialOperate, o.isSpecialParent, o.pay_ip, o.supply_id, o.products_center_id, o.split_code, o.is_import, o.shipDays,o.delivery_country,o.use_coupon ,o.payment_method FROM orders AS o LEFT JOIN orders_total AS ot ON ot.orders_id=o.orders_id AND ot.class='ot_total' WHERE 1 AND o.is_delete = 0 AND o.date_purchased >= '2013-09-30 10:00:00' AND (o.specialOperate = 0 OR o.isSpecialParent=1)
-> ORDER BY date_purchased DESC LIMIT 0, 20;
+----+-------------+-------+-------+----------------------------------+----------------------------+---------+----------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+----------------------------------+----------------------------+---------+----------------------+--------+-------------+
| 1 | SIMPLE | o | range | date_purchased | date_purchased | 9 | NULL | 606632 | Using where |
| 1 | SIMPLE | ot | ref | idx_orders_total_orders_id,class | idx_orders_total_orders_id | 4 | banggood.o.orders_id | 19 | |
+----+-------------+-------+-------+----------------------------------+----------------------------+---------+----------------------+--------+-------------+
2 rows in set (0.01 sec)
索引使用情况不变,但是下面的profiling,发现结果瞬间出来,执行时间不过0.003s,而且已经没有了Copying to tmp table on disk状态。
总结:1.因为distinct关键字需要对结果集进行去重,如果天然无重复,是不需要加上去重关键字的,上面的例子结果集有将近百万,去重字段又多,在tmp_table_size以及sort_buffer_size中排序已经不够用,所以将结果集复制到磁盘,严重影响速度
2. order by a,b 开发人员很喜欢用类似的语句,尽管对功能没有多大作用
不要随随便便的distinct和order by的更多相关文章
- 解决distinct与order by 的冲突
sql="select distinct id from test order by otherfield desc" 需要找到不同的id,同时又想让记录按fbsj排序.但是这样一 ...
- MySql5.7 Distinct与Order By同时使用报错的解决方案
mysql5.7版本中,如果DISTINCT和order by一起使用将会报3065错误,sql语句无法执行.这是由于5.7版本语法比之前版本语法要求更加严格导致的. 解决方案: 1.vim /etc ...
- mysql数据去重并排序使用distinct 和 order by 的问题
比如直接使用: SELECT distinct mobileFROM table_aWHERE code = 123ORDER BY a_ime desc 在本地mysql数据库没有错,在线上的数据库 ...
- sql中distinct和order by问题的解决方案
需求:根据PID字段对数据去重,根据Sort字段排序,需要显示这个两个字段. 如图,这是原始数据,先排序: 排序后发现两个项是重复的,需要去除一个, 因为Distinct对检查Select里面的每一列 ...
- distinct与order by
不知为啥,当我得查询中出现distinct时,order by 中必须包含要查询的列,否则报错. SELECT DISTINCT a.DetailId, a.OrderId, a.ProductId, ...
- distinct 与order by 一起用
distinct 后面不要直接跟Order by , 如果要用在子查询用用order by .
- 解决Sql中DIstinct与Order By共同使用的冲突问题
1.需求场景: 需要把最新更新文章的前五名作者展示出来. 2.解决问题第一步: select top 5 creator from table order by updateDate desc 结果: ...
- 《MySQL必知必会》检索数据,排序检索数据(select ,* ,distinct ,limit , . , order by ,desc)
<MySQL必知必会>检索数据,排序检索数据 1.检索数据 1.1 select 语句 为了使用SELECT检索表数据,必须至少给出两条信息一想选择什 么,以及从什么地方选择. 1.2 检 ...
- 在SELECT DISTINCT 状况下使用 Order BY Newid() 随机数选出记录
在日常作业中,有时候可能是一些活动要抽出得奖人或选出抽查的一些名单, 就常常会使用到 Order BY Newid() 的方式来做随机数选出, 但有可能的状况需是要搭配到 DISTINCT 来选出,这 ...
随机推荐
- POJ 3384 Feng Shui(半平面交向内推进求最远点对)
题目链接 题意 : 两个圆能够覆盖的最大多边形面积的时候两个圆圆心的坐标是多少,两个圆必须在多边形内. 思路 : 向内推进r,然后求多边形最远的两个点就是能覆盖的最大面积. #include < ...
- Codeforces Round #337 (Div. 2) C. Harmony Analysis 数学
C. Harmony Analysis The semester is already ending, so Danil made an effort and decided to visit a ...
- [iOS]SourceTree+oschina实现代码远程托管
在iOS开发, 涉及到多人协同开发的时候, 这个时候, 我们就得利用版本控制系统(例如GIT), 来合并和管理代码了, 今天我们来讲一下, 利用 SourceTree+oschina进行版本控制 先来 ...
- docker 笔记(基本概念、快速运行、自定义镜像)
1.docker docker是一个打包应用的工具 非常强大,能把操作系统也打在包里,进行无差别部署和运行. 所以docker也被认为是建立在操作系统上的虚拟机. 2.基本概念 镜像(image) ...
- 练习用php做表格
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Vim 新用法
daw , delete a word cw , delete from cursor to the end then insert mode a word 移动: f ; Aa Oo Cc Ii S ...
- pythonhttp
import urllib.requestimport http.cookiejar from urllib.error import URLError,HTTPError import urllib ...
- 关于何时view.setLayoutParams(params);
1,从view得到LayoutParams params LayoutParams params = view.getLayoutParams(); 2,可以从用params.height得到当前v ...
- MTK Android 编译命令
一.Target 编译命令 usage: (makeMtk|mk) [options] project actions [moudles] options: -t,-tcc ...
- Android gingerbread eMMC booting
Android gingerbread eMMC booting This page is currently under construction. The content of this page ...