Comments (11)
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:This way, reusable options can be shared.// 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)
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.
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.
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.
@a8m this is awesome 👏 👏 👏 -- gonna give it a try this week
from ent.
Will it be implemented with JOIN?
from ent.
Yes.
from ent.
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.
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.
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.
@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.
@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)
- Ent generate panics with Go 1.22 HOT 1
- Server Crashes When building project due to high memory usage
- Generate some sub query which are enclosed in brackets and combined with unions and use in form HOT 1
- Upsert using a custom GoType for ID results in error
- [Question] Why defining two TO associations in edge will generate two tables in migrate
- why select always append id HOT 1
- I hope the Count api can be modified to be elegant and flexible like this: err := query.Count(ctx, *int) HOT 1
- Mismatch field type between edge field HOT 2
- How to create table without id field or any primary key in ent HOT 1
- Privacy filter does not apply to IDs inside hooks for *One operations HOT 1
- Is there any plan to returning affected_rows after a query sql? HOT 4
- mysql field name "field" HOT 1
- How to retrieve all columns from all tables HOT 1
- How to modify StructTag for a M2M Join Table using Through
- Unable to correctly bind field value when using .Sensitive() method with Entgo and Golang Echo HOT 2
- I don't want the field to be null, but why is ent always optional when setting fk? HOT 2
- GraphQL integration - Field Collection docs, CollectFields is not documented HOT 1
- Dynamic configurable table name prefix
- GraphQL integration - explicit names for the where input fields?
- panic: go generate ./ent HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ent.