Giter Site home page Giter Site logo

proposal: eager loading support about ent HOT 11 CLOSED

ent avatar ent commented on May 6, 2024 10
proposal: eager loading support

from ent.

Comments (11)

a8m avatar a8m commented on May 6, 2024 4

Update:
After a discussion between @alexsn and I, the suggested changes to the API are:

  • Add a field named Edges to the generated type that will holds all type edges. For example:
    type User struct {
        // user fields.
        
        // preloaded edges.
        Edges UserEdges {
          Pet []*Pet
          Card *Card
          // ...
        }
    }
  • Add a With<Edge>(<Edge>QueryFunc...) to each builder to preload edges. For example:
    // PetQueryFunc is a function type that applied on the PetQuery.
    type PetQueryFunc func(*PetQuery)
    
     client.User.
         Query().
         // "groups" is an edge of a user. 
         WithGroups().
         // "pets" is an edge of a user. 
         WithPets(func (q *ent.PetQuery) {
                 q.Where(pet.HasFriends()).Limit(5)
         })
         // nested eager loading. 
         WithFriends(func (q *ent.UserQuery) {
                 q.WithPets()
         }).
         All(ctx)
    This way, reusable options can be shared.
    func Limit(n int) PetQueryFunc { return func (q *ent.UserQuery) { q.Limit(n) } }
    func Offset(n int) PetQueryFunc { return func (q *ent.UserQuery) { q.Offset(n) } }
    
    // ..
    WithPets(Limit(5), Offset(5))
    // ..

from ent.

alexsn avatar alexsn commented on May 6, 2024 3

By the same token, if I get a user and all of his/her cars in one eager query, will the return value have both the user's columns as well as the cars?

Yes, as an example:

users, err := client.User.
     Query().
     // eagerly load cars along with users
     WithCars().
     All(ctx)
if err != nil {
     // handle error
}
for _, user := range users {
    for _, car := range user.Edges.Cars {
        // do something with car
    }
}

from ent.

ansarizafar avatar ansarizafar commented on May 6, 2024 3

Multiple database queries for a single ent query means multiple database round trips and increased latency. Microsoft Entity framework 3 is now generating only one sql query for each LINQ query. Previously they were using ent approch but they had to change it due to performance and data consistency issues, https://www.youtube.com/watch?v=PXdgyPpfaz4&list=PLReL099Y5nRd04p81Q7p5TtyjCrj9tz1t&index=14&t=899s

from ent.

marwan-at-work avatar marwan-at-work commented on May 6, 2024 2

@a8m this is awesome 👏 👏 👏 -- gonna give it a try this week

from ent.

devig avatar devig commented on May 6, 2024

Will it be implemented with JOIN?

from ent.

alexsn avatar alexsn commented on May 6, 2024

Yes.

from ent.

a8m avatar a8m commented on May 6, 2024

Will it be implemented with JOIN?

Like @alexsn said, JOINs will be used for eager loading. But, in cases of loading multiple and nested relations, additional queries will be executed as well.

For Gremlin, we didn't figure out yet what is the "best" way for doing this.

from ent.

marwan-at-work avatar marwan-at-work commented on May 6, 2024

By the same token, if I get a user and all of his/her cars in one eager query, will the return value have both the user's columns as well as the cars?

Right now, graph traversals only return the last edge but not the edges you've visited along the way.

from ent.

a8m avatar a8m commented on May 6, 2024

Thanks for the reference @ansarizafar, I'll go over it.

I just want to mention that the development is never done, there's always room for performance improvements. We just discussed between us on how we can use JOIN in some specific cases to improve performance.

We have a working solution now (better state than before), we'll improve it gradually, and we'll be able to compare the performance in each iteration.

from ent.

ansarizafar avatar ansarizafar commented on May 6, 2024

@a8m Please also take a look at Fauna Query Language https://docs.fauna.com/fauna/current/api/fql/ and https://github.com/volatiletech/sqlboiler

from ent.

ansarizafar avatar ansarizafar commented on May 6, 2024

@a8m This go project is can translate GraphQL queries into a single fast SQL query. We can use the same method for eager loading in ent.

from ent.

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.