Skip to content

Model.rebuild! slow in postgres for larger collections with many roots #276

@morus

Description

@morus

We have an issue with Model.rebuild! beeing slow on a dataset with some 10000 records and 4000 root elements (no parent) in a postgres database.

We tracked that down to statements updating the sort order column for all root elements over and over again
(looks like once for every root element, so its ~4k updates of 4k records) and postgres is getting slower and
slower on that, probably because of table/index bloat or other issues.

The update is done through the reorder_with_parent_id in the PostgresSQLAdapter module in numeric_order_support.rb .

Suggested fix:

Most of these updates could be avoided if the (2nd) where clause would be amended by a check if the value in the order column changes with the update (postgres executes a real update with a copied record even if nothing changes in the record) as most of the writes are skipped then.

Would you be open to such a change? I could open a PR then.
Of course we are open to other solutions, but several hours for the rebuild! is not a nice behavior.
This went down to some three minutes with the change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions