Skip to content
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 unnecessary join in RelationFilter #3882

Merged
merged 1 commit into from
Sep 13, 2023
Merged

Conversation

KhooHaoYit
Copy link
Contributor

@KhooHaoYit KhooHaoYit commented Apr 17, 2023

Fixes prisma/prisma#18343
Mitigates prisma/prisma#16450

This PR aims to improve relation filter by removing unnecessary join

How this is achieved is when a model contains fields that can reference to the related table, we can use that fields instead of getting the values from the related table and joining the table
If the fields are stored in the other model, join table would still be performed

There might be performance regression where the related table might return the same identifier multiple time (Many to one) but if that is a concerns we can fall back to join table

Before patch

SELECT "public"."User"."id",
  "public"."User"."email",
  "public"."User"."name"
FROM "public"."User"
WHERE ("public"."User"."id") IN (
    SELECT "t0"."id"
    FROM "public"."User" AS "t0"
      INNER JOIN "public"."Post" AS "j0" ON ("j0"."authorId") = ("t0"."id")
    WHERE (
        "j0"."title"::text LIKE 'a'
        AND "j0"."content"::text LIKE 'b'
        AND "t0"."id" IS NOT NULL
      )
  )

After patch

SELECT "public"."User"."id",
  "public"."User"."email",
  "public"."User"."name"
FROM "public"."User"
WHERE ("public"."User"."id") IN (
    SELECT "t0"."authorId"
    FROM "public"."Post" AS "t0"
    WHERE (
        "t0"."title"::text LIKE 'a'
        AND "t0"."content"::text LIKE 'b'
        AND "t0"."authorId" IS NOT NULL
      )
  )

More SQL improvement can be done by following these suggestions (Not implemented in this PR)

@KhooHaoYit KhooHaoYit requested a review from a team April 17, 2023 08:04
@codspeed-hq
Copy link

codspeed-hq bot commented Apr 17, 2023

CodSpeed Performance Report

Merging #3882 KhooHaoYit:main (7458eac) will not alter performances.

Summary

🔥 0 improvements
❌ 0 regressions
✅ 6 untouched benchmarks

🆕 0 new benchmarks
⁉️ 0 dropped benchmarks

@janpio
Copy link
Contributor

janpio commented Apr 24, 2023

(Rerunning hopefully flaky test)

@Weakky
Copy link
Contributor

Weakky commented Jun 7, 2023

Hey @KhooHaoYit, thanks a lot for your second contribution! We'll make sure to review your PR soon 🙏

@janpio janpio requested a review from a team as a code owner June 14, 2023 10:52
@janpio janpio requested a review from Weakky July 25, 2023 14:32
Copy link
Contributor

@Weakky Weakky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the late answer again @KhooHaoYit. As we're improving relational filters (mostly using joins for to-one relational filters), your PR will be a great additional improvement! Great job spotting it and thank you for the contribution 💯🙏.

@Weakky Weakky added this to the 5.4.0 milestone Sep 13, 2023
@Weakky Weakky merged commit e6d9ad6 into prisma:main Sep 13, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary INNER JOIN when doing nested queries
3 participants