[MongoDB] Multikey Index

기훈·2024년 3월 24일

MongoDB

목록 보기
18/28

Multikey Index: 배열필드거나 배열안의 내장된 필드에 인덱스를 생성하는 인덱스

왼쪽은 두개의 도큐먼트, 오른쪽은 총 네개의 도큐먼트지만 addr에 인덱스를 설정하면 인덱스의 수는 같다.(왼쪽은 멀티키인덱스, 오른쪽은 싱글인덱스로 적용된다)

Multikey Index 비용
배열에 대한 읽기에 속도는 빨라지지만, 수정의 비용은 배열에 비례하여 증가한다.(1.5정도의 비용 소모)

데이터베이스에 인덱스 설정하기

// 내림차순으로 인덱스 생성
db.data.createIndex({sections: -1})

db.data.getIndexes()

// stage: 'IXSCAN', isMultiKey: true 
db.data.find({ sections: 'AG1' }).explain('executionStats')

// 배열안 내장 도큐먼트에 대해 인덱스 설정
db.grades.createIndex({"scores.type":1})

db.grades.getIndexes()

// stage: 'IXSCAN', isMultiKey: true 
db.grades.find(
    {"scores.type": "exam"}
).explain('executionStats')

// 멀티키인덱스 + 복합인덱스 
db.grades.createIndex(![](https://velog.velcdn.com/images/rlaejrqo465/post/239db27a-fdaf-4ea8-b93e-704eacece7eb/image.png)

    {class_id: 1, "scores.type": 1}
)

db.grades.find(
    {
        "scores.type": "exam",
        class_id: {
            $gte: 350
        }
    }
).explain("executionStats")

업로드중..

0개의 댓글