Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择需要20秒,删除仍在运行@30分钟_Mysql_Sql - Fatal编程技术网

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

Mysql 选择需要20秒,删除仍在运行@30分钟,mysql,sql,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

完成此选择查询大约需要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
为什么?

表模式如下所示:

+----+-------------+------------+------+---------------+------+---------+------+-------+--------------------------------+
| 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|
+----+-------------+------------+------+---------------+------+---------+------+-------+--------------------------------
选择的解释如下:

+----+-------------+------------+------+---------------+------+---------+------+-------+--------------------------------+
| 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|
+----+-------------+------------+------+---------------+------+---------+------+-------+--------------------------------

好的,我只是用谷歌搜索结果解释,这暗示我的查询可能会很慢,因为pid上没有索引。它实际上并没有这么说,但我只是从阅读解释的结果中有一种预感。
所以我在pid上添加了一个索引。在1分钟内删除

您的查询的解释计划是什么样的?有多少行?基于表的索引或函数有多少?由于管理索引的开销,当索引存在时,删除需要更长的时间。或基于函数的索引等功能。。。33000行。和1个索引id。尝试解释添加解释的结果是如何使用索引的极好介绍。