Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement separate fetch relations in findOptions #8323

Closed
wants to merge 1 commit into from

Conversation

GaryXiongxiong
Copy link

This new feature allow fetch relations in separated sql query, that will fix memory running out when .find() called with multiple join relations. This issue is described in #5829 & #3857

Fixes: #5829

Description of change

This new feature allow fetch relations in separated sql query, that will fix memory running out when .find() called with multiple join relations. This issue is described in #5829 & #3857

You can fetch relations by using:

    const posts = await connection.manager.find(Post, {
        relations: ["category"],
        where: {category:{name: "Category #2"}},
        separateFetchRelations: ["author","author.post"]
    });

Then the relation author and post of author will be queried in separated sql:

SELECT * FROM post 
LEFT JOIN category on category.post_id = post.id
WHERE category.name = 'Category #2';

SELECT * FROM author
WHERE author.post_id = post_id; --post's id just fetched

SELECT * FROM post
WHERE post.author_id = author_id; --author's id just fetched

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run lint passes with this change
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

This new feature allow fetch relations in separated sql query, that  will fix memory running out when .find() called with multiple join relations. This issue is described in typeorm#5829 & typeorm#3857

Closes: typeorm#5829
@pleerock
Copy link
Member

pleerock commented Nov 2, 2021

Functionality of separate queries for relations were implemented in @next branch years ago. Unfortunately we were not able to release them in time, and now @next is far away behind master in many aspects.

I think current implementation of this feature in @next is more advanced and we should port changes from there to master.

But the way it was implemented in @next is simply replacing of current join approach completely to separate queries. Some people were disagree, because in some cases joins are more effective. But mostly I think it's a better approach.

So, what if we port what we have in @next, keep what we have in master and introduce a new option called relationLoadStrategy: "join" | "query" ?

@GaryXiongxiong do you want to spend more of your time / will in order to check how it's currently implemented in @next and think how to properly port it to master?

@YixiongJiang
Copy link

Functionality of separate queries for relations were implemented in @next branch years ago. Unfortunately we were not able to release them in time, and now @next is far away behind master in many aspects.

I think current implementation of this feature in @next is more advanced and we should port changes from there to master.

But the way it was implemented in @next is simply replacing of current join approach completely to separate queries. Some people were disagree, because it some cases joins are more effective. But mostly I think it's a better approach.

So, what if we port what we have in @next, keep what we have in master and introduce a new option called relationLoadStrategy: "join" | "query" ?

@GaryXiongxiong do you want to spend more of your time / will in order to check how it's currently implemented in @next and think how to properly port it to master?

Yeah, will take some time for it

@GeiOoo
Copy link

GeiOoo commented Jan 24, 2022

I dont wanna rush... but is there any progress on that topic?
We've got some huge performance problems with fairly complex entity relations.
A fully resolved query for a single record can easily result in a 75000 row result set taking about 2 seconds to execute.
We would love to spread those LEFT JOIN in separate queries to minimize data flow.

@Penners
Copy link

Penners commented Jan 28, 2022

Feature looks great! Looking forward to when it gets merged.

@pleerock
Copy link
Member

pleerock commented Feb 9, 2022

implemented in #8616

@pleerock pleerock closed this Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Entity .find() running out of memory with nested eager relations
5 participants