GithubHelp home page GithubHelp logo

Comments (2)

genego-io avatar genego-io commented on August 18, 2024

I led a short investigation into this performance issue. The optimization likely would come from looking more closely at PlanSerializer, which now initializes the PlanDetailSerializer. This is done with the many=True flag, but regardless still calls the Query for each object in the returned list.

@diego-escobedo, I am not seeing 180 db hits. How many plans do you currently have that are being loaded?

For example, if I set up 3 plans (which only have a name), and then call /app/plans/:

image

If I dig a little deeper, I arrive at PlanDetailSerializer.get_versions being responsible for 39 of these queries (13 for each nth Plan).

    def get_versions(self, obj) -> PlanVersionDetailSerializer(many=True):
        return PlanVersionDetailSerializer(
            obj.versions.all().order_by("version"), many=True
        ).data

image

If I remove get_versions, then obv. we have a much more performant request, the time is almost cut in half:

image

Ideally, this could be annotated and combined into a join with 1 or 2 queries.


Additionally, for every request to /app/plans/ there is another call being made to /app/organizations/, which unless I am mistaking, should not be necessary, but it is resulting in another 33 additional queries.

image

Can't solve the issue, but perhaps its enough help to investigate.

If I have some more time I will look into it as well.

from lotus.

diego-escobedo avatar diego-escobedo commented on August 18, 2024

Screenshot 2023-02-13 at 9 05 06 AM

Seeing the number of queries here. I have 7 plan versions in mine so its possible its doing even more inefficiently in the get_versions. First step I'm thinking of taking is using the to_attr in the prefetching of the versions so that it gets used explicitly in get_versions. I suspect calling obj.versions.all().order_by("version") refetches everything.

Screenshot 2023-02-13 at 9 11 20 AM

this cuts maybe like 7 queries per plan. Another thing I'm noticing is I'm not prefetching recurring charges, which were separated from being columns of plan versions into their own models.

Screenshot 2023-02-13 at 9 18 29 AM

I'm curious at this point to see how much of the remaining queries are from stuff further down the stack. I prefetch everything we should need in the get_queryset method of the PlanViewSet, but it seems likely most of this ends up not being used. I suspect part of this comes from display_version, which is just one of the versions but gets pulled directly by the PlanDetailSerializer from the model instead of using the already prefetched version.

Screenshot 2023-02-13 at 12 08 30 PM

Next problem seems to be these queries associated with the recurring charges.

Screenshot 2023-02-13 at 12 10 11 PM

Started using the prefetched versions instead of just whatever the plan decided to pull,

Screenshot 2023-02-13 at 12 45 03 PM

Now need to figure out why that single plan also gets pulled, and where these organization queries are coming from.

Turns out they were coming from the TimezoneFieldMixin. It was 1. using db lookups instead of caching (and deleting cached timezones when changed) and 2. checking even when there was no datetimefield in the representation.

Screenshot 2023-02-13 at 1 13 07 PM

getting close! just weird that we have another query lookup after the entrypoint post prefetching.

Screenshot 2023-02-13 at 1 28 03 PM

boom! demolished those queries. Thank you so much for the help @genego-io

from lotus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.