Hey there,
I am performing a query with multiple relations and the performance is so low,
The query is:
const company = await this.dataSource
.getRepository(CompanyEntity)
.createQueryBuilder('company')
.leftJoinAndSelect('company.financial', 'financial')
.leftJoinAndSelect('company.default', 'default')
.leftJoinAndSelect('company.management', 'management')
.leftJoinAndSelect('company.board', 'board')
.leftJoinAndSelect('company.shareholder', 'shareholder')
.leftJoinAndSelect('company.subsidiary', 'subsidiary')
.leftJoinAndSelect('company.segment', 'segment')
.leftJoinAndSelect('company.client', 'client')
.leftJoinAndSelect('company.project', 'project')
.leftJoinAndSelect('company.projectList', 'projectList')
.leftJoinAndSelect('company.bid', 'bid')
.where('company.ratingId = :ratingId', { ratingId })
.orderBy('company.id', 'DESC')
.getOne();
Copying the raw sql code from this query in DBEaver tooks about 50ms, but creating the current object tooks about 27segs,
If in CompanyEntity change all the relations with lazy = true and query like:
const company = await this.dataSource
.getRepository(CompanyEntity)
.createQueryBuilder('company')
.where('company.ratingId = :ratingId', { ratingId })
.orderBy('company.id', 'DESC')
.getOne();
await Promise.all([
company.financial,
company.default,
company.management,
company.board,
company.shareholder,
company.subsidiary,
company.segment,
company.client,
company.project,
company.projectList,
company.bid,
]);
This way executing separated queries only took 151ms,
The question is:
¿Anyone knows why take so long when there are so many relations?
¿Any method to get whole data in a single query?
Thanks in advance,