相关查询非常慢,通过程序拿到了相关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. poj 3317 Stake Your Claim 极大极小搜索

    思路:为了方便,当c1>c2时将0变为1,1变为0. 空格最多有10个,每个空格有3个状态,如果不状态压缩,会TLE的.所以最多有3^10种情况 代码如下: #include<iostre ...

  2. Integral类型的跨平台使用

    fundamental integral types or extended integral types 我们先通过下图,来了解可以跨平台使用的整数类型: 之所以我们需要以上各种明确指定宽度的int ...

  3. 标准管道(popen)

    NAME popen, pclose - pipe stream to or from a process SYNOPSIS #include <stdio.h> FILE *popen( ...

  4. AngularJS学习笔记1——什么是AngularJS?

    Angular JS是一个由Google维护的开源的Javascript框架,主要作者为: Misko Hevery(angular JS之父, Sr. Computer Scientist at G ...

  5. NIM博弈的必胜取法

    #include<stdio.h> ; int a[Max]; int main() { int i,n,ans; int tmp; while(scanf("%d", ...

  6. <iostream> 和 <iostream.h>的区别 及 Linux下编译iostream.h的方法

    0.序言 其实2者主要的区别就是iostream是C++标准的输入输出流头文件,而iostream.h是非标准的头文件. 标准头文件iostream中的函数属于标准命令空间,而iostream.h中的 ...

  7. MyBatis笔记——初次环境配置

    简单介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBati ...

  8. intellij idea搭建ssh开发框架之绑定数据源

    原文:intellij idea搭建ssh开发框架之绑定数据源 在intellij idea中绑定数据源并生成hibernate实体对象.在IDE中的右边找到Database标签. 点击弹出窗口中的图 ...

  9. 275. H-Index II

    题目: Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optim ...

  10. Linux命令-yum

    定义 yum仓库是为进一步简化RPM管理软件难而设计的,yum能够根据用户的要求分析出所需软件包以及相关依赖关系,自动从服务器下载软件包并安装到系统. 实例