Hi guys quick question, how much more performant i...
# orm-help
d
Hi guys quick question, how much more performant is a
findUnique
call compared to a
findFirst
call? Is there any difference and if so, how big is it?
c
It depends on the query, and there’s no general story here because the performance of the call reduces to the performance of the underlying query (in the case of SQL). Prisma does some of the legwork in that
findUnique
guarantees there’s an index it can look at, but the query planner for your DB can always have other plans.
findFirst
can be more performant if your indices are set up in a way more conducive to the query terms.
👍 2
d
Ah very interesting! Thanks for the reply @Casey Chow. I am looking into optimising my queries and this is very good to know 🙂 🙏
c
If you haven’t come across this yet I highly recommend this site: https://use-the-index-luke.com
Best of luck!
n
Hey Daan 👋
findUnique
uses only unique fields to find a property that will have performance benefits over large data sets, and is strict in using indexed properties.
findFirst
is essentially
findMany
with a take of one and returns the 0 index of the array. It was created to solve people having to return findMany()[0] and write their own catches and checks.
👍 3
j
FindUnique also is the recommended approach in graphql to ensure proper batching of queries, to counter the n+1 problem, right?
n
Yes, that’s true
findUnique
is recommended