-
Notifications
You must be signed in to change notification settings - Fork 155
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
Ability to omit table joining if relations are not needed by API consumer #20
Comments
This also seems an appropriate time to ask why selectin was chosen as the lazy loading type? |
I think cancel the relationship between all tables may be more flexible, and let the program itself handle the relationship. |
By program you mean the client consuming the API? What if a person needed in some cases to see the hero with their team, and in other cases just needed hero, and the spending time on the join was useless? |
Hello @bazylhorsey the selectin was used on relationships due to the async implementation of sqlmodel I was facing this issue fastapi/sqlmodel#74. I implemented selectin on the Relationship otherwise it should be done on the query. I agree that queries can be optimized so some joining can be omitted. What do you mean by "change to a join at query time"? Do you want those body params to allow users to decide if they want to have the relationship response? A sample with a custom query like this?
|
Hello @bazylhorsey lazy can also be joined, subquery, or selectin as described here https://docs.sqlalchemy.org/en/14/orm/loading_relationships.html I have tested elapsed time with different lazy techniques but the time is similar, a couple of us import timeit
elapsed_time = timeit.repeat(lambda: (await crud.hero.get_multi_paginated(params=params) for _ in '_').__anext__(), number=10000, repeat=5)
for index, exec_time in enumerate(elapsed_time, 1):
m_secs = round(exec_time * 10 ** 2, 2)
print(f"Case {index}: Time: {m_secs}µs")
print(f"Mean: {round(max(elapsed_time)* 10 ** 6, 2)}") |
Finally found a general guideline for loading techniques! 🥂 My understanding django -> fastapi:
A colleague of mine had a solution to your bug. In your crud base you can use response.unique().scalar_one_or_none() |
Hello @bazylhorsey thanks for this reference I am going to try "response.unique().scalar_one_or_none()" and see what is its output |
I'd go for using joined in all your many-to-ones (side with foreign key), and one-to-one (both sides). |
Hello @bazylhorsey can you please share your sample code you were testing? |
@bazylhorsey Based on loading relationships and the insight of your link. One-One and One-Many relationships have been updated to use sa_relationship_kwargs={"lazy": "joined"} and Many-Many relationships use sa_relationship_kwargs={"lazy": "selectin"} |
After turning on SQL echo, it seems that there can be quite a bit more querying than needs to necessarily happen.
Queries can be much faster if related tables are not unwrapped.
Example body param:
It would be EVEN BETTER if you could only select from one of the related fields. This could easily be implemented with an additional Enum representing the base table.
Example body param:
In cases where the expand_fields list is empty, it should not show the unwrapped location, and more importantly not perform the selectin.
The text was updated successfully, but these errors were encountered: