programing

mongodb 인덱스 생성 작업 상태

bestprogram 2023. 5. 7. 12:02

mongodb 인덱스 생성 작업 상태

저는 MongoDB를 사용하고 있으며 약 7천 5백만 개의 레코드가 있는 컬렉션을 가지고 있습니다.다음 명령을 사용하여 두 개의 "필드"에 복합 인덱스를 추가했습니다.

db.my_collection.ensureIndex({"data.items.text":1, "created_at":1},{background:true}).

이틀 후에 인덱스 생성 상태를 확인하려고 합니다.입니다.db.currentOp()돌아온다{}그러나 다른 인덱스를 생성하려고 하면 다음 오류 메시지가 표시됩니다.

cannot add index with a background operation in progress.

인덱스 생성 작업의 상태/진행 상황을 확인할 수 있는 방법이 있습니까?

한 가지 추가할 것은 - 저는 mongodb 버전 2.0.6을 사용하고 있습니다.감사합니다!

mongo 셸에서 아래 명령을 입력하여 현재 진행률을 확인합니다.

rs0:PRIMARY> db.currentOp(true).inprog.forEach(function(op){ if(op.msg!==undefined) print(op.msg) })

Index Build (background) Index Build (background): 1431577/55212209 2%

실시간 실행 상태 로그를 수행하는 방법

> while (true) { db.currentOp(true).inprog.forEach(function(op){ if(op.msg!==undefined) print(op.msg) }); sleep(1000); }
Index Build: scanning collection Index Build: scanning collection: 43687948/47760207 91%
Index Build: scanning collection Index Build: scanning collection: 43861991/47760228 91%
Index Build: scanning collection Index Build: scanning collection: 44993874/47760246 94%
Index Build: scanning collection Index Build: scanning collection: 45968152/47760259 96%

유휴 연결 및 시스템 작업을 포함하여 보다 자세한 출력을 반환하는 true 인수와 함께 currentOp를 사용할 수 있습니다.

db.currentOp(true)

그런 다음 db.killOp()사용하여 원하는 작업을 종료할 수 있습니다.

다음은 인덱스 진행률을 출력해야 합니다.

db
  .currentOp({"command.createIndexes": { $exists : true } })
  .inprog
  .forEach(function(op){ print(op.msg) })

출력:

Index Build (background) Index Build (background): 5311727/27231147 19%

안타깝게도 DR9885 답변은 저에게 효과가 없었고, 코드에 공백이 있으며(구문 오류) 공백이 제거되어도 아무것도 반환되지 않습니다.

이것은 Mongo Shell에서 작동합니다.v3.6.0

db.currentOp().inprog.forEach(function(op){ if(op.msg) print(op.msg) })

내가 내 것을 게시한 후까지 Bajal 답변을 읽지 않았지만, 코드가 약간 더 짧고 작동한다는 점을 제외하면 거의 똑같습니다.

좋아요:

db.currentOp({ 
    'msg' :{ $exists: true },
    'command': { $exists: true },
    $or: [ 
        { 'command.createIndexes': { $exists: true } }, 
        { 'command.reIndex': { $exists: true } }
    ]
}).inprog.forEach(function(op) { 
    print(op.msg); 
});

출력 예:

인덱스 빌드 인덱스 빌드: 84826/335739 25%

설명서는 다음을 제안합니다.

db.adminCommand(
    {
      currentOp: true,
      $or: [
        { op: "command", "command.createIndexes": { $exists: true }  },
        { op: "none", "msg" : /^Index Build/ }
      ]
    }
)

활성 인덱싱 작업 예제입니다.

진행 중인 단일 인덱스의 진행 상황을 확인할 수 있는 간단한 방법:

db.currentOp({"msg":/Index/}).inprog[0].progress;

출력:

{ "done" : 86007212, "total" : 96868386 }

인덱스 작업의 진행률을 찾습니다. 라이너 하나가 좋습니다.

> db.currentOp().inprog.map(a => a.msg)
[
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    "Index Build: scanning collection Index Build: scanning collection: 16448156/54469342 30%",
    undefined,
    undefined
]

언급URL : https://stackoverflow.com/questions/22309268/mongodb-status-of-index-creation-job