Opened 9 months ago

Closed 2 months ago

#34841 closed Cleanup/optimization (fixed)

Reverse migrations model state rendering slow with moderate to large migrations

Reported by: David Sanders Owned by: David Sanders
Component: Migrations Version: 4.2
Severity: Normal Keywords:
Cc: Simon Charette, Markus Holtermann, Shai Berger, Josh Smeaton Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Relevant discussion: https://forum.djangoproject.com/t/migrating-backwards-takes-orders-of-magnitude-longer-than-migrating-forwards/20873/2

Investigations revealed 2 things.

  1. this block of code in db/migrations/excecutor.py was responsible for a large portion of processing time: https://github.com/django/django/blob/b8b2f7451201f3ff60891b6ce55f177400700d7a/django/db/migrations/executor.py#L222-L232

In my small project with only a moderate number of migrations, a simple reverse migrate of the last migration on the main app took ~20s. The block below was responsible for ~16s.

        # Generate the post migration state by starting from the state before
        # the last migration is unapplied and mutating it to include all the
        # remaining applied migrations.
        last_unapplied_migration = plan[-1][0]
        state = states[last_unapplied_migration]
        for index, (migration, _) in enumerate(full_plan):
            if migration == last_unapplied_migration:
                for migration, _ in full_plan[index:]:
                    if migration in applied_migrations:
                        migration.mutate_state(state, preserve=False)
                break
  1. these 2 lines appeared to be responsible for the slowdown, commenting them out made the block above run almost instantly: https://github.com/django/django/blob/b8b2f7451201f3ff60891b6ce55f177400700d7a/django/db/migrations/executor.py#L203-L204
                if "apps" not in state.__dict__:
                    state.apps  # Render all -- performance critical

I have a small patch to move record the unapplied state before these 2 lines and clone it, removing the state.apps attribute.

Change History (6)

comment:1 by David Sanders, 9 months ago

Has patch: set
Owner: changed from nobody to David Sanders
Status: newassigned

comment:2 by Mariusz Felisiak, 9 months ago

Cc: Simon Charette added

comment:3 by Mariusz Felisiak, 8 months ago

Cc: Markus Holtermann Shai Berger Josh Smeaton added
Triage Stage: UnreviewedAccepted

Tentatively accepted for the investigation.

comment:4 by Mariusz Felisiak, 8 months ago

Patch needs improvement: set

comment:5 by Mariusz Felisiak, 2 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 2 months ago

Resolution: fixed
Status: assignedclosed

In b6e2b83:

Fixed #34841 -- Avoided rendering apps on state still requiring mutation.

Note: See TracTickets for help on using tickets.
Back to Top