Skip to content
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

ME: Many-to-many table indices sub-optimal #554

Closed
pimeys opened this issue Mar 4, 2020 · 2 comments · Fixed by #561
Closed

ME: Many-to-many table indices sub-optimal #554

pimeys opened this issue Mar 4, 2020 · 2 comments · Fixed by #561
Assignees
Labels
kind/bug A reported bug.

Comments

@pimeys
Copy link
Contributor

pimeys commented Mar 4, 2020

Let's imagine a model:

model User {
  id         Int       @id
  friendWith User[]    @relation("FriendShip")
  friendOf   User[]    @relation("FriendShip")
}

In this case we have a many-to-many relation, and we create an extra table for the relations:

         Table "_FriendShip"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 A      | integer |           | not null |
 B      | integer |           | not null |

And a few constraints:

Indexes:
    "_FriendShip_AB_unique" UNIQUE, btree ("A", "B")
Foreign-key constraints:
    "_FriendShip_A_fkey" FOREIGN KEY ("A") REFERENCES "User"(id) ON UPDATE CASCADE ON DELETE CASCADE
    "_FriendShip_B_fkey" FOREIGN KEY ("B") REFERENCES "User"(id) ON UPDATE CASCADE ON DELETE CASCADE

Now everything works fine when selecting the friendWith attribute, due to us doing a search with the column A. But when using the friendOf attribute, we suddenly query the table from the column B causing a sub-optimal performance.

We should always add an additional index for the column B to prevent situations like this.

@pimeys pimeys added the kind/bug A reported bug. label Mar 4, 2020
@do4gr
Copy link
Member

do4gr commented Mar 4, 2020

Prisma 1.0 and 1.1 had both the unqiue(A,B) and index(B). I agree that we should re-introduce that.

@pimeys
Copy link
Contributor Author

pimeys commented Mar 4, 2020

The same bug applies in cases where we have a many-to-many relationship between two separate tables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A reported bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants