I'm using TypeORM with NestJS and MySQL.
I've noticed that this particular query appears frequently in RDS performance insights, and it's causing performance issues. The query selects id and user_id (relation id of user table) from user_auth table. This query has high Load by waits and Avg latency (ms)/call.
SELECT `user_auth`.`id` AS `id`, `user_auth`.`user_id` AS `user_id`
FROM `user_auth` `user_auth`
WHERE ((`user_auth`.`user_id` = ?)
OR (`user_auth`.`user_id` = ?)
OR (`user_auth`.`user_id` = ?)
OR...
I'm curious about why this query occurs so frequently. How can I go about resolving this performance issue?
I couldn't find method finding many user_auth.id
on my code.
Something suspicious is that User and UserAuth Entity have a OneToOne relationship.
user-auth.entity.ts
@Field(() => User)
@OneToOne(() => User, (user) => user.auth, {
createForeignKeyConstraints: false,
})
@JoinColumn({ name: 'user_id' })
user: User;
@Field(() => ID)
@RelationId((userAuth: UserAuth) => userAuth.user)
userId: number;
user.entity.ts
@Field(() => UserAuth, {
nullable: true,
})
@OneToOne(() => UserAuth, (userAuth) => userAuth.user)
auth: UserAuth;
@Field(() => ID)
@RelationId((user: User) => user.auth)
authId: number;
The issue has been resolved.
The @RelationId
authId
was the cause - when I deleted it, that SQL was no longer executed.
It looks like the query was trying to read from the user_auth
table because the user
table doesn't have a auth_id
.