rivantsov / vita

VITA Application Framework
MIT License
59 stars 15 forks source link

[Question] Efficient way for sub query #194

Closed jasonlaw closed 3 years ago

jasonlaw commented 3 years ago

I have the following entity schema.

[Entity]
public interface ICommunity
{
   string Name {get;set;} 
   IList<IMemberInCommunity> Members { get; }
}

[Entity]
public interface IMemberInCommunity
{
   string UserId {get; set;}
   ICommunity Community {get; set;}
}

Suppose a login user call an API, I want to return a list of Community, with a flag if the user is a member or not.

Eg of Community list output.

public class Community
{
   public string Name {get; set;}
   public bool IsMember  {get; set;}
}

May I know what is the most efficient way to construct the query? I am thinking of using View but it doesn't has parameter.

rivantsov commented 3 years ago

The simplest and probably the most efficient way is to retrieve list of user's communities; and then, assuming you have a list of all communities cached somewhere in c#, merge it with user's communities to get what you want, on c# side. I assume lists are hundreds, not millions

jasonlaw commented 3 years ago

Thanks for the suggestion, I will try that out. Btw, is it possible for the view to have parameter?

rivantsov commented 3 years ago

NO, as far as I know. There's such thing as Table-valued functions in MS SQL, these are essentially views with params.

jasonlaw commented 3 years ago

I am using mysql and also try not to have too much dependency to a specific dbms. Anyway, thanks for the clarification. Your suggestion works well so will closing this issue.

One out topic questions, I am very interested to understand how smart loading works and how it solve the n+1 problem, do you have plan to write some documents about it?

Vita is a great framework, really appreciate your work.

rivantsov commented 3 years ago

smart loading - explained here, in the last section Batching and ORM: https://github.com/rivantsov/ngraphql/wiki/QuickStart

jasonlaw commented 3 years ago

The smart loading is only effective in ngraphql? Can it be utilized in REST?

rivantsov commented 3 years ago

yes, anywhere, just open session with this extra flag and you have it working. And all stuff applies

jasonlaw commented 3 years ago

Cool!!! Thanks for the clarification! :)