最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Question about Prisma indexes, response its slow - Stack Overflow

programmeradmin3浏览0评论

I just noticed that I haven an issue with one of my queries. I have the following model on prisma: Database: MYSQL

model Subscriber {
  id         String        @id @default(uuid())
  timestamp  DateTime      @default(now())
  popup_id   String
  user_id    String
  session_id String?
  popup_type PopupTypeEnum
  user_agent String?
  referrer   String?
  url        String
  email      String
  code       String?

  @@index([user_id, timestamp(sort: Desc)])
}

The issue is that when I fetch a response with the orderBy its taking 1 min to answer. And if I remove the orderBy its instant. The db has aprox 200k rows because im testing.

const subscribers = await prisma.subscriber.findMany({
        where: whereClause,
        take: limit,
        orderBy: { timestamp: "desc" },
        ...(oldCursor ? { cursor: { id: oldCursor }, skip: 1 } : {}),
      });

If I remove the orderBy

const subscribers = await prisma.subscriber.findMany({
        where: whereClause,
        take: limit,
        ...(oldCursor ? { cursor: { id: oldCursor }, skip: 1 } : {}),
      });

Works perfectly, can someone help me I need to order the response how can I fix this? Already added the indexes on prisma but still taking like 1 min to answer...

I would appreciate if someone can point me on the right direction!.

I just noticed that I haven an issue with one of my queries. I have the following model on prisma: Database: MYSQL

model Subscriber {
  id         String        @id @default(uuid())
  timestamp  DateTime      @default(now())
  popup_id   String
  user_id    String
  session_id String?
  popup_type PopupTypeEnum
  user_agent String?
  referrer   String?
  url        String
  email      String
  code       String?

  @@index([user_id, timestamp(sort: Desc)])
}

The issue is that when I fetch a response with the orderBy its taking 1 min to answer. And if I remove the orderBy its instant. The db has aprox 200k rows because im testing.

const subscribers = await prisma.subscriber.findMany({
        where: whereClause,
        take: limit,
        orderBy: { timestamp: "desc" },
        ...(oldCursor ? { cursor: { id: oldCursor }, skip: 1 } : {}),
      });

If I remove the orderBy

const subscribers = await prisma.subscriber.findMany({
        where: whereClause,
        take: limit,
        ...(oldCursor ? { cursor: { id: oldCursor }, skip: 1 } : {}),
      });

Works perfectly, can someone help me I need to order the response how can I fix this? Already added the indexes on prisma but still taking like 1 min to answer...

I would appreciate if someone can point me on the right direction!.

Share Improve this question edited Mar 6 at 13:01 PugProvoleta asked Mar 6 at 12:49 PugProvoletaPugProvoleta 1483 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You probably want to run a db profiler or queryplanner to diagnose properly, but at a first glance the issue may be that your index has fields - bothuserid and timestamp. Have you tried adding just a timestamp index, like @@index(timestamp(sort: Desc)) ?

Ok I fix it: Just need it to add to the orderBy the id also.

const subscribers = await prisma.subscriber.findMany({
        where: whereClause,
        take: limit,
        orderBy: [
          { timestamp: "desc" },
          { id: "desc" }, // Secondary sort for deterministic order
        ],
        ...(oldCursor
          ? {
              cursor: {
                id: oldCursor,
              },
              skip: 1,
            }
          : {}),
      });

And added this indexes:

model Subscriber {
  id         String        @id @default(uuid())
  timestamp  DateTime      @default(now())
  popup_id   String
  user_id    String
  session_id String?
  popup_type PopupTypeEnum
  user_agent String?
  referrer   String?
  url        String
  email      String
  code       String?

  @@index([timestamp(sort: Desc), id(sort: Desc)])
  @@index([user_id, timestamp(sort: Desc)])
}

Maybe it helps to someone.

发布评论

评论列表(0)

  1. 暂无评论