优化阻塞数据库的 MySQL DELETE 查询
Optimizing a MySQL DELETE Query which is blocking the database
我需要关于优化以下 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 秒,即使没有讨论帖子。
有什么办法可以加快这个过程吗?
为什么删除操作不使用连接而不是子查询
delete tg from table_groupposts tg
join table_grouptopics gt on gt.grouptopic_id = tg.grouppost_grouptopic_id
where gt.grouptopic_group_id='$group_id'
确保连接键已编入索引,如果没有则按
alter table table_groupposts add index grouppost_grouptopic_id_idx(grouppost_grouptopic_id);
alter table table_grouptopics add index grouptopic_id_idx(grouptopic_id);
最后
alter table table_grouptopics add index grouptopic_group_id_idx(grouptopic_group_id)
我需要关于优化以下 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 秒,即使没有讨论帖子。
有什么办法可以加快这个过程吗?
为什么删除操作不使用连接而不是子查询
delete tg from table_groupposts tg
join table_grouptopics gt on gt.grouptopic_id = tg.grouppost_grouptopic_id
where gt.grouptopic_group_id='$group_id'
确保连接键已编入索引,如果没有则按
alter table table_groupposts add index grouppost_grouptopic_id_idx(grouppost_grouptopic_id);
alter table table_grouptopics add index grouptopic_id_idx(grouptopic_id);
最后
alter table table_grouptopics add index grouptopic_group_id_idx(grouptopic_group_id)