While researching ways to optimize database searches, I came across promise.all which has the potential to speed up the process.
My approach involves conducting searches through three functions. The tables consist of various joins, with instances where the same table needs to be queried multiple times.
async methodA(){
const [result1, result2] = await promise.all([
this.Arepository.find({relation:[...],where:{...}},
this.Brepository.find({relation:[...],where:{...}})
]);
for(const r of result1){
r.user = await this.usersRepository.findOne({id:r.user_id}}
}
return ...
}
async methodB(){
const [result1, result2] = await promise.all([
this.Brepository.find({relation:[...],where:{...}},
this.Crepository.find({relation:[...],where:{...}})
]);
for(const r of result1){
r.user = await this.usersRepository.findOne({id:r.user_id}}
}
return ...
}
async methodC(){
const [result1, result2] = await promise.all([
this.Arepository.find({relation:[...],where:{...}},
this.Crepository.find({relation:[...],where:{...}})
]);
for(const r of result1){
r.user = await this.usersRepository.findOne({id:r.user_id}}
}
return ...
}
The provided code is structured as described above. I am curious if running the following code in the controller would impact the speed:
const [a,b,c] = await Promise.all([
this.service.methodA(),
this.service.methodB(),
this.service.methodC()]);
In my current implementation, the time taken is approximately 1 second without utilizing promise.all and around 0.8-0.9 seconds when using it.
Assessing any additional effects from this optimization has proven challenging. Despite my research efforts, I have not found a definitive answer regarding whether there can be further improvements in speed, especially when querying the same table repeatedly.
I eagerly await your response. Thank you.