不要随随便便的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 来选出,这 ...
随机推荐
- Heroku 与 ASP.NET 5
一. Heroku 简单来讲,Heroku是一个支持多种语言.极易部署.多价位可免费的 Pass 平台,通过 Buildpack 搭建语言运行环境, 默认内建的大部分是 Web 开发中较为常见的语言, ...
- 机器学习之神经网络模型-下(Neural Networks: Representation)
3. Model Representation I 1 神经网络是在模仿大脑中的神经元或者神经网络时发明的.因此,要解释如何表示模型假设,我们不妨先来看单个神经元在大脑中是什么样的. 我们的大脑中充满 ...
- UVA 10943 How do you add? DP
Larry is very bad at math — he usually uses a calculator, whichworked well throughout college. Unfor ...
- 初始BOM
1.BOM(Browser Object Model),定义了操作浏览器的借口 2.常用的BOM对象:Window, History,Navigator,Screen, Location等 3.由于浏 ...
- 如何使用JMeter来实现更大批量的并发的解决方案(即如何设置controller和Agent)
http://www.testwo.com/blog/6373 近期在用JMeter进行负载测试的 时候,发现使用单台机器模拟测试超过比如500个进程的并发就有些力不从心或者说不能如实的反应实际情况, ...
- CKEditor上传图片—配置CKFinder
参考:http://blog.csdn.net/gavin710/article/details/8741738
- mq_unlink
NAME mq_unlink - 销毁一个消息队列 (REALTIME) SYNOPSIS #include <mqueue.h> int mq_unlink(const char *na ...
- Android 核心分析之十三Android GWES之Android窗口管理
Android GWES之Android窗口管理1基本构架原理 Android的窗口管理是C/S模式的.Android中的Window是表示Top Level等顶级窗口的概念.DecorView是Wi ...
- javaScript解决Form的嵌套
HTML是不允许FORM嵌套的,用一个简单的JAVASCRIPT就可以解决问题了 <script language=javascript> function process(v){ if( ...
- photoshopCS4换中文
有些朋友的安装的时候可能安装界面是全中文的,安装完软件是英文的. 下载这个中文包, http://download.csdn.net/download/tuberose1605/5171091 解压后 ...