【发布时间】:2015-03-06 16:24:03
【问题描述】:
我需要一个优化以下 mysql 删除查询的建议:
$group_id = '1234';
// DELETE GROUP DISCUSSION POSTS
mysql_query("DELETE FROM table_groupposts
WHERE grouppost_grouptopic_id
IN (SELECT grouptopic_id from table_grouptopics
WHERE grouptopic_group_id='{$group_id}')
");
echo "DEL groupposts: " . mysql_affected_rows() . "<br />";
拥有大型数据库,运行查询最多需要 70 秒,即使没有讨论帖子。
有什么想法可以加快这个过程吗?
【问题讨论】:
-
既然你已经用 InnoDB 标记了这个 - 在相关表上放置一个外键索引,并具有适当的关系和
ON DELETE CASCADE限制;然后只需DELETE FROM table_grouptopics WHERE table_grouptopics.grouptopic_group_id = ?(使用准备好的语句替换?占位符)并允许数据库处理级联删除。 -
@CD001 老实说,我刚刚切换到 innoDB 并且对此仍然很陌生(“放置外键索引”)。
标签: php mysql innodb sql-delete