
// 1. 多次查询 const users = await Promise.all(userIds.map(id => { return this.userRepository.findOne({ where: { id } }) })) // 2. 一次查询 const users = await this.userRepository.createQueryBuilder('user') .where('user.id IN (:...ids)', { ids: userIds }) .getMany() 1 optional 2022-10-25 19:10:14 +08:00 一般情况肯定是 2 的性能比较好(只考虑数据库)。 但是如果放到实际项目中如果考虑缓存系统的话,1 的适应度反而会更高,缓存更友好更好控制。 |
2 lmshl 2022-10-25 21:18:45 +08:00 必然是 2 性能好,缓存交给数据库自己去做就行,这种主键检索数据库自己的缓存比什么都快 |