[MongoDB]데이터 삭제 후 디스크 공간 돌려받기 및 속도향상

참고 사이트 : https://www.mongodb.com/blog/post/going-in-mongodb-42

 

Going in MongoDB 4.2 | MongoDB Blog

There's a lot going into MongoDB 4.2, like on-demand materialized views, pipeline powered updates and distributed transactions. And as we cleared the way for those new features, so we moved on and various commands and features that were previously deprecat

www.mongodb.com

MongoDB 4.2 버전에서 테스트 됨

compact

MongoDB는 deleteMany 시에 데이터는 삭제를 하지만 디스크 공간을 삭제한만큼 되돌려 주지 않는다.

이때 프라그먼테이션을 이용해 디스크 공간을 재할당하여 공간을 돌려 받을 수 있다.

use DB_name
db.runCommand({compact: "collection_name", force: false})

 

단 프라이머리 일 경우 위 처럼 사용할 경우 오류가 발생한다.

프라이머리일 경우에는 force: true 를 사용해야 한다.

또한 세컨더리에는 따로 명령을 내려줘야 세컨더리 디스크에도 재할당이 이뤄진다.

reIndex

색인이 많은데 삭제를 할 경우 색인이 뒤엉킬 수 있다고 한다. 이 때 인덱스를 다시 해 주면 도움이 된다고 함

db.collections_name.reIndex()

+ Recent posts