Postgres - Perform Inner Join without adding the I...
# orm-help
s
Postgres - Perform Inner Join without adding the IN Clause in the query cc: @Pieter
βœ… 1
We have two model 1. Foo a. id b. name 2. Bar a. id b. foo = FK (A) c. status We are trying to run a query which required to found count of Foo where status of Bar in ["pending", "inprogress"] So technically query should be in postgres is select count (*) from A INNER JOIN ON Foo.id = Bar.foo_id where Bar.status in ('pending', 'inprogress'); So here is our ORM query await prisma.Foo.count({ Bars: { some: { status: { in: ['pending', 'inprogress'], }, } } }) The query looks like following SELECT count (*) from (select Foo.id From Foo where (Foo.id IN (select id from Foo INNER JOIN Bar on Foo.id = Bar.foo_id where Bar.status in ('inprogress', 'pending')) )) This query is unnecessary adding the In Clause within the query which leads to unwanted Parallel hash join and have high impact in our response time.
Instead of making a JOIN on main query, it converting into the subquery
d
Seems like you can use β€œor” for this
s
Thanks for replying you mean OR instead of IN in status filter?
SELECT count (*) from (select Foo.id From Foo where (Foo.id IN (select id from Foo INNER JOIN Bar on Foo.id = Bar.foo_id where Bar.status in ('inprogress', 'pending')) )) I had mark in bold Foo.id IN which is giving issue
p
@Nurul @janpio any advice?
j
No advice from me, I am really bad at writing specific Prisma Client queries to achieve certain SQL queries - that is not really what Prisma is supposed to do. You ask it for some data, and it returns it to you. If this query is significantly worse than what could be, please open an issue of course and describe exactly what is going on etc. We might be able to add this as an optimization.
p
Thanks @janpio that makes total sense @Sanyam Bansal can you go ahead and flag a issue for the performance impact
πŸ‘ 1