Skip to content

Commit 712de3f

Browse files
committed
make updating the hierarchy way faster on mysql
1 parent 9babda8 commit 712de3f

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

lib/closure_tree/hierarchy_maintenance.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,13 @@ def delete_hierarchy_references
9595
# See http://dev.mysql.com/doc/refman/5.0/en/subquery-errors.html
9696
# Also: PostgreSQL doesn't support INNER JOIN on DELETE, so we can't use that.
9797
_ct.connection.execute <<-SQL.strip_heredoc
98-
DELETE FROM #{_ct.quoted_hierarchy_table_name}
99-
WHERE descendant_id IN (
100-
SELECT DISTINCT descendant_id
101-
FROM (SELECT descendant_id
102-
FROM #{_ct.quoted_hierarchy_table_name}
103-
WHERE ancestor_id = #{_ct.quote(id)}
104-
OR descendant_id = #{_ct.quote(id)}
105-
) #{ _ct.t_alias_keyword } x )
98+
DELETE #{_ct.quoted_hierarchy_table_name} FROM #{_ct.quoted_hierarchy_table_name}
99+
JOIN (
100+
SELECT descendant_id FROM #{_ct.quoted_hierarchy_table_name}
101+
WHERE ancestor_id = #{_ct.quote(id)}
102+
OR descendant_id = #{_ct.quote(id)}
103+
) #{ _ct.t_alias_keyword } x
104+
ON x.descendant_id = #{_ct.quoted_hierarchy_table_name}.descendant_id
106105
SQL
107106
end
108107
end

0 commit comments

Comments
 (0)