Phương thức count() trong MongoDB có thể chậm trên collection lớn. Cách tối ưu hóa hiệu quả nhất là tạo **index** cho trường được query.
Ví dụ minh họa
Tạo collection và dữ liệu
> db.countPerformanceDemo.insertOne({"StudentName":"John","StudentCountryName":"US"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ebcf82f684a30fbdfd55f") }
> db.countPerformanceDemo.insertOne({"StudentName":"Mike","StudentCountryName":"UK"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ebd042f684a30fbdfd560") }
> db.countPerformanceDemo.insertOne({"StudentName":"David","StudentCountryName":"AUS"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ebd112f684a30fbdfd561") }
> db.countPerformanceDemo.insertOne({"StudentName":"Carol","StudentCountryName":"US"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ebd1a2f684a30fbdfd562") }
> db.countPerformanceDemo.insertOne({"StudentName":"Bob","StudentCountryName":"UK"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ebd212f684a30fbdfd563") }
> db.countPerformanceDemo.insertOne({"StudentName":"David","StudentCountryName":"UK"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ebd9a2f684a30fbdfd564") }
> db.countPerformanceDemo.insertOne({"StudentName":"David","StudentCountryName":"US"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c8ebd9e2f684a30fbdfd565") }
Xem tất cả documents
> db.countPerformanceDemo.find().pretty();
{
"_id" : ObjectId("5c8ebcf82f684a30fbdfd55f"),
"StudentName" : "John",
"StudentCountryName" : "US"
}
{
"_id" : ObjectId("5c8ebd042f684a30fbdfd560"),
"StudentName" : "Mike",
"StudentCountryName" : "UK"
}
{
"_id" : ObjectId("5c8ebd112f684a30fbdfd561"),
"StudentName" : "David",
"StudentCountryName" : "AUS"
}
{
"_id" : ObjectId("5c8ebd1a2f684a30fbdfd562"),
"StudentName" : "Carol",
"StudentCountryName" : "US"
}
{
"_id" : ObjectId("5c8ebd212f684a30fbdfd563"),
"StudentName" : "Bob",
"StudentCountryName" : "UK"
}
{
"_id" : ObjectId("5c8ebd9a2f684a30fbdfd564"),
"StudentName" : "David",
"StudentCountryName" : "UK"
}
{
"_id" : ObjectId("5c8ebd9e2f684a30fbdfd565"),
"StudentName" : "David",
"StudentCountryName" : "US"
}
Tạo index để tối ưu count()
> db.countPerformanceDemo.ensureIndex({"StudentName":1});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
Thực hiện count() sau khi có index
> db.countPerformanceDemo.find({"StudentName":"David"}).count();
3
Có 3 document với StudentName = "David". Với index, MongoDB chỉ cần quét index thay vì quét toàn bộ collection.
Các phương pháp count() khác (MongoDB 4.0+)
| Phương thức | Mô tả | Hiệu năng |
|---|---|---|
count() | Deprecated, dùng find().count() | Chậm nếu không có index |
countDocuments() | Đếm chính xác, hỗ trợ query filter | Tốt hơn count() |
estimatedDocumentCount() | Ước lượng nhanh dựa trên metadata | Nhanh nhất (không chính xác 100%) |
Best Practice
- Luôn tạo index cho trường thường dùng trong query count
- Dùng
estimatedDocumentCount()khi chỉ cần ước lượng tổng số document - Dùng
countDocuments({filter})khi cần đếm chính xác với điều kiện - Tránh
count()không có điều kiện trên collection lớn