当前位置:  数据库>oracle

不要随随便便的distinct和order by

    来源: 互联网  发布时间:2017-06-02

    本文导语: 有客户反应网站后台订单相关查询非常慢,通过程序拿到了相关sqlexplainexplain 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.pa...

有客户反应网站后台订单相关查询非常慢,通过程序拿到了相关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 开发人员很喜欢用类似的语句,尽管对功能没有多大作用


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java命名空间java.sql类types的类成员方法: distinct定义及介绍
  • SQL SELECT DISTINCT 语句用法
  • 请问这小段代码有什么问题?distinct & SQLException
  • 一个删选数据的例子,使用GROUP、DISTINCT实例解析
  • sql server中distinct筛选重复记录的用法举例
  • oracle sql 去重复记录不用distinct如何实现
  • 使用distinct在mysql中查询多条不重复记录值的解决办法
  • 为何Linq的Distinct实在是不给力
  • sqlserver中distinct的用法(不重复的记录)


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,