검색 페이지에 들어올 때 마다 모든 유저의 [ id, name, introduce, profile_image ]를 가져와 클라이언트로 전송한다.
const userProfileList = await this.userRepository
.createQueryBuilder("user")
.select("user.user_id", "user_id")
.addSelect("user.name", "name")
.addSelect("user.introduce", "introduce")
.addSelect("user.profile_image", "profile_image")
.getRawMany();
클라이언트측에서는 검색을 할 때 해당 검색어를 리스트의 name에서 찾아 일치하는 유저들을 보여준다.
검색 페이지에 들어올 때 모든 유저의 정보를 가져오는게 너무 오래 걸린다.
검색 페이지에 들어오면 로딩 시간이 필요한 상황이라 사용자 입장에서 사용했을 때 심각한 불편을 느꼈다.
개발자 도구에서 네트워크를 확인해보니 모든 유저 정보를 가져오는데 대략 120ms이 걸리는 것을 볼 수 있으며, 다른 API와 비교했을 때 대략 4~6배 느린것을 확인할 수 있다.
데이터베이스에서 문자열을 찾는 쿼리를 날릴 때 가장 먼저 떠오르는 것은 MySQL의 LIKE
이다. 초기에는 LIKE
를 이용한 쿼리를 개발하고, 추후에는 조회 시에 인덱스를 이용해서 성능을 개선할려 했다.
const userProfileList = await this.userRepository
.createQueryBuilder("user")
.select("user.user_id", "user_id")
.addSelect("user.name", "name")
.addSelect("user.introduce", "introduce")
.addSelect("user.profile_image", "profile_image")
.where("user.name like :name", { name: `%${userName}%`})
.getRawMany();