我有一个巨大的收藏品(1000万),我想搜索并删除比时间戳更旧的记录。
我在字段 lastUpdatedTime
上创建了一个索引 db.MyCol.remove({"lastUpdatedTime" : {$lt: ISODate("2016-10-06 00:00:00 AM") }})
以上删除查询超时并修改为使用BulkOperation。
在连接时执行id为4334的命令'delete'失败 'connectionId {localValue:13,serverValue:22}'到服务器'XXXXX:27017' 除了'com.mongodb.MongoSocketReadTimeoutException:超时 在接收消息'
时
我知道mongo不支持删除限制。所以,我正在实现类似下面的内容
//Read 10K records
BasicDBObject query = new BasicDBObject();
query.append("lastUpdatedTime",
new BasicDBObject("$lte", new Timestamp(cal.getTimeInMillis())));
DBCursor cursorDocBuilder = myCol.find(query).limit(10000);
// Get Ids
BasicDBList inList = new BasicDBList();
while (cursorDocBuilder.hasNext())
{
inList.add(cursorDocBuilder.next().get("_id"));
}
//construct In clause
BasicDBObject deleteQuery = new BasicDBObject();
deleteQuery.put("_id", new BasicDBObject(MongoOps.$IN, inList));
WriteResult result =myCol.remove(deleteQuery);
P.S:我可以做多个线程来清理。我不想限制数据库,因为我期望对同一个集合进行高读/写操作。
添加explain()以获取1000条记录
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "XXX",
"indexFilterSet" : false,
"parsedQuery" : {
"lastUpdatedTime" : {
"$lt" : ISODate("2016-10-06T00:00:00Z")
}
},
"winningPlan" : {
"stage" : "LIMIT",
"limitAmount" : 1000,
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"lastUpdatedTime" : 1
},
"indexName" : "lastUpdatedTime_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"lastUpdatedTime" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"lastUpdatedTime" : [
"(true, new Date(1475712000000))"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1000,
"executionTimeMillis" : 200,
"totalKeysExamined" : 1000,
"totalDocsExamined" : 1000,
"executionStages" : {
"stage" : "LIMIT",
"nReturned" : 1000,
"executionTimeMillisEstimate" : 201,
"works" : 1001,
"advanced" : 1000,
"needTime" : 0,
"needYield" : 0,
"saveState" : 10,
"restoreState" : 10,
"isEOF" : 1,
"invalidates" : 0,
"limitAmount" : 1000,
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 1000,
"executionTimeMillisEstimate" : 201,
"works" : 1000,
"advanced" : 1000,
"needTime" : 0,
"needYield" : 0,
"saveState" : 10,
"restoreState" : 10,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 1000,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1000,
"executionTimeMillisEstimate" : 0,
"works" : 1000,
"advanced" : 1000,
"needTime" : 0,
"needYield" : 0,
"saveState" : 10,
"restoreState" : 10,
"isEOF" : 0,
"invalidates" : 0,
"keyPattern" : {
"lastUpdatedTime" : 1
},
"indexName" : "lastUpdatedTime_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"lastUpdatedTime" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"lastUpdatedTime" : [
"(true, new Date(1475712000000))"
]
},
"keysExamined" : 1000,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
}