Skip to content

Commit f53e454

Browse files
committed
[FIX] util/records: improve remove_records
If we are going to remove too many records we better do so in parallel. This util was observed used to remove 23401 ids. upg-2476330 closes #212 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 52a7732 commit f53e454

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/util/records.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""Utility functions for record-level operations."""
32

43
import logging
@@ -34,7 +33,7 @@
3433
)
3534
from .indirect_references import indirect_references
3635
from .inherit import direct_inherit_parents, for_each_inherit
37-
from .misc import parse_version, version_gte
36+
from .misc import chunks, parse_version, version_gte
3837
from .orm import env, flush
3938
from .pg import (
4039
PGRegexp,
@@ -409,7 +408,11 @@ def remove_records(cr, model, ids):
409408
remove_records(cr, inh.model, [rid for (rid,) in cr.fetchall()])
410409

411410
table = table_of_model(cr, model)
412-
cr.execute('DELETE FROM "{}" WHERE id IN %s'.format(table), [ids])
411+
base_query = format_query(cr, "DELETE FROM {} WHERE id IN %s", table)
412+
parallel_execute(
413+
cr,
414+
[cr.mogrify(base_query, [chunk_ids]).decode() for chunk_ids in chunks(ids, 1000, fmt=tuple)],
415+
)
413416
for ir in indirect_references(cr, bound_only=True):
414417
if not ir.company_dependent_comodel:
415418
query = 'DELETE FROM "{}" WHERE {} AND "{}" IN %s'.format(ir.table, ir.model_filter(), ir.res_id)

0 commit comments

Comments
 (0)