prisma icon indicating copy to clipboard operation
prisma copied to clipboard

Slow queries in tables related via explicit many-to-many relation table

Open slinden2 opened this issue 5 years ago • 2 comments

Bug description

I am encountering a problem that causes very slow queries using prisma2, nexus and nexus-plugin-prisma. The problem happens in a simple query that gets data from multiple tables related via an explicit many-to-many relation table.

The use case is surely fairly common. I think this is me doing something wrong rather than an actual bug.

Expected behavior

I expected the data loader to kick in and result in much faster and less queries.

Prisma information

Prisma schema

model Conference {
  id              String                    @id @default(cuid())
  conferenceIdApi Int                       @unique
  link            String
  name            String
  shortName       String
  abbreviation    String
  activeSeasons   ConferenceActiveSeasons[]
  divisions       DivisionConference[]
  teams           TeamConference[]
}

model Division {
  id            String                  @id @default(cuid())
  divisionIdApi Int                     @unique
  link          String
  name          String
  shortName     String
  abbreviation  String
  activeSeasons DivisionActiveSeasons[]
  conferences   DivisionConference[]
  teams         TeamDivision[]
}

model DivisionConference {
  id           String     @id @default(cuid())
  season       String
  conference   Conference @relation(fields: [conferenceId], references: [id])
  conferenceId String
  division     Division   @relation(fields: [divisionId], references: [id])
  divisionId   String

  @@unique([season, conferenceId, divisionId])
}

GraphQL objects

export const Conference = objectType({
  name: "Conference",
  definition(t) {
    t.model.id();
    t.model.divisions();
  },
});

export const Division = objectType({
  name: "Division",
  definition(t) {
    t.model.id();
    t.model.conferences();
  },
});

export const DivisionConference = objectType({
  name: "DivisionConference",
  definition(t) {
    t.model.id();
    t.model.season();
    t.model.conference();
    t.model.conferenceId();
    t.model.division();
    t.model.divisionId();
  },
});

Defined query

export const DivisionQuery = extendType({
  type: "Query",
  definition(t) {
    t.nonNull.list.field("allDivisions", {
      type: Division,
      async resolve(_root, _args, ctx) {
        const divisions = await ctx.prisma.division.findMany({
          include: { conferences: true },
        });
        return divisions;
      },
    });
  },
});

GraphQL query

query {
  allDivisions {
    id
    conferences {
      season
      conference {
        id
      }
    }
  }
}

Log when I execute the query: https://pastebin.com/fZsbvM45

Environment & setup

  • OS: Windows 10
  • Database: PostgreSQL
  • Node.js version: 15.3.0
  • Prisma version: 2.16.0
  • Nexus version: ^1.0.0
  • nexus-plugin-prisma: 0.29.0

slinden2 avatar Feb 04 '21 17:02 slinden2

Hey @slinden2

Can you please try this with the latest version once? I remember we fixed a N+1 bug recently so this might be already addressed in the latest version. Also, update the nexus plugin.

pantharshit00 avatar Mar 10 '21 08:03 pantharshit00

I am going to close this as its been 3 months now and there is no activity here.

Please reply if you want us to take another look.

pantharshit00 avatar May 16 '21 13:05 pantharshit00