join-monster icon indicating copy to clipboard operation
join-monster copied to clipboard

Highlight the drawback on fetching one to many relations

Open tlvenn opened this issue 9 years ago • 10 comments

I think it would be fair to mention that whenever join-monster is joining a one to many, it will retrieve excessive data that will be thrown out and it gets pretty bad as soon as you nest them.

For example, in the case of user <-> comments, user data is duplicated for each comment. If you have user <-> comments <-> ratings where a given user have 10 comments and each comments have 20 ratings, the query will actually fetch for the given user:

  • user data 200 times
  • each comment, 20 times

tlvenn avatar Feb 04 '17 16:02 tlvenn

This is a valid point; however I would argue that the postgresql query planner will remove some of the redundancies for sure. I think that since GraphQL is designed to return redundant cycled data, that this point is sort of moot here. You could further tune join monster to avoid fetching duplicate data with something like facebooks' dataloader, but then you have to start reasoning about the data join both in application land and in sql land which is likely to increase programming errors (aka we have to return noop nodes for already fetched data, yet still join thru the primary keys so the rest of the child nodes can be associated properly).

dreamcodez avatar Feb 04 '17 23:02 dreamcodez

@tlvenn: would you have time to put together an example postgres schema & data set for postgres so I can do a query planning analysis (EXPLAIN ANALYZE) on the use case your referring too?

If postgres handles things efficiently on the server side, this is really only then adding overhead in rpc between graphql and database which should be on fast internal network if your architecture is setup correctly -- not to mention gzip will also reduce redundant data 'fatness'.

dreamcodez avatar Feb 04 '17 23:02 dreamcodez

Hi @dreamcodez,

The query planner will not remove any redundancy given the duplication happens after once the result is materialized. I am sure postgres try to be as clever as possible and minimize memory consumption but nevertheless, it's not a free operation and this is inherently a limitation of SQL.

On the wire, there is actually no compression as far as I know in the protocol. The only exception being if connection is over SSL and sslcompression flag is turned on (the default) but otherwise nothing is compressed. And most people dont setup their connections over SSL to talk to pgsql.

On the client side, the driver will have to ingest the whole stream which takes memory and cpu times as well.

Dont get me wrong, I am not saying join-monster is a bad idea but there are tradeoffs with this approach which are not properly documented imho. Also for example, joins are one of the most expensive/complex operation a DB can do, the query planner might do a good job to reduce the complexity or not and depending on your dataset, might be able to keep things in memory or not.

Approaches like Dataloader alleviate that issue all together by not leveraging joins at all which could be a very good thing, again, depending of your dataset. The net effect would be that dataloader will not load the database server as much with the tradeoff of a little more latenty for each graphql query given it needs more round trips to the DB.

As always, there is no silver bullet but one needs to carefully evaluate both approaches to understand which one should overall produce better results for his use case.

tlvenn avatar Feb 05 '17 03:02 tlvenn

@acarl005 any though on the above ?

tlvenn avatar Feb 06 '17 10:02 tlvenn

@tlvenn Thanks for expressing your concerns. Indeed, JOINs have the unfortunate effect of duplicating data at an exponential rate. Despite having taken the time to create a page in the docs regarding the cons of multiple round trips, I kind of neglected the cons of joins. More joins is not necessarily better than obsessively reducing the number of network requests. If I was sure this was all Join Monster was ever going to do, I would update that page as you suggested.

However, the current release of Join Monster is still version 0 and new functionality may be coming. I'm already working version 1.0 and one of the experimental features is the ability to use multiple batched requests instead of joins, kind of like DataLoader but still generating the SQL. Ironically, Join Monster may not need to do any joins if you configure it not to. It will be up to the user to choose the best strategy: joins, batches, or some mixture of the two.

acarl005 avatar Feb 06 '17 19:02 acarl005

I've found that Postgres' json_agg and to_json functions can be used to compact the joined data into a column on the parent row, however I'm sure there are limitations when it comes to maintaining data types. Although it might be worth a thought?

seratonik avatar Feb 07 '17 07:02 seratonik

@seratonik Yes aggregating as JSON can help mitigate the problem. It's not implemented because it's an explosion of complexity.

  1. You can't do any more JOINs after you json_agg one table - Join Monster will (currently) do multiple joins in order to resolve multiple object types. You can easily chain joins together and it can be done dynamically for each GraphQL query. If instead you do a json_agg, that's it, no more chaining. So the situations where we can take advantage of this are limited. It's not something the user can specify because they can't predict which join will be the "last one" due to the flexibility of GraphQL.
  2. Unexpected data types, as you said - Coercing to JSON will mean converting things like date or timestamp types to strings. Given that some database drivers convert data types based on column types, like timestamps to JavaScript Date objects, using json_agg in some queries will prevent the data types in JavaScript from being consistent.
  3. It's Postgres-specific - It might be worth attempting to implement if all the SQL databases could benefit from it. Sadly they cannot.

acarl005 avatar Feb 07 '17 08:02 acarl005

@seratonik I added a suggestion to the documentation for a strategy like this. link

acarl005 avatar Feb 23 '17 11:02 acarl005

Join Monster v1.0.0 now has batching baked in as an alternative to JOINs.

acarl005 avatar Feb 23 '17 11:02 acarl005

@acarl005 Thanks for the example, that's great! I'm loving join-monster.

seratonik avatar Feb 23 '17 17:02 seratonik