-- MongoDB
[MongoDB] 인덱스
어린왕자악꿍
2013. 1. 5. 11:10
1. 인덱스생성
쿼리에대한인덱스를생성하기전에이미생성된인덱스를아래와같이조회하여어떤인덱스를생성할것인지판단한다.
> db.person.getIndexes()
쿼리에대한속도를향상하기위해인덱스를생성할때아래와같이한다.
> db.person.ensureIndex({ "name" : 1 }) // 1 : 오름차순, -1 : 내림차순
UNIQUE 인덱스
> db.person.ensureIndex({ "emp_no" : 1 }, {background : true, unique : true, dropDups : true })
background : 인덱스생성시백그라운드작업으로실행
unique : 인덱스를고유한고유인덱스로생성
dropDups : 기존문서를고유인덱스로만들때, 중복값이있다면삭제해고유인덱스생성
3. 인덱스삭제
이미생성된인덱스중필요하지않아삭제할경우에는다음과같이한다.
> db.person.dropIndex({"name" : 1 })
{ "nIndexesWas" : 3, "ok" : 1 }
모든인덱스삭제
> db.person.dropIndexes()
{
"nIndexesWas" : 4,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
4. 인덱스힌트사용
쿼리에특정인덱스를사용하도록할때 hint를사용한다.
> db.person.find({ name : "neo" }).hint({ "name" : -1 })