Why do RelationQueryService creates an INNER JOIN for all entities instead of matching them by their reference? #365
MateoGuerreroE
started this conversation in
General
Replies: 1 comment 1 reply
-
Thinking a bit more about this - Maybe is typeorm defaults and not this lib? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So, a bit of context. We connect this resolvers to generated views, and some of those views have a large amount of results due to some calculations, etc. (Some can have more than 20M entries). We scope those views by some fields included on the
Authorization
decorator, so when querying entities from there, the dataset is actually 10K-20K, and is executed sufficiently fast.However - When I want to include a relation on the query, the executed SQL is creating a INNER JOIN agains the whole raw view, matching the whole 20M records since the authorization that's taking is from the relation DTO, and not the base. This has caused some simple queries to take 50s and some complex more than 10minutes.
I was able to find a workaround extending
RelationQueryService
and overwritting thefindRelation
method so It does not do all that, but It was kind of tricky since I had to overwrite the actual query from the relation. I took thedto
list (already paginated) that's received by that method and using the query service of the relation entity did a query based on the reference fields (I mean, for example, Ifentity
is related torelation
by therelatedTo
field on the entity, aiming torelationId
on the relation entity, I queried the queryService of the relation using those fieldsfilter: { relationId: { eq: dto.relatedTo }}
).So normal behavior is for the entity list to have a INNER JOIN against the base entity table, and then use the entity (not the relation) id to match on those. So If I want to obtain
relation
fromentity
It will take relation table, do a INNER JOIN against entity table and then match usingentityId
, and It works but I want to understand If there's a specific reason why? Maybe for the ability to abstract since the queryService is only created within the instance of a class?This may be just not optional for large data sets, specially when
entity
andrelation
may have different authorization filters.Beta Was this translation helpful? Give feedback.
All reactions