일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- javav 1676
- spring mongodb switch
- java 1509
- java 팩토리얼 개수
- spring mongoTemplate switch
- mongodb lookup
- Java Call By Refernce
- 안정해시
- java 파티
- spring mongodb
- go
- 백준 1504 java
- Spring ipfs
- kotiln functional interface
- 익명 객체 @transactional
- java 백준 1509
- kotiln const val
- 백준 특정한 최단 경로
- java 1238
- spring mongoTemplate
- kotiln const
- 전략 패턴이란
- ipfs singletone
- ipfs bean
- 자바 1676
- 백준 2252 줄세우기
- nodejs rabbitmq
- 자바 백준 팩토리얼 개수
- 백준 연결요소 자바
- rabbitmq 싱글톤
Archives
- Today
- Total
공부 흔적남기기
Mongodb lookup 최적화 본문
728x90
반응형
- localField와 foreignField를 사용
- foreignField에 index를 생성해서 사용
- pipeline을 통해서 match를 걸어 사용할 경우 $expr을 사용해야하기 때문에
- 인덱스가 사용되지 않는다.
- from collection에서 가져올 데이터가 적다면
- pipeline에 $project를 사용하고 해당 데이터들에 대해 인덱스를 생성해서 사용
- 이런 방식으로 사용하면 collection을 읽지않고 covering Index를 사용해서 데이터를 가져오기 때문에 효과적인 $lookup 사용이 가능하다.
Good Example
Boards collection 에 userId와 userName에 index가 각각 걸려있음
coveringIndex를 사용하기 때문에 explain 해보면
"totalDocsExamined": 0 통해 알 수 있음
{
from: "Boards",
localField: "userId",
foreignField: "userId",
pipeline: [
{
$project: {
userName : 1
}
}
],
as: "result"
}
Bad Example
expr 때문에 agentId index를 사용하지못해 table full scan
{
from: "Boards",
let: {userId: "$userId"}
pipeline: [
{$match: {$expr: {$eq: ["$userId", "$$userId"]} },
{
$project: {
userName : 1
}
}
],
as: "result"
}
728x90
반응형
'데이터베이스 > MongoDB' 카테고리의 다른 글
Spring Mongodb Pagination Helper Code (0) | 2023.12.20 |
---|---|
Spring data mongo CustomAggregation Invalid reference 트러블 슈팅 (0) | 2023.02.14 |
MongoTemplate in 순서대로 가져오기 (0) | 2022.07.15 |