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

Support for SQL-level DISTINCT operation for PostgreSQL #14765

Open
kibertoad opened this issue Aug 11, 2022 · 5 comments
Open

Support for SQL-level DISTINCT operation for PostgreSQL #14765

kibertoad opened this issue Aug 11, 2022 · 5 comments
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/feature A request for a new feature. status/is-preview-feature This feature request is currently available as a Preview feature. topic: distinct topic: nativeDistinct topic: performance/memory topic: performance/queries topic: performance

Comments

@kibertoad
Copy link

Problem

#2835 only implemented in-memory DISTINCT filtering, which is not ideal when dealing with large amount of data.

Suggested solution

Support generating DISTINCT SQL query.

Additional context

Possible API:

const result = await prisma.user.findMany({
  where: {},
  distinctSql: ['id', 'email']
})
@janpio janpio added kind/feature A request for a new feature. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: distinct labels Aug 11, 2022
@janpio
Copy link
Member

janpio commented Aug 23, 2022

Some additional context from previous issues #2835:

According to the docs, this distinct feature doesn't actually generate a distinct query, but filters the result set programmatically. This isn't a great option when there are thousands of records. Is there an open feature request for generating true distinct queries?

prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#distinct-under-the-hood

image

@Druue
Copy link
Contributor

Druue commented Dec 7, 2023

Hey!

We’ve just added support for distinct to use native SQL in Prisma 5.7.0 for PostgreSQL with the nativeDistinct preview feature. Once enabled, DISTINCT ON will be used instead of performing the distinct operation in memory with a caveat for now where it will revert back to in memory processing when used in conjunction with orderBy.
We plan on building this out further so that we can remove this limitation and also add support for other databases.

If you’re curious, we’ve added an example SQL output in our latest release notes
We also have a dedicated preview feature feedback discussion here if you have any thoughts you’d like to share :)

Cheers!

@kibertoad
Copy link
Author

@Druue Thanks a lot, this is fantastic! Any particular reasons for keeping it in preview? Is it potentially unstable and can eat my data if used in production?

@Druue
Copy link
Contributor

Druue commented Dec 7, 2023

Hey @kibertoad, it’s so that folks have the ability to opt-in/out as we continue to build the feature and listen to feedback around performance and correctness. So please try it out and let us know if it still returns the correct data, and does so faster than before!

@janpio
Copy link
Member

janpio commented Apr 12, 2024

Support for other databases has been split into its own issue here: #23846

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/feature A request for a new feature. status/is-preview-feature This feature request is currently available as a Preview feature. topic: distinct topic: nativeDistinct topic: performance/memory topic: performance/queries topic: performance
Projects
None yet
Development

No branches or pull requests

3 participants