加入收藏 | 设为首页 | 会员中心 | 我要投稿 西安站长网 (https://www.029zz.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

这些常被忽视的SQL错误用法,你知道吗

发布时间:2019-09-25 14:24:59 所属栏目:建站 来源:佚名
导读:副标题#e# sql语句的执行顺序: FROM left_table ON join_condition join_type JOIN right_table WHERE where_condition GROUPBY group_by_list HAVING having_condition SELECT DISTINCT select_list ORDERBY order_by_condition LIMIT limit_number 1、LI

重写为 JOIN 之后,子查询的选择模式从 DEPENDENT SUBQUERY 变成 DERIVED,执行速度大大加快,从7秒降低到2毫秒。

  1. UPDATE operation o  
  2.  JOIN (SELECT o.id,  
  3.  o.status  
  4.  FROM operation o  
  5.  WHERE o.group = 123  
  6.  AND o.status NOT IN ( 'done' )  
  7.  ORDER BY o.parent,  
  8.  o.id  
  9.  LIMIT 1) t 
  10.  ON o.id = t.id  
  11. SET status = 'applying'  

执行计划简化为:

  1. +----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+ 
  2. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | 
  3. +----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+ 
  4. | 1 | PRIMARY | | | | | | | | Impossible WHERE noticed after reading const tables | 
  5. | 2 | DERIVED | o | ref | idx_2,idx_5 | idx_5 | 8 | const | 1 | Using where; Using filesort | 
  6. +----+-------------+-------+------+---------------+-------+---------+-------+------+-----------------------------------------------------+ 

4、混合排序

MySQL 不能利用索引进行混合排序。但在某些场景,还是有机会使用特殊方法提升性能的。

  1. SELECT *  
  2. FROM my_order o  
  3.  INNER JOIN my_appraise a ON a.orderid = o.id  
  4. ORDER BY a.is_reply ASC,  
  5.  a.appraise_time DESC  
  6. LIMIT 0, 20  

(编辑:西安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读