티스토리 뷰

BigData/MongoDB

MongoDB 인덱스 관리

Tomining 2015. 3. 5. 18:38
1. 인덱스 생성 & 삭제
db.emp.ensureIndex({ eno:1 }, { unique:true });
db.emp.dropIndex({ eno:1 });

     1 => ASC
     -1 => DESC

2. 인덱스 재구성 및 삭제
db.emp.dropIndexes();
db.emp.dropIndex({ eno:1 });
db.runCommand({dropIndexes:'em', index:{eno:1}})
db.emp.reIndex();
db.runCommand({reIndex:'emp'});
  • Index의 대소문자는 엄격히 구분됩니다.
  • Document를 Update 할 때 해당 Index Key만 변경되지만, 변경되는 Document 크기가 기존 Extent 공간 크기보다 큰 경우에 더 큰 Extent 공간으로 마이그레이션 될 수 있기 때문에 성능 저하 현상이 발생할 수 있습니다.(충분한 Extent 크기로 설계해야 합니다.)
  • sort() 절과 limit() 절은 함께 사용하는 것이 성능에 도움이 됩니다.

3. 인덱스 종류
INDEX타입
설명
Non Unique Index
( Single Key Index
Compound Key Index)
하나 또는 하나 이상의 중복 값을 가진 Field로 구성되는 Index로 가장 대표적은 Balance Tree Index 구조로 생성됩니다.

Ex) db.emp.ensureIndex({eno:1})
     db.emp.ensureIndex({eno:1, loc:-1})
Unique Index
Index가 생성되는 Field가 유일한 속성 값을 가진 Index타입입니다.

Ex) db.emp.ensureIndex({eno:1, ename:1}, {unique:true})
Sparse Index
하나 이상의 필드에 Null값을 가진 데이터가 대부분이고 드물게 어떤 데이터 값을 가지고 있는 경우에 생성하면 효율적인 Index입니다.

Ex) db.emp.ensureIndex({desc:1}, {sparse:true})
  
4. Background Index
Index 타입
설명
Background Index
일반적으로 인덱스의 생성은 DB 전체의 성능 지연 현상을 유발 시킬 수 있습니다. (V1.3.2부터 지원)
Covered Index
여러 개의 Field로 생성된 Index를 검색할 때 Index 검색 만으로도 조건을 만족하는 Document를 추출할 수 있는 타입입니다.

Ex) db.users.ensureIndex({username:1, password:1, roles:1});
     db.users.save({username:"joe", password:"pass", roles:2});
     db.users.save({username:"liz", password:"pass2", roles:4});
     db.users.find({username:"joe"}, {_id:0, roles:1}).explain();
     {"cursor" : " .... ",
indexOnly: true}
DropDups Index
동일한 값이 여러 개 저장되어 있는 필드에 DropDups Index를 생성하면 최초 입력된 Document만 남고 나머지 Document는 제거됩니다.

Ex) db.people.ensureIndex({idate:1}, {dropdups:true})
GeoSpatial Index
좌표로 구성되는 2차원 구조로 하나의 Collection에 하나의 2D Index를 생성할 수 있습니다.
  
5. Covered Index
Oracle의 IOT 개념으로 Document 검색시 index 검색만을 통해서 원하는 데이터를 추출할 수 있다는 장점을 갖고 있습니다. 실행계획에서 "index Only : true"를 확인 할 수 있습니다. 이 검색은 컬렉션에 대한 추가 검색을 피할 수 있기 때문에 I/O를 최소화하여 성능 개선을 시킬 수 있습니다.

6. GeoSpatial Index
좌표에 의해 구성되는 2차원 구조로 하나의 Collection에 하나의 2D Index를 생성할 수 있습니다.

Mongo Reference 페이지에서 아래와 같이 정의하고 있습니다.

When you create a geospatial index on legacy coordinate pairs, MongoDB computes geohash values for the coordinate pairs within the specified location range and then indexes the geohash values.

To calculate a geohash value, recursively divide a two-dimensional map into quadrants. Then assign each quadrant a two-bit value. For example, a two-bit representation of four quadrants would be:

01  11

00  10

These two-bit values (00, 01, 10, and 11) represent each of the quadrants and all points within each quadrant. For a geohash with two bits of resolution, all points in the bottom left quadrant would have a geohash of 00. The top left quadrant would have the geohash of 01. The bottom right and top right would have a geohash of 10 and 11, respectively.

To provide additional precision, continue dividing each quadrant into sub-quadrants. Each sub-quadrant would have the geohash value of the containing quadrant concatenated with the value of the sub-quadrant. The geohash for the upper-right quadrant is 11, and the geohash for the sub-quadrants would be (clockwise from the top left): 1101, 1111, 1110, and 1100, respectively.


  • geohash - a binary representation of the location on a coordinate grid.

Multi-location Documents for 2d Indexes

New in version 2.0: Support for multiple locations in a document.

While 2d geospatial indexes do not support more than one set of coordinates in a document, you can use a multi-key index to index multiple coordinate pairs in a single document. In the simplest example you may have a field (e.g. locs) that holds an array of coordinates, as in the following example:

{ _id : ObjectId(...),
  locs : [ [ 55.5 , 42.3 ] ,
           [ -74 , 44.74 ] ,
           { lng : 55.5 , lat : 42.3 } ]}

The values of the array may be either arrays, as in [ 55.5, 42.3 ], or embedded documents, as in { lng : 55.5 , lat : 42.3 }.

You could then create a geospatial index on the locs field, as in the following:

db.places.ensureIndex( { "locs": "2d" } )

You may also model the location data as a field inside of a sub-document. In this case, the document would contain a field (e.g. addresses) that holds an array of documents where each document has a field (e.g. loc:) that holds location coordinates. For example:

{ _id : ObjectId(...),
  name : "...",
  addresses : [ {
                 context : "home" ,
                 loc : [ 55.5, 42.3 ]
                } ,
                {
                 context : "home",
                 loc : [ -74 , 44.74 ]
                }
              ]}

You could then create the geospatial index on the addresses.loc field as in the following example:

db.records.ensureIndex( { "addresses.loc": "2d" } )








'BigData > MongoDB' 카테고리의 다른 글

MongoDB Having 쿼리 적용하기  (0) 2015.03.05
MongoDB Aggregation시 Document Size 오류  (0) 2015.03.05
MongoDB paddingFactor 옵션  (0) 2015.03.05
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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
글 보관함