-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Remove the need for primary key column when embedding parents in &select #411
Comments
@ruslantalpa is this issue still relevant? |
Yes this is still an issue
returns this
|
This happens only in -- Current query generated by GET /projects?select=*,clients(id,name)
WITH pg_source AS (
SELECT "test"."projects".*,
row_to_json("clients_clients".*) AS "clients"
FROM "test"."projects"
LEFT OUTER JOIN (
SELECT "test"."clients"."id",
"test"."clients"."name"
FROM "test"."clients"
) AS "clients_clients" ON "clients_clients"."id" = "projects"."client_id")
SELECT NULL AS total_result_set,
pg_catalog.count(_postgrest_t) AS page_total,
array[]::text[] AS header,
coalesce(array_to_json(array_agg(row_to_json(_postgrest_t))), '[]')::character varying AS BODY
FROM ( SELECT * FROM pg_source) _postgrest_t;
-- Proposed new query with implicit joins
WITH pg_source AS (
SELECT "test"."projects".*,
(SELECT row_to_json("clients")
FROM (
SELECT "test"."clients"."id",
"test"."clients"."name"
FROM "test"."clients"
WHERE "test"."clients"."id" = "test"."projects"."client_id"
) "clients"
) AS "clients"
FROM "test"."projects")
SELECT NULL AS total_result_set,
pg_catalog.count(_postgrest_t) AS page_total,
array[]::text[] AS header,
coalesce(array_to_json(array_agg(row_to_json(_postgrest_t))), '[]')::character varying AS body
FROM ( SELECT * FROM pg_source) _postgrest_t; They both give the same result: [{"id":1,"name":"Windows 7","client_id":1,"clients":{"id":1,"name":"Microsoft"}},{"id":2,"name":"Windows 10","client_id":1,"clients":{"id":1,"name":"Microsoft"}},{"id":3,"name":"IOS","client_id":2,"clients":{"id":2,"name":"Apple"}},{"id":4,"name":"OSX","client_id":2,"clients":{"id":2,"name":"Appl
e"}},{"id":5,"name":"Orphan","client_id":null,"clients":null}] But the new query can be used without the The drawback of this new query is that is more expensive, when doing an EXPLAIN the costs are:
However the -- Many to many embed
GET /tasks?select=id,users{id}
cost=16838.48..16838.49
-- Child embed
GET /clients?select=*,projects{name}
cost=42793.13..42793.14 So as a first step towards solving this issue I'm gonna change the |
i am completley against that. the direction should be how do we make child embeds faster, not the other way arround. it would be mich better to make it so that if the user requests a parent without an id, we automatically add the id to the select so that we do not generate a bad query, and when the user wonders why did i get the id, the docs would say that it's necessary for the query. just because some cases of the embeds have worse performance, does nto amke it acceptable to drag down the other cases when it's fast |
I mentioned that the complete solution will have all the embedding queries doing explicit joins(lowest costs). |
but dont we have that right now with parrent embedding, and in this type of join, you need the id, I mean you are welcome to try :) and even if you are doing this for one level (up or down) think how it will work going three levels down. |
(opened this issue so that i don't forget the bug, and i have a ID for the issue)
The text was updated successfully, but these errors were encountered: