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
Prisma generated query takes 10x as long than a manually composed one #14402
Comments
Maybe its relatated to cursor pagination with desc order #12650 |
I do have an explicit DESC index set for sortTime though. |
U can try implement cursor pagination without prisma cursor. You can find some inspiration there #11138 (comment) |
Prisma cursor don't work when u provide non existent cursor #14283 |
At this point, I've gone back to using an offset based pagination (code or see it in action here when you scroll to the bottom of the page). While this is normally not ideal, I usually only need the first few hundred records of a sorted list of over 130k, so the offset value never gets to big that the usual performance problems of having to traverse too much data via an offset will come up. As there are a few cursor-related issues reported here, perhaps there will be some optimizations on the Prisma side down the line so I can revisit this. |
I can't do a simple greater than comparison on the ID as a cursor replacement like it's suggested in this comment. The sortTime order and my ID sort order are not always in sync, so I would need to do something like this: AND "Video"."sortTime" <= (
SELECT
"sortTime"
FROM
"Video"
WHERE
id = 29949) I can't do that natively in Prisma (without Prisma cursors), and if I split this into two Prisma calls to get the sortTime first, that's another performance hit that negates any improvements. |
Having a very similar issue to this with related tables as well. In my case, I'm trying to filter on a few related tables and seeing much more complicated logic generated by prisma versus when I add a simple join manually. Here's the query I'm passing to prismaClient:
I added the The full query diff is here Does anyone know why Prisma created the join this way? When I run explain analyze with my query I had 198ms execution time and Prisma's is 1.9 seconds (10x difference) I would love to use the query syntax and not use a raw query, so any help would be greatly appreciated! Thanks in advance 🙏 |
My issue above seems to be related to this: #13306 Seems like I might have to go with a raw query after all... |
I have a pretty standard GraphQL resolver Prisma query that ends up being horribly inefficient when I let Prisma generate the SQL, as opposed to just writing the (Postgre) SQL myself. At my current database size, it takes over 5 seconds via Prisma, and about 400 ms via SQL that I have composed.
At this point, I am not sure if this is just a fundamental Prisma issue in how it generates SQL in my case, or if there is anything I can do to avoid having to resort to a raw query.
More details, including my indexes and an
EXPLAIN (ANALYZE, BUFFERS)
output over at Stack Overflow, but here is the Prisma syntax......the relevant parts of my Prisma schema (some fields left out for brevity, and some of my indexes have temporarily been defined outside of my Prisma schema, directly on the DB while I debug this)...
...the SQL that this generates (Prisma 4.0.0)...
...and my much faster version version that I wrote:
I also noticed that the Prisma syntax does not add a
LIMIT
statement, despite there being atake
parameter. Is that to be expected? Curiously, if I add an explicitLIMIT
to the Prisma generated SQL, the query takes about twice as long.I've also tried using a composite index (
CREATE INDEX "Video_sortTime_status_idx" ON "Video"("sortTime" DESC, status)
) as suggested on SO, but this hasn't helped (updated EXPLAIN output). Over on SO, they suggested Postgres will only use one index, and I guess it's favouring Prisma's default one it set up for the primary key?The text was updated successfully, but these errors were encountered: