Faster Migrations! But at what cost?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Faster Migrations! But at what cost?

Raphael Gaschignard-2
Hi Developers,

  We have a decently-sized "large project", around 240 models across 90 apps, with roughly 500 migrations to work off of. We do periodically squash migrations to keep the migration count under control, but because of all this migrations in our testing server take 3-5 minutes to run to completion.

I am not sure about what the size of a typical Django project is (or rather, a typical "large project") so it's hard for me to quantify how big of an issue this is.

Looking through the migration code and some profiling I found a place where caching was possible (on the ModelState -> Model rendering, based on some of the invariants stated in ModelState code), which would cut our full migration from 230 seconds to 50 seconds (on my machine at least). On the specific caching I did, I was hitting a 90% cache hit rate on our full migration run.

Caching is always a bit scary, though, and there are a lot of places in the apps registry code/model registration code in particular where caches are constantly being wiped. So this stuff scares me quite a bit. In my personal ideal, I would love to be able to check in my caching thing but have it be behind some MIGRATIONS_FASTER_BUT_MAYBE_UNSAFE flag. I am not recommending this for Django because it's not how the project tends to do things, this is just my personal feeling. After all, you're rarely running all  your migrations in production, so this is a testing problem more than anything.

I do think there would be an alternative way to move forward though. Currently the migrations Operation class relies on having the from_state and to_state for DB operations in particular. But I think that we could change up this API based on how these properties are used in Django-provided Operation classes to avoid having to copy the state to provide from_state and to_state. I haven't gone through with this investigation too much yet but I think this would improve things a bit.

So this is a multi-pronged question:

- Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.

- Does the caching of ModelState.render as done in this PR (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
- Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?

I am still trying to wrap my head around some of this problem space, so any insight will be very appreciated

Thanks,
   Raphael

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/fd945497-ef84-4135-b92a-5473ca098809%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Faster Migrations! But at what cost?

Simon Charette
Hello Raphael,

> Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.

I'm not aware of any similar surveys in the recent years but I would say 240 models across 90 apps, with roughly 500 migrations would be considered a really large project in my experience. Did you look into squashing these 500 migrations by any chance? Something we did at $DAYJOB to speed up the test bootstraping process is to prebuild containers with migrations already applied in production so CI running PRs only applies new migrations on top of them.

> Does the caching of ModelState.render as done in this PR (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?

While the layer you added seems to yield significant benefits I would argue that it complicates an already too complex apps rendering caching layer. As you'll probably come to discover while trying to resolve the currently failing tests model.Fields equality is not implemented how you'd expect it to be[0] and thus require costly deconstruction to be used as a cache staleness predicate[1].

> Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?

Given the large adoption of migrations and the fact the Operation API is publicly documented[2] I would say the performance benefits would need to be quite substantial to break backward compatibility. In my opinion, and I think that's something Markus Holtermann who also worked a lot on speeding up migrations would agree on, we should focus our efforts on avoiding model rendering at all cost. We've already made all state mutation (Operation.state_forwards) avoid all accesses to .apps and I think the next step would be to make `database_forwards` and `database_backwards` do the same. This is something Markus worked on a few years ago[3].

Cheers,
Simon

[0] https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499
[1] https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/migrations/autodetector.py#L49-L87
[2] https://docs.djangoproject.com/en/2.2/ref/migration-operations/#writing-your-own
[3] https://github.com/django/django/compare/master...MarkusH:schemaeditor-modelstate

Le dimanche 19 mai 2019 22:13:03 UTC-4, Raphael Gaschignard a écrit :
Hi Developers,

  We have a decently-sized "large project", around 240 models across 90 apps, with roughly 500 migrations to work off of. We do periodically squash migrations to keep the migration count under control, but because of all this migrations in our testing server take 3-5 minutes to run to completion.

I am not sure about what the size of a typical Django project is (or rather, a typical "large project") so it's hard for me to quantify how big of an issue this is.

Looking through the migration code and some profiling I found a place where caching was possible (on the ModelState -> Model rendering, based on some of the invariants stated in ModelState code), which would cut our full migration from 230 seconds to 50 seconds (on my machine at least). On the specific caching I did, I was hitting a 90% cache hit rate on our full migration run.

Caching is always a bit scary, though, and there are a lot of places in the apps registry code/model registration code in particular where caches are constantly being wiped. So this stuff scares me quite a bit. In my personal ideal, I would love to be able to check in my caching thing but have it be behind some MIGRATIONS_FASTER_BUT_MAYBE_UNSAFE flag. I am not recommending this for Django because it's not how the project tends to do things, this is just my personal feeling. After all, you're rarely running all  your migrations in production, so this is a testing problem more than anything.

I do think there would be an alternative way to move forward though. Currently the migrations Operation class relies on having the from_state and to_state for DB operations in particular. But I think that we could change up this API based on how these properties are used in Django-provided Operation classes to avoid having to copy the state to provide from_state and to_state. I haven't gone through with this investigation too much yet but I think this would improve things a bit.

So this is a multi-pronged question:

- Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.

- Does the caching of ModelState.render as done in <a href="https://github.com/django/django/pull/11388" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fpull%2F11388\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEqJJthlkc6-a2G7B_EigyFNAgEdA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fpull%2F11388\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEqJJthlkc6-a2G7B_EigyFNAgEdA&#39;;return true;">this PR (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
- Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?

I am still trying to wrap my head around some of this problem space, so any insight will be very appreciated

Thanks,
   Raphael

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Faster Migrations! But at what cost?

Markus Holtermann
Thanks Raphael for bringing this topic up and Simon for your input already.

I just left a note on your PR: https://github.com/django/django/pull/11388#issuecomment-494076750 . I'll quote it here for ease of readability:

As far as I can see right now, a similar caching happened as a first approach to the Django 1.8 release but cause significant problems, specifically with regards to relational fields. Relational fields (ForeignKey, OneToOneField, ManyToManyField) keep an instance reference to the related model in `.related_model` or the related fields in `.related_fields`. The problem now is, if you reuse a field (and you do because you're only calling `list()` on `self.fields` to copy but not deepcopy the list), you're screwing up references between the models that _will_ cause trouble with `RunSQL` and other operations that use related field or model attributes.

https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499

From my work on migrations, I see essentially 2 approaches that are viable and would lead to significant performance improvements:

## 1. Make the schema editor work with model states

There's a _very old_ branch on my fork that tries to implement this approach: https://github.com/MarkusH/django/commits/schemaeditor-modelstate

I lost interest and eventually also didn't have the time to pursue this approach further. I think that's the better of the two approaches, as it gets rid of the piece of code that actually _causes_ the slow behavior: turning a model state into a model class to be used in the schema editor.

However, making the schema editor backward compatible has been proven difficult and to be a huge pain (just check out the commits :wink: )

## 2. Don't resolve models/fields to instances but rely on string references.

This approach is IMO merely a workaround as it would allow us to cache the fields and models as you're doing in your PR. `model._meta.get_field("author").related_model` would not return `<class 'myapp.models.Author'>` but `"myapp.Author"`. However, as far as I can tell, that's highly backward incompatible. And at this point, I also don't see a way to make this backward compatible.

Cheers,

Markus

On Mon, May 20, 2019, at 5:26 PM, charettes wrote:

> Hello Raphael,
>
> > Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.
>
> I'm not aware of any similar surveys in the recent years but I would
> say *240 models across 90 apps, with roughly 500 migrations* would be
> considered a really large project in my experience. Did you look into
> squashing these 500 migrations by any chance? Something we did at
> $DAYJOB to speed up the test bootstraping process is to prebuild
> containers with migrations already applied in production so CI running
> PRs only applies new migrations on top of them.
>
> > Does the caching of ModelState.render as done in this PR (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
>
> While the layer you added seems to yield significant benefits I would
> argue that it complicates an already too complex apps rendering caching
> layer. As you'll probably come to discover while trying to resolve the
> currently failing tests model.Fields equality is not implemented how
> you'd expect it to be[0] and thus require costly deconstruction to be
> used as a cache staleness predicate[1].
>
> > Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?
>
> Given the large adoption of migrations and the fact the Operation API
> is publicly documented[2] I would say the performance benefits would
> need to be quite substantial to break backward compatibility. In my
> opinion, and I think that's something Markus Holtermann who also worked
> a lot on speeding up migrations would agree on, we should focus our
> efforts on avoiding model rendering at all cost. We've already made all
> state mutation (Operation.state_forwards) avoid all accesses to .apps
> and I think the next step would be to make `database_forwards` and
> `database_backwards` do the same. This is something Markus worked on a
> few years ago[3].
>
> Cheers,
> Simon
>
> [0]
> https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499
> [1]
> https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/migrations/autodetector.py#L49-L87
> [2]
> https://docs.djangoproject.com/en/2.2/ref/migration-operations/#writing-your-own
> [3]
> https://github.com/django/django/compare/master...MarkusH:schemaeditor-modelstate
>
> Le dimanche 19 mai 2019 22:13:03 UTC-4, Raphael Gaschignard a écrit :
> > Hi Developers,
> >
> >  We have a decently-sized "large project", around 240 models across 90 apps, with roughly 500 migrations to work off of. We do periodically squash migrations to keep the migration count under control, but because of all this migrations in our testing server take 3-5 minutes to run to completion.
> >
> > I am not sure about what the size of a typical Django project is (or rather, a typical "large project") so it's hard for me to quantify how big of an issue this is.
> >
> > Looking through the migration code and some profiling I found a place where caching was possible (on the ModelState -> Model rendering, based on some of the invariants stated in ModelState code), which would cut *our* full migration from 230 seconds to 50 seconds (on my machine at least). On the specific caching I did, I was hitting a 90% cache hit rate on our full migration run.
> >
> > Caching is always a bit scary, though, and there are a *lot* of places in the apps registry code/model registration code in particular where caches are constantly being wiped. So this stuff scares me quite a bit. In my personal ideal, I would love to be able to check in my caching thing but have it be behind some MIGRATIONS_FASTER_BUT_MAYBE_UNSAFE flag. I am not recommending this for Django because it's not how the project tends to do things, this is just my personal feeling. After all, you're rarely running all your migrations in production, so this is a testing problem more than anything.
> >
> > I do think there would be an alternative way to move forward though. Currently the migrations Operation class relies on having the from_state and to_state for DB operations in particular. But I think that we could change up this API based on how these properties are used in Django-provided Operation classes to avoid having to copy the state to provide from_state and to_state. I haven't gone through with this investigation too much yet but I think this would improve things a bit.
> >
> > So this is a multi-pronged question:
> >
> > - Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.
> >
> > - Does the caching of ModelState.render as done in this PR <https://github.com/django/django/pull/11388> (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
> > - Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?
> >
> > I am still trying to wrap my head around some of this problem space, so any insight will be very appreciated
> >
> > Thanks,
> >  Raphael
>
>  --
>  You received this message because you are subscribed to the Google
> Groups "Django developers (Contributions to Django itself)" group.
>  To unsubscribe from this group and stop receiving emails from it, send
> an email to [hidden email].
>  To post to this group, send email to
> [hidden email].
>  Visit this group at https://groups.google.com/group/django-developers.
>  To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com <https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com?utm_medium=email&utm_source=footer>.
>  For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/20bf2f53-31e5-4789-975e-8c688d32c424%40www.fastmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Faster Migrations! But at what cost?

Raphael Gaschignard-2
Hi Markus, Simon,

  Both of you, thank you for the detailed replies and status report on
this sort of stuff.

> Did you look into squashing these 500 migrations by any chance?

Yeah so I'll go in and squash things, though (partly because effective
squashing requires moving models around, though we're still at Django
1.11 so it might become easier, partly because of cross-app references)
it's a decent amount of work.

I do like the idea of reusing a "effectively production DB" for things,
not least because it's actually acurate.

OK so the Operation API is effectively documented. I am tempted to try
modifying the `database_forwards` and `database_backwards` to not
require `from_state` (probably through adding a hook that gets called
_before_ `state_forwards` to allow capturing the small details of the
old state). It might actually be possible to make this backwards
compatible by porting Django migrations to use an `apps`-free workflow,
but falling back to the old "re-render the universe" mechanism for
operations that don't apply it.

This is very "that xkcd comic about time spent versus time saved"....

 > will cause trouble with RunSQL and other operations that use related
field or model attributes

So one thing I felt like was an invariant in this code was that field
sharing was expected? From the docstring of ModelState:

     Note that while you are allowed to mutate .fields, you are not allowed
     to mutate the Field instances inside there themselves - you must
instead
     assign new ones, as these are not detached during a clone.

Also maybe you meant to refer to RunPython instead of RunSQL. But I get
your point in general here. Related models can be a problem

One throwaway idea would be to not allow related model/related field
access in these models? There's already a lot of documentation related
to not allowing general model methods (effectively establishing that
"migration models are _not_ normal models"), so there's a bit of
precedent. But beyond the general backwards incompatability, it might
not actually even be obvious how one would implement this. And you kinda
need this info for foreign keys and the like anyways.


Working directly off of `ModelState` is interesting, and I think there
might be a backwards-compatible way forward there, where we still allow
for rendering on certain operations but hold off on it on the basic
ones. Even in our large project, most of our migration operatiosn are
dirt-simple, so if the core django migrations could work off of
`ModelState` then we could get a fast path through there.

Thanks for your input, both of you. I have a couple ideas now that I'm
pretty tempted to try out, mainly around the "fast path and slow path"
strategies that should offer backwards compatibility.

  Raphael

Markus Holtermann wrote on 2019/05/21 2:26:

> Thanks Raphael for bringing this topic up and Simon for your input already.
>
> I just left a note on your PR: https://github.com/django/django/pull/11388#issuecomment-494076750 . I'll quote it here for ease of readability:
>
> As far as I can see right now, a similar caching happened as a first approach to the Django 1.8 release but cause significant problems, specifically with regards to relational fields. Relational fields (ForeignKey, OneToOneField, ManyToManyField) keep an instance reference to the related model in `.related_model` or the related fields in `.related_fields`. The problem now is, if you reuse a field (and you do because you're only calling `list()` on `self.fields` to copy but not deepcopy the list), you're screwing up references between the models that _will_ cause trouble with `RunSQL` and other operations that use related field or model attributes.
>
> https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499
>
>  From my work on migrations, I see essentially 2 approaches that are viable and would lead to significant performance improvements:
>
> ## 1. Make the schema editor work with model states
>
> There's a _very old_ branch on my fork that tries to implement this approach: https://github.com/MarkusH/django/commits/schemaeditor-modelstate
>
> I lost interest and eventually also didn't have the time to pursue this approach further. I think that's the better of the two approaches, as it gets rid of the piece of code that actually _causes_ the slow behavior: turning a model state into a model class to be used in the schema editor.
>
> However, making the schema editor backward compatible has been proven difficult and to be a huge pain (just check out the commits :wink: )
>
> ## 2. Don't resolve models/fields to instances but rely on string references.
>
> This approach is IMO merely a workaround as it would allow us to cache the fields and models as you're doing in your PR. `model._meta.get_field("author").related_model` would not return `<class 'myapp.models.Author'>` but `"myapp.Author"`. However, as far as I can tell, that's highly backward incompatible. And at this point, I also don't see a way to make this backward compatible.
>
> Cheers,
>
> Markus
>
> On Mon, May 20, 2019, at 5:26 PM, charettes wrote:
>> Hello Raphael,
>>
>>> Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.
>> I'm not aware of any similar surveys in the recent years but I would
>> say *240 models across 90 apps, with roughly 500 migrations* would be
>> considered a really large project in my experience. Did you look into
>> squashing these 500 migrations by any chance? Something we did at
>> $DAYJOB to speed up the test bootstraping process is to prebuild
>> containers with migrations already applied in production so CI running
>> PRs only applies new migrations on top of them.
>>
>>> Does the caching of ModelState.render as done in this PR (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
>> While the layer you added seems to yield significant benefits I would
>> argue that it complicates an already too complex apps rendering caching
>> layer. As you'll probably come to discover while trying to resolve the
>> currently failing tests model.Fields equality is not implemented how
>> you'd expect it to be[0] and thus require costly deconstruction to be
>> used as a cache staleness predicate[1].
>>
>>> Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?
>> Given the large adoption of migrations and the fact the Operation API
>> is publicly documented[2] I would say the performance benefits would
>> need to be quite substantial to break backward compatibility. In my
>> opinion, and I think that's something Markus Holtermann who also worked
>> a lot on speeding up migrations would agree on, we should focus our
>> efforts on avoiding model rendering at all cost. We've already made all
>> state mutation (Operation.state_forwards) avoid all accesses to .apps
>> and I think the next step would be to make `database_forwards` and
>> `database_backwards` do the same. This is something Markus worked on a
>> few years ago[3].
>>
>> Cheers,
>> Simon
>>
>> [0]
>> https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499
>> [1]
>> https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/migrations/autodetector.py#L49-L87
>> [2]
>> https://docs.djangoproject.com/en/2.2/ref/migration-operations/#writing-your-own
>> [3]
>> https://github.com/django/django/compare/master...MarkusH:schemaeditor-modelstate
>>
>> Le dimanche 19 mai 2019 22:13:03 UTC-4, Raphael Gaschignard a écrit :
>>> Hi Developers,
>>>
>>>   We have a decently-sized "large project", around 240 models across 90 apps, with roughly 500 migrations to work off of. We do periodically squash migrations to keep the migration count under control, but because of all this migrations in our testing server take 3-5 minutes to run to completion.
>>>
>>> I am not sure about what the size of a typical Django project is (or rather, a typical "large project") so it's hard for me to quantify how big of an issue this is.
>>>
>>> Looking through the migration code and some profiling I found a place where caching was possible (on the ModelState -> Model rendering, based on some of the invariants stated in ModelState code), which would cut *our* full migration from 230 seconds to 50 seconds (on my machine at least). On the specific caching I did, I was hitting a 90% cache hit rate on our full migration run.
>>>
>>> Caching is always a bit scary, though, and there are a *lot* of places in the apps registry code/model registration code in particular where caches are constantly being wiped. So this stuff scares me quite a bit. In my personal ideal, I would love to be able to check in my caching thing but have it be behind some MIGRATIONS_FASTER_BUT_MAYBE_UNSAFE flag. I am not recommending this for Django because it's not how the project tends to do things, this is just my personal feeling. After all, you're rarely running all your migrations in production, so this is a testing problem more than anything.
>>>
>>> I do think there would be an alternative way to move forward though. Currently the migrations Operation class relies on having the from_state and to_state for DB operations in particular. But I think that we could change up this API based on how these properties are used in Django-provided Operation classes to avoid having to copy the state to provide from_state and to_state. I haven't gone through with this investigation too much yet but I think this would improve things a bit.
>>>
>>> So this is a multi-pronged question:
>>>
>>> - Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.
>>>
>>> - Does the caching of ModelState.render as done in this PR <https://github.com/django/django/pull/11388> (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
>>> - Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?
>>>
>>> I am still trying to wrap my head around some of this problem space, so any insight will be very appreciated
>>>
>>> Thanks,
>>>   Raphael
>>   --
>>   You received this message because you are subscribed to the Google
>> Groups "Django developers (Contributions to Django itself)" group.
>>   To unsubscribe from this group and stop receiving emails from it, send
>> an email to [hidden email].
>>   To post to this group, send email to
>> [hidden email].
>>   Visit this group at https://groups.google.com/group/django-developers.
>>   To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com <https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>   For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/405b32b8-1707-71d3-0313-e743a7bb3e58%40makeleaps.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Faster Migrations! But at what cost?

Dan Davis
Migrations are very slow, so I don't run them during test runs, and run tests with --keepdb even in CI/CD.  This is required for my environment anyway, because we use Oracle heavily, a "schema" is the same thing as a "user", and is what I used to think of as a "database" coming from MySQL and PostgreSQL.

To make migrations testable, I wrote a management command "clearschema" which can clear the schema of any database alias, or its test analog.  This "clearschema" command is in a framework app that all the apps at my workplace include, but I think only I and a couple other projects use it.

Anyway, it seems that if --keepdb is so necessary, that anything we can do to speed up migrations during tests and during deployment would be worthwhile, but we have to be very careful to make sure that migrations are imported once, and that any caching scheme is idempotent - e.g. we can throw away the cached data.

On Mon, May 20, 2019 at 11:26 PM Raphael Gaschignard <[hidden email]> wrote:
Hi Markus, Simon,

  Both of you, thank you for the detailed replies and status report on
this sort of stuff.

> Did you look into squashing these 500 migrations by any chance?

Yeah so I'll go in and squash things, though (partly because effective
squashing requires moving models around, though we're still at Django
1.11 so it might become easier, partly because of cross-app references)
it's a decent amount of work.

I do like the idea of reusing a "effectively production DB" for things,
not least because it's actually acurate.

OK so the Operation API is effectively documented. I am tempted to try
modifying the `database_forwards` and `database_backwards` to not
require `from_state` (probably through adding a hook that gets called
_before_ `state_forwards` to allow capturing the small details of the
old state). It might actually be possible to make this backwards
compatible by porting Django migrations to use an `apps`-free workflow,
but falling back to the old "re-render the universe" mechanism for
operations that don't apply it.

This is very "that xkcd comic about time spent versus time saved"....

 > will cause trouble with RunSQL and other operations that use related
field or model attributes

So one thing I felt like was an invariant in this code was that field
sharing was expected? From the docstring of ModelState:

     Note that while you are allowed to mutate .fields, you are not allowed
     to mutate the Field instances inside there themselves - you must
instead
     assign new ones, as these are not detached during a clone.

Also maybe you meant to refer to RunPython instead of RunSQL. But I get
your point in general here. Related models can be a problem

One throwaway idea would be to not allow related model/related field
access in these models? There's already a lot of documentation related
to not allowing general model methods (effectively establishing that
"migration models are _not_ normal models"), so there's a bit of
precedent. But beyond the general backwards incompatability, it might
not actually even be obvious how one would implement this. And you kinda
need this info for foreign keys and the like anyways.


Working directly off of `ModelState` is interesting, and I think there
might be a backwards-compatible way forward there, where we still allow
for rendering on certain operations but hold off on it on the basic
ones. Even in our large project, most of our migration operatiosn are
dirt-simple, so if the core django migrations could work off of
`ModelState` then we could get a fast path through there.

Thanks for your input, both of you. I have a couple ideas now that I'm
pretty tempted to try out, mainly around the "fast path and slow path"
strategies that should offer backwards compatibility.

  Raphael

Markus Holtermann wrote on 2019/05/21 2:26:
> Thanks Raphael for bringing this topic up and Simon for your input already.
>
> I just left a note on your PR: https://github.com/django/django/pull/11388#issuecomment-494076750 . I'll quote it here for ease of readability:
>
> As far as I can see right now, a similar caching happened as a first approach to the Django 1.8 release but cause significant problems, specifically with regards to relational fields. Relational fields (ForeignKey, OneToOneField, ManyToManyField) keep an instance reference to the related model in `.related_model` or the related fields in `.related_fields`. The problem now is, if you reuse a field (and you do because you're only calling `list()` on `self.fields` to copy but not deepcopy the list), you're screwing up references between the models that _will_ cause trouble with `RunSQL` and other operations that use related field or model attributes.
>
> https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499
>
>  From my work on migrations, I see essentially 2 approaches that are viable and would lead to significant performance improvements:
>
> ## 1. Make the schema editor work with model states
>
> There's a _very old_ branch on my fork that tries to implement this approach: https://github.com/MarkusH/django/commits/schemaeditor-modelstate
>
> I lost interest and eventually also didn't have the time to pursue this approach further. I think that's the better of the two approaches, as it gets rid of the piece of code that actually _causes_ the slow behavior: turning a model state into a model class to be used in the schema editor.
>
> However, making the schema editor backward compatible has been proven difficult and to be a huge pain (just check out the commits :wink: )
>
> ## 2. Don't resolve models/fields to instances but rely on string references.
>
> This approach is IMO merely a workaround as it would allow us to cache the fields and models as you're doing in your PR. `model._meta.get_field("author").related_model` would not return `<class 'myapp.models.Author'>` but `"myapp.Author"`. However, as far as I can tell, that's highly backward incompatible. And at this point, I also don't see a way to make this backward compatible.
>
> Cheers,
>
> Markus
>
> On Mon, May 20, 2019, at 5:26 PM, charettes wrote:
>> Hello Raphael,
>>
>>> Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.
>> I'm not aware of any similar surveys in the recent years but I would
>> say *240 models across 90 apps, with roughly 500 migrations* would be
>> considered a really large project in my experience. Did you look into
>> squashing these 500 migrations by any chance? Something we did at
>> $DAYJOB to speed up the test bootstraping process is to prebuild
>> containers with migrations already applied in production so CI running
>> PRs only applies new migrations on top of them.
>>
>>> Does the caching of ModelState.render as done in this PR (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
>> While the layer you added seems to yield significant benefits I would
>> argue that it complicates an already too complex apps rendering caching
>> layer. As you'll probably come to discover while trying to resolve the
>> currently failing tests model.Fields equality is not implemented how
>> you'd expect it to be[0] and thus require costly deconstruction to be
>> used as a cache staleness predicate[1].
>>
>>> Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?
>> Given the large adoption of migrations and the fact the Operation API
>> is publicly documented[2] I would say the performance benefits would
>> need to be quite substantial to break backward compatibility. In my
>> opinion, and I think that's something Markus Holtermann who also worked
>> a lot on speeding up migrations would agree on, we should focus our
>> efforts on avoiding model rendering at all cost. We've already made all
>> state mutation (Operation.state_forwards) avoid all accesses to .apps
>> and I think the next step would be to make `database_forwards` and
>> `database_backwards` do the same. This is something Markus worked on a
>> few years ago[3].
>>
>> Cheers,
>> Simon
>>
>> [0]
>> https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499
>> [1]
>> https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/migrations/autodetector.py#L49-L87
>> [2]
>> https://docs.djangoproject.com/en/2.2/ref/migration-operations/#writing-your-own
>> [3]
>> https://github.com/django/django/compare/master...MarkusH:schemaeditor-modelstate
>>
>> Le dimanche 19 mai 2019 22:13:03 UTC-4, Raphael Gaschignard a écrit :
>>> Hi Developers,
>>>
>>>   We have a decently-sized "large project", around 240 models across 90 apps, with roughly 500 migrations to work off of. We do periodically squash migrations to keep the migration count under control, but because of all this migrations in our testing server take 3-5 minutes to run to completion.
>>>
>>> I am not sure about what the size of a typical Django project is (or rather, a typical "large project") so it's hard for me to quantify how big of an issue this is.
>>>
>>> Looking through the migration code and some profiling I found a place where caching was possible (on the ModelState -> Model rendering, based on some of the invariants stated in ModelState code), which would cut *our* full migration from 230 seconds to 50 seconds (on my machine at least). On the specific caching I did, I was hitting a 90% cache hit rate on our full migration run.
>>>
>>> Caching is always a bit scary, though, and there are a *lot* of places in the apps registry code/model registration code in particular where caches are constantly being wiped. So this stuff scares me quite a bit. In my personal ideal, I would love to be able to check in my caching thing but have it be behind some MIGRATIONS_FASTER_BUT_MAYBE_UNSAFE flag. I am not recommending this for Django because it's not how the project tends to do things, this is just my personal feeling. After all, you're rarely running all your migrations in production, so this is a testing problem more than anything.
>>>
>>> I do think there would be an alternative way to move forward though. Currently the migrations Operation class relies on having the from_state and to_state for DB operations in particular. But I think that we could change up this API based on how these properties are used in Django-provided Operation classes to avoid having to copy the state to provide from_state and to_state. I haven't gone through with this investigation too much yet but I think this would improve things a bit.
>>>
>>> So this is a multi-pronged question:
>>>
>>> - Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.
>>>
>>> - Does the caching of ModelState.render as done in this PR <https://github.com/django/django/pull/11388> (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
>>> - Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?
>>>
>>> I am still trying to wrap my head around some of this problem space, so any insight will be very appreciated
>>>
>>> Thanks,
>>>   Raphael
>>   --
>>   You received this message because you are subscribed to the Google
>> Groups "Django developers (Contributions to Django itself)" group.
>>   To unsubscribe from this group and stop receiving emails from it, send
>> an email to [hidden email].
>>   To post to this group, send email to
>> [hidden email].
>>   Visit this group at https://groups.google.com/group/django-developers.
>>   To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com <https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>   For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/405b32b8-1707-71d3-0313-e743a7bb3e58%40makeleaps.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAFzonYZ5Sv%2B5F2mW38KzDFS8s4JEXbj9XYAdWjhg0tFGpHyvXA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Faster Migrations! But at what cost?

Josh Smeaton
In reply to this post by Raphael Gaschignard-2
Just to add some stats to the conversation, our largest project has ~90 apps (+3rd party), and 240 migration files. This is after we reset our migration history when migrating from 1.8 -> 1.11 (just over a year ago). We would have had well in excess of 800 migration files at that point.

To avoid running migrations in unit tests, we define:

class DisableMigrations(object):
    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return None

MIGRATION_MODULES = DisableMigrations()

Which effectively syncdb's rather than applying migrations. Caveat - you lose all data/runpython/runsql operations in your test environment.

Our dev/staging setup relies on exporting a sanitised version of production, so we only need to run incremental migrations since the last backup.



On Tuesday, 21 May 2019 13:26:39 UTC+10, Raphael Gaschignard wrote:
Hi Markus, Simon,

  Both of you, thank you for the detailed replies and status report on
this sort of stuff.

> Did you look into squashing these 500 migrations by any chance?

Yeah so I'll go in and squash things, though (partly because effective
squashing requires moving models around, though we're still at Django
1.11 so it might become easier, partly because of cross-app references)
it's a decent amount of work.

I do like the idea of reusing a "effectively production DB" for things,
not least because it's actually acurate.

OK so the Operation API is effectively documented. I am tempted to try
modifying the `database_forwards` and `database_backwards` to not
require `from_state` (probably through adding a hook that gets called
_before_ `state_forwards` to allow capturing the small details of the
old state). It might actually be possible to make this backwards
compatible by porting Django migrations to use an `apps`-free workflow,
but falling back to the old "re-render the universe" mechanism for
operations that don't apply it.

This is very "that xkcd comic about time spent versus time saved"....

 > will cause trouble with RunSQL and other operations that use related
field or model attributes

So one thing I felt like was an invariant in this code was that field
sharing was expected? From the docstring of ModelState:

     Note that while you are allowed to mutate .fields, you are not allowed
     to mutate the Field instances inside there themselves - you must
instead
     assign new ones, as these are not detached during a clone.

Also maybe you meant to refer to RunPython instead of RunSQL. But I get
your point in general here. Related models can be a problem

One throwaway idea would be to not allow related model/related field
access in these models? There's already a lot of documentation related
to not allowing general model methods (effectively establishing that
"migration models are _not_ normal models"), so there's a bit of
precedent. But beyond the general backwards incompatability, it might
not actually even be obvious how one would implement this. And you kinda
need this info for foreign keys and the like anyways.


Working directly off of `ModelState` is interesting, and I think there
might be a backwards-compatible way forward there, where we still allow
for rendering on certain operations but hold off on it on the basic
ones. Even in our large project, most of our migration operatiosn are
dirt-simple, so if the core django migrations could work off of
`ModelState` then we could get a fast path through there.

Thanks for your input, both of you. I have a couple ideas now that I'm
pretty tempted to try out, mainly around the "fast path and slow path"
strategies that should offer backwards compatibility.

  Raphael

Markus Holtermann wrote on 2019/05/21 2:26:

> Thanks Raphael for bringing this topic up and Simon for your input already.
>
> I just left a note on your PR: <a href="https://github.com/django/django/pull/11388#issuecomment-494076750" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fpull%2F11388%23issuecomment-494076750\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHcCqHRq-_rrMq8Z-aRrnDwt8X7uA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fpull%2F11388%23issuecomment-494076750\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHcCqHRq-_rrMq8Z-aRrnDwt8X7uA&#39;;return true;">https://github.com/django/django/pull/11388#issuecomment-494076750 . I'll quote it here for ease of readability:
>
> As far as I can see right now, a similar caching happened as a first approach to the Django 1.8 release but cause significant problems, specifically with regards to relational fields. Relational fields (ForeignKey, OneToOneField, ManyToManyField) keep an instance reference to the related model in `.related_model` or the related fields in `.related_fields`. The problem now is, if you reuse a field (and you do because you're only calling `list()` on `self.fields` to copy but not deepcopy the list), you're screwing up references between the models that _will_ cause trouble with `RunSQL` and other operations that use related field or model attributes.
>
> <a href="https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2F1d0bab0bfd77edcf1228d45bf654457a8ff1890d%2Fdjango%2Fdb%2Fmodels%2Ffields%2F__init__.py%23L495-L499\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEnkcz02EwnzkuXlIkdwSpXfewFfg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2F1d0bab0bfd77edcf1228d45bf654457a8ff1890d%2Fdjango%2Fdb%2Fmodels%2Ffields%2F__init__.py%23L495-L499\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEnkcz02EwnzkuXlIkdwSpXfewFfg&#39;;return true;">https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499
>
>  From my work on migrations, I see essentially 2 approaches that are viable and would lead to significant performance improvements:
>
> ## 1. Make the schema editor work with model states
>
> There's a _very old_ branch on my fork that tries to implement this approach: <a href="https://github.com/MarkusH/django/commits/schemaeditor-modelstate" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2FMarkusH%2Fdjango%2Fcommits%2Fschemaeditor-modelstate\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGoQqOaRi-NKlxRD_zKDvMvfeBhpA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2FMarkusH%2Fdjango%2Fcommits%2Fschemaeditor-modelstate\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGoQqOaRi-NKlxRD_zKDvMvfeBhpA&#39;;return true;">https://github.com/MarkusH/django/commits/schemaeditor-modelstate
>
> I lost interest and eventually also didn't have the time to pursue this approach further. I think that's the better of the two approaches, as it gets rid of the piece of code that actually _causes_ the slow behavior: turning a model state into a model class to be used in the schema editor.
>
> However, making the schema editor backward compatible has been proven difficult and to be a huge pain (just check out the commits :wink: )
>
> ## 2. Don't resolve models/fields to instances but rely on string references.
>
> This approach is IMO merely a workaround as it would allow us to cache the fields and models as you're doing in your PR. `model._meta.get_field("author").related_model` would not return `<class 'myapp.models.Author'>` but `"myapp.Author"`. However, as far as I can tell, that's highly backward incompatible. And at this point, I also don't see a way to make this backward compatible.
>
> Cheers,
>
> Markus
>
> On Mon, May 20, 2019, at 5:26 PM, charettes wrote:
>> Hello Raphael,
>>
>>> Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.
>> I'm not aware of any similar surveys in the recent years but I would
>> say *240 models across 90 apps, with roughly 500 migrations* would be
>> considered a really large project in my experience. Did you look into
>> squashing these 500 migrations by any chance? Something we did at
>> $DAYJOB to speed up the test bootstraping process is to prebuild
>> containers with migrations already applied in production so CI running
>> PRs only applies new migrations on top of them.
>>
>>> Does the caching of ModelState.render as done in this PR (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
>> While the layer you added seems to yield significant benefits I would
>> argue that it complicates an already too complex apps rendering caching
>> layer. As you'll probably come to discover while trying to resolve the
>> currently failing tests model.Fields equality is not implemented how
>> you'd expect it to be[0] and thus require costly deconstruction to be
>> used as a cache staleness predicate[1].
>>
>>> Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?
>> Given the large adoption of migrations and the fact the Operation API
>> is publicly documented[2] I would say the performance benefits would
>> need to be quite substantial to break backward compatibility. In my
>> opinion, and I think that's something Markus Holtermann who also worked
>> a lot on speeding up migrations would agree on, we should focus our
>> efforts on avoiding model rendering at all cost. We've already made all
>> state mutation (Operation.state_forwards) avoid all accesses to .apps
>> and I think the next step would be to make `database_forwards` and
>> `database_backwards` do the same. This is something Markus worked on a
>> few years ago[3].
>>
>> Cheers,
>> Simon
>>
>> [0]
>> <a href="https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2F1d0bab0bfd77edcf1228d45bf654457a8ff1890d%2Fdjango%2Fdb%2Fmodels%2Ffields%2F__init__.py%23L495-L499\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEnkcz02EwnzkuXlIkdwSpXfewFfg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2F1d0bab0bfd77edcf1228d45bf654457a8ff1890d%2Fdjango%2Fdb%2Fmodels%2Ffields%2F__init__.py%23L495-L499\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEnkcz02EwnzkuXlIkdwSpXfewFfg&#39;;return true;">https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/models/fields/__init__.py#L495-L499
>> [1]
>> <a href="https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/migrations/autodetector.py#L49-L87" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2F1d0bab0bfd77edcf1228d45bf654457a8ff1890d%2Fdjango%2Fdb%2Fmigrations%2Fautodetector.py%23L49-L87\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGuE39K-S_DBRa82O42f1kI7_fWrw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2F1d0bab0bfd77edcf1228d45bf654457a8ff1890d%2Fdjango%2Fdb%2Fmigrations%2Fautodetector.py%23L49-L87\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGuE39K-S_DBRa82O42f1kI7_fWrw&#39;;return true;">https://github.com/django/django/blob/1d0bab0bfd77edcf1228d45bf654457a8ff1890d/django/db/migrations/autodetector.py#L49-L87
>> [2]
>> <a href="https://docs.djangoproject.com/en/2.2/ref/migration-operations/#writing-your-own" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.2%2Fref%2Fmigration-operations%2F%23writing-your-own\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGkKoHmo--6VsrvdArBXwv6awvhgA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F2.2%2Fref%2Fmigration-operations%2F%23writing-your-own\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGkKoHmo--6VsrvdArBXwv6awvhgA&#39;;return true;">https://docs.djangoproject.com/en/2.2/ref/migration-operations/#writing-your-own
>> [3]
>> <a href="https://github.com/django/django/compare/master...MarkusH:schemaeditor-modelstate" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fcompare%2Fmaster...MarkusH%3Aschemaeditor-modelstate\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHc3LWdbj83vAe9oNxzIm5ahdeXvw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fcompare%2Fmaster...MarkusH%3Aschemaeditor-modelstate\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHc3LWdbj83vAe9oNxzIm5ahdeXvw&#39;;return true;">https://github.com/django/django/compare/master...MarkusH:schemaeditor-modelstate
>>
>> Le dimanche 19 mai 2019 22:13:03 UTC-4, Raphael Gaschignard a écrit :
>>> Hi Developers,
>>>
>>>   We have a decently-sized "large project", around 240 models across 90 apps, with roughly 500 migrations to work off of. We do periodically squash migrations to keep the migration count under control, but because of all this migrations in our testing server take 3-5 minutes to run to completion.
>>>
>>> I am not sure about what the size of a typical Django project is (or rather, a typical "large project") so it's hard for me to quantify how big of an issue this is.
>>>
>>> Looking through the migration code and some profiling I found a place where caching was possible (on the ModelState -> Model rendering, based on some of the invariants stated in ModelState code), which would cut *our* full migration from 230 seconds to 50 seconds (on my machine at least). On the specific caching I did, I was hitting a 90% cache hit rate on our full migration run.
>>>
>>> Caching is always a bit scary, though, and there are a *lot* of places in the apps registry code/model registration code in particular where caches are constantly being wiped. So this stuff scares me quite a bit. In my personal ideal, I would love to be able to check in my caching thing but have it be behind some MIGRATIONS_FASTER_BUT_MAYBE_UNSAFE flag. I am not recommending this for Django because it's not how the project tends to do things, this is just my personal feeling. After all, you're rarely running all your migrations in production, so this is a testing problem more than anything.
>>>
>>> I do think there would be an alternative way to move forward though. Currently the migrations Operation class relies on having the from_state and to_state for DB operations in particular. But I think that we could change up this API based on how these properties are used in Django-provided Operation classes to avoid having to copy the state to provide from_state and to_state. I haven't gone through with this investigation too much yet but I think this would improve things a bit.
>>>
>>> So this is a multi-pronged question:
>>>
>>> - Have there ever been any surveys about how the size of Django projects? I don't know the value of investigating this further except for our own usage.
>>>
>>> - Does the caching of ModelState.render as done in this PR <<a href="https://github.com/django/django/pull/11388" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fpull%2F11388\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEqJJthlkc6-a2G7B_EigyFNAgEdA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fpull%2F11388\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNEqJJthlkc6-a2G7B_EigyFNAgEdA&#39;;return true;">https://github.com/django/django/pull/11388> (still need to work through a couple failing tests) sound reasonable? Or is this veering too far in the performance/safety guarantee tradeoff?
>>> - Is the migration operation infrastructure considered a public API? As in, would changing the Operation model API (potentially breaking subclasses) be considered a major undertaking? Or would it be an acceptable cost to pay for some performance improvements?
>>>
>>> I am still trying to wrap my head around some of this problem space, so any insight will be very appreciated
>>>
>>> Thanks,
>>>   Raphael
>>   --
>>   You received this message because you are subscribed to the Google
>> Groups "Django developers (Contributions to Django itself)" group.
>>   To unsubscribe from this group and stop receiving emails from it, send
>> an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="0bMs-svKAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-d...@googlegroups.com.
>>   To post to this group, send email to
>> <a href="javascript:" target="_blank" gdf-obfuscated-mailto="0bMs-svKAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-d...@googlegroups.com.
>>   Visit this group at <a href="https://groups.google.com/group/django-developers" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;">https://groups.google.com/group/django-developers.
>>   To view this discussion on the web visit
>> <a href="https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com <<a href="https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-developers/978c443a-d020-4084-9e47-77fd2122389d%40googlegroups.com?utm_medium=email&utm_source=footer>.
>>   For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/d0f1469e-5a33-4c04-bc30-1de85cd5edb7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.