Rebuild tree should depend only on number of ids
For now the choice between rebuild or update MPTT tree is based on the number of SQL queries that will be performed which is 4 per id for the update and number of record in the table for update MPTT.
This leads to the test: len(ids) < max(cls.estimated_count() / 4, 4)
But on large table (which is the cases that matters) all queries are not executed at the same time. For example the UPDATE in rebuild, update only 1 row when the other UPDATEs are all many rows at once (on average half of the rows).
So we can compute the cost of each method with n = len(ids)
, C = estimated_cound()
s
the cost of select and u
the cost of updating a row:
-
_update_mptt
:n * (2 * s + 2 * n/2 * u)
-
_rebuild_tree
:C * s + C * u
If we consider that s = u = 1
so a select has the same cost as updating a row:
-
_update_mptt
:2n (1 + n / 2)
-
_rebuild_tree
:2C
So the test should be: len(ids) * (1 + len(ids) / 2) < cls.estimated_count()