选择需要20秒,删除仍然运行@ 30分钟

时间:2014-05-14 19:23:03

标签: mysql sql

此选择查询大约需要20秒才能完成。

 select Count(*)
   from products as bad_rows
   inner join (
   select pid, MAX(last_updated_date) as maxdate
      from products
       group by pid
         having count(*) > 1
      ) as good_rows on good_rows.pid= bad_rows.pid
        and good_rows.maxdate <> bad_rows.last_updated_date
        where bad_rows.available = 0

另一方面,删除仍然在30分钟后运行!

  delete bad_rows
     from products as bad_rows
      inner join (
       select pid, MAX(last_updated_date) as maxdate
          from products
           group by pid
             having count(*) > 1
          ) as good_rows on good_rows.pid= bad_rows.pid
            and good_rows.maxdate <> bad_rows.last_updated_date
            where bad_rows.available = 0

为什么?

表格模式如下:

enter image description here

解释选择如下:

+----+-------------+------------+------+---------------+------+---------+------+-------+--------------------------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows  | Extra                          |
+----+-------------+------------+------+---------------+------+---------+------+-------+--------------------------------+
|  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL | NULL    | NULL |  6253 |                                |
|  1 | PRIMARY     | bad_rows   | ALL  | NULL          | NULL | NULL    | NULL | 34603 | Using where; Using join buffer |
|  2 | DERIVED     | products   | ALL  | NULL          | NULL | NULL    | NULL | 34603 | Using temporary; Using filesort|
+----+-------------+------------+------+---------------+------+---------+------+-------+--------------------------------

1 个答案:

答案 0 :(得分:1)

好吧所以我只是用谷歌搜索结果解释,暗示我的查询可能因为pid上没有索引而变慢。实际上并没有这么说,但我只是在阅读有关Explain结果的预感。 所以我在pid和voila上添加了一个索引。在1分钟内删除!!