Coder Social home page Coder Social logo

django hits DB twice when it accesses a cached_property field. (A django field with @cached_property decorator.) about strawberry HOT 6 CLOSED

shmoon-kr avatar shmoon-kr commented on June 23, 2024
django hits DB twice when it accesses a cached_property field. (A django field with @cached_property decorator.)

from strawberry.

Comments (6)

patrick91 avatar patrick91 commented on June 23, 2024

@shmoon-kr could build a small reproduction for us? 😊

from strawberry.

shmoon-kr avatar shmoon-kr commented on June 23, 2024

I encountered this issue while working on a project for my company, so I can't use the source code,
I've tried to reproduce the case by creating a simpler version, but it's not working.
I'm guessing that it's not the cached_property itself that's causing the problem, but rather the use of select_related with it. It's going to take me a bit more time to see what's happening more clearly and reproduce it, but I'll comment back as soon as I've simplified it and succeeded in reproducing it.

from strawberry.

patrick91 avatar patrick91 commented on June 23, 2024

@bellini666 might have some ideas then 😊

from strawberry.

bellini666 avatar bellini666 commented on June 23, 2024

Hey @shmoon-kr ,

First of all, are you using strawberry only or strawberrry_django? Based on your description, I'll assume it is strawberry_django and that you have the query optimizer enabled.

For that situation, the optimizer will call .only()/.select_related() on the queryset for the selected fields only. But it has to know that the fields were selected. When you retrieve something inside a cached_property such as the age in your example it would not know.

TBH your example would actually solve it because you are selecting birthday together with age, meaning that your query would guarantee that birthday is selected, but if you remove it and only select age then you would for sure see this problem happening.

For those cases you can give the resolver some optimization hints. Another possibility is to use a model property instead of your cached_property, which acts the same way when you pass cached=True to it, but does accept optimization hints as well.

Let me know if that explanation helps you. If not, a reproduction example might be required for me to understand what might be going on :)

from strawberry.

shmoon-kr avatar shmoon-kr commented on June 23, 2024

Hi. @bellini666

Yes, exactly. I'm using strawberrry_django and I enabled query optimizer.

From your description I can roughly understand what is going on.
I will try to apply your suggestions one by one. If it doesn't work, I'll talk to you again.

Thanks!

from strawberry.

shmoon-kr avatar shmoon-kr commented on June 23, 2024

The issue is completely resolved.

When I put the birthday and age fields in the query at the same time, django stopped making unnecessary accesses to the DB.

It's a workaround, but it's good enough for now, and since you also mentioned giving query hints, I think it's good enough.

Thanks.

from strawberry.

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.