设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 数据 手机
当前位置: 首页 > 站长学院 > MySql教程 > 正文

8种你可能正在写错的SQL用法(2)

发布时间:2019-07-22 15:59 所属栏目:115 来源:佚名
导读:执行计划: +----+--------------------+-------+-------+---------------+---------+---------+-------+------+-----------------------------------------------------+ |id|select_type|table|type|possible_key

执行计划:

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

重写为 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'  

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读