私はそのパフォーマンスをチェックするためにmongoDbで少し遊んでいます。1 つの小さなコレクションを使用して、Java で小さなテストを作成しました。
public class ClientWord {
@Id
private ObjectId id;
@Field(value="client_id")
private ObjectId clientId;
private String text;
private int value;
....
このドキュメントには、二重の複合インデックスがあります。
> db.client_words.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test1.client_words",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"client_id" : 1,
"text" : 1
},
"unique" : true,
"ns" : "test1.client_words",
"name" : "client_id_1_text_1"
}
]
私のテストでは、100.000 個のドキュメントが挿入されます。
public void test(){
int size = 10;
List<ObjectId> clientIds = createClientIds(size);
t1 = t2 = t3 = new Date();
printWithTime("Inserting... ");
for (int i=0; i<10000; i++){
for (ObjectId id : clientIds){
mongoTemplate.upsert(
Query.query(Criteria.where("text").is("text"+i).and("client_id").is(id)),
Update.update("text","text"+i).set("client_id",id).inc("value", 3),
ClientWord.class);
}
if(i%100 == 0){
System.out.printf("\t%6d\t",i*size);
printWithTime("");
}
}
printWithTime("Total time: ");
System.out.println("Total elements in the ddbb: "+mongoTemplate.count(new Query(), ClientWord.class));
}
通常のコレクションと上限のあるコレクションでこのコードを試しました。通常は10分以上かかり、上限は20秒。また、コレクションが成長している間、通常のコレクションはますます時間がかかるのに対し、 capped は一定時間の挿入を行うこともわかりました。これは、インデックスのサイズですが、両方のコレクションのインデックスが同じであるためだと思います...
なぜそんなに大きな違いがあるのか 誰か説明できますか?通常の収集パフォーマンスを改善するためのトリックはありますか?