prisma orm 2
user profile avatar
Tech Wizard

Published on • 🕑3 min read

My Experience with PRISMA so Far and Why I am Considering Switching to Drizzle

3likes14

Blog views1.4K

Listen to this blog

I recently switched to using Prisma ORM for the backend of my blog. This decision came after encountering difficulties hosting my Ruby on Rails API for free. Most hosting platforms, including my favorite Railway, require a monthly payment, which exceeds my current budget. Here is my experience with Prisma so far.

About Prisma

Prisma is a next-generation ORM that makes working with databases easy for application developers.

  • Object-relational mapper
  • A way to interact with our database
  • Completely type safe

Prisma is super easy to work with, and I was able to switch my whole database in just 4 days. The Prisma schema makes it super easy to declare relationships between models, and with Prisma seeding feature also makes it easy to populate the database with existing data.

Here is a sample prisma schema

sample-prisma-schema

However, I initially struggled with the seeding process and I had to create data manually via API routes. Together with NextJS API routes, Prisma is super fun to work with. One major benefit of NextJS is that expensive queries can be cached so that refreshing the route does not re-fetch the data from the database.
Another super cool feature of Prisma is the Prisma studio. This opens up a new tab where users can easily visualize data and even manage the data, such as creating new records, updating existing data, and deleting records. Individuals can access Prisma Studio by running.

npx prisma studio

prisma-studio

Challenges

Immediately after switching to Prisma, I realized my server was taking too long to respond. A single login request, which often took 700ms to complete, took over 10s. This delay is significant and could affect user performance. 

Requests made with Prisma were ten times slower than normal!

I looked around to try and understand why this was happening or some errors that I could be making. Prisma documentation suggests indexing frequently queried columns such as username and email by adding @@index([username, email]).

Query is 5 times slower when using `findMany` compared to running identical  query via `queryRaw` · Issue #11130 · prisma/prisma · GitHub

However, this does not solve the issue. Prisma is too slow, especially in queries that involve selecting data from more than one table.

The database also has a cold start, which means that after the Prisma client disconnects, new requests will take time.

PRISMA is also not designed to run on the edge, where vercel hosts their serveless functions. This implies that the servers are not up and running all the time in a serverless environment, thus the cold starts before Vercel spins the function and awakens the database.

Here is what I have realized so far:

  • Prisma is not suited to run on the edge and thus the database sleeps when the Prisma client disconnects. Prisma takes time to connect again.
  • Every new insert via Prisma opened a database-level transaction (?). An extremely weird design choice by Prisma. This makes you exhaust your connection pool very fast.
  • There is no concept of SQL-level joins in Prisma. This means for every transaction that requires join, prisma fetches both tables, merges the data, and returns it to the user. This makes the findMany query take too long.
  • Prisma is fixing the SQL-level joins with a new feature called relation joins. However, this is still a preview feature. Users can enable this by adding this to their schema: preview features = ["relationJoins"
  • On every insert, Prisma opens a transaction where it inserts and then returns the inserted record too, even if we do not want it.
  • For every action (Create, Read, Update, and Delete) prisma returns the object by default even if you do not want it. Even for delete!
  • Prisma supports JSONB datatypes in Postgresql and MySQL. However, there is no way to update such data type. The existing way is to fetch the whole object, spread it, and then add or delete data, then post the same object again. This is utterly ridiculous.
  • Prisma has no way of hiding sensitive data, such as not returning password fields when you fetch a user object. 

I have heard good things about some other ORMs such as Drizzle and Kysely, although they are not as easy to work with as Prisma. However, I will consider interacting with them first before making a concrete decision.

Update:

I switched to Prisma Accelerate and now Prisma is very fast with queries although it does not beat Active-Record.

Maybe Prisma is intentionally slow so individuals can switch to the paid plan on Prisma Accelerate! However, I do agree that simplicity can make you get hooked.

Like what you see? Share with a Friend

3 Comments

14 Likes

Comments (3)

sort comments

Before you comment please read our community guidelines


Please Login or Register to comment

user profile avatar

diamond degesh

Published on

Try drizzle it is super fast although you will not enjoy the simplicity Prisma brings since you have to write SQL.

user profile avatar

Tutor Juliet

online

Published on

This is my first time hearing about PRISMA not gonna lie 🤥 😂😂

user profile avatar

Tech Wizard

online

Published on

Maybe I was too harsh but the current setup cannot also work well under concurrency. I admit I have alot to learn but I might have jumped from fire to the frying pan