相关查询非常慢,通过程序拿到了相关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的更多相关文章

  1. 解决distinct与order by 的冲突

    sql="select distinct id from test order by otherfield desc" 需要找到不同的id,同时又想让记录按fbsj排序.但是这样一 ...

  2. MySql5.7 Distinct与Order By同时使用报错的解决方案

    mysql5.7版本中,如果DISTINCT和order by一起使用将会报3065错误,sql语句无法执行.这是由于5.7版本语法比之前版本语法要求更加严格导致的. 解决方案: 1.vim /etc ...

  3. mysql数据去重并排序使用distinct 和 order by 的问题

    比如直接使用: SELECT distinct mobileFROM table_aWHERE code = 123ORDER BY a_ime desc 在本地mysql数据库没有错,在线上的数据库 ...

  4. sql中distinct和order by问题的解决方案

    需求:根据PID字段对数据去重,根据Sort字段排序,需要显示这个两个字段. 如图,这是原始数据,先排序: 排序后发现两个项是重复的,需要去除一个, 因为Distinct对检查Select里面的每一列 ...

  5. distinct与order by

    不知为啥,当我得查询中出现distinct时,order by 中必须包含要查询的列,否则报错. SELECT DISTINCT a.DetailId, a.OrderId, a.ProductId, ...

  6. distinct 与order by 一起用

    distinct 后面不要直接跟Order by , 如果要用在子查询用用order by .

  7. 解决Sql中DIstinct与Order By共同使用的冲突问题

    1.需求场景: 需要把最新更新文章的前五名作者展示出来. 2.解决问题第一步: select top 5 creator from table order by updateDate desc 结果: ...

  8. 《MySQL必知必会》检索数据,排序检索数据(select ,* ,distinct ,limit , . , order by ,desc)

    <MySQL必知必会>检索数据,排序检索数据 1.检索数据 1.1 select 语句 为了使用SELECT检索表数据,必须至少给出两条信息一想选择什 么,以及从什么地方选择. 1.2 检 ...

  9. 在SELECT DISTINCT 状况下使用 Order BY Newid() 随机数选出记录

    在日常作业中,有时候可能是一些活动要抽出得奖人或选出抽查的一些名单, 就常常会使用到 Order BY Newid() 的方式来做随机数选出, 但有可能的状况需是要搭配到 DISTINCT 来选出,这 ...

随机推荐

  1. Mysql统计总结 - 最近30天,昨天的数据统计

    -- 最近30天的医说发布数量SELECT substr(a.feed_publish_time,6, 5) AS '日期', count(*) AS '医说数' FROM xm_feed a WHE ...

  2. hdu 3092 Least common multiple

    思路: 容易知道,分解成素数的lcm肯定是最大的,因为假设分解成2个合数,设定x为他们的 最大公约数, 那么他们的最小公倍数就要减少x倍了 然后如果是素数之间的最小公倍数,那么就只是他们的乘积,同样的 ...

  3. 自制 JS.format带分页索引

    //第一参数是:Json对象,第二个是   序号  第三个   页数     第四  当前页数String.prototype.format = function (args, sid, pagesi ...

  4. node入门开发遇到的问题

    最近在看node入门这本书,https://cnodejs.org/getstart 里面是跟随作者完成一个小的demo,书中不免会有遗漏的,下面是我在实现里面最后一个例子时遇到的问题,希望能够帮助其 ...

  5. JS初学的一些易错的知识点

    1.  false ,0 , "" ,undefined , null  在Boolean 环境下当成 false: null  在数值环境下当成 0: undefined 在数值 ...

  6. Java-马士兵设计模式学习笔记-观察者模式-OOD 封装event

    把小孩醒来时的具体情况封装成事件类 Test.java class WakenUpEvent{ private long time; private String location; private ...

  7. 打印TMemo的内容到打印机

    Canvas.TextOut真是好用,Printer也实在好用: procedure PrintTStrings(Lst : TStrings) ; var I, Line : Integer; be ...

  8. Centos下修改启动项和网络配置

    1.Centos默认是从图形界面启动,需要较多的资源,为了节省资源可以从命令行启动.修改方法如下: /etc/inittab文件,把 代码: id:5:initdefault:这一行,修改成 代码: ...

  9. Ubuntu 12.04 SSH 安装

    By default Ubuntu Desktop OS comes with ssh clientpackage. It does not include ssh server package wh ...

  10. Maven实战(六)--- dependencies与dependencyManagement的区别

    在上一个项目中遇到一些jar包冲突的问题,之后还有很多人分不清楚dependencies与dependencyManagement的区别,本篇文章将这些区别总结下来. 1.DepencyManagem ...