MongoDB Storage Optimization
MongoDB: Storage Optimization Just by Using Shorter Filed-Names/Enum-Values.
We can save on disk/memory depends on the document size number of fields their length and the size of the value assigned.
This is an easy small straightforward change not used much, We might not want to shorten the field-name/enum values present for the existing collection, but keeping this in mind when creating a new collection will help, will see how.
Why?
- Filed name size/length contributes to the document size as MongoDB stores all field names in every document. The contribution ratio depends on the number of fields their length and the size of the value and document size.
MongoDB documentation for the same: https://docs.mongodb.com/manual/core/data-model-operations/
Pros:
- Storage optimization [ 15–30 percent depending on the document/field/value size]
- Reduced Cache Pressure [smaller document loaded into memory/cache]
- Reduced Network usage
- Reduced Disk I/O
Cons:
- Low Readability- With short names/abbreviation have low readability, not everyone will be able to get the meaning out of those name.
Test to See Storage Optimization Gain:
Tried using shorter filed name for one of the collection say “hotelReservation”, inserted 10 million duplicate documents(for easy/accurate comparison).
Size mentioned here is the actual document size, not size on disk(that may vary depending compression technique used by MongoDB engine)
- Below is the structure of all the 4 document’s mentioned above with script to populate the data.
// original document
for(var count = 0;count<10000000; count++ ){
db.hotelReservation_original.insert({
"_class" : "com.org.reservation.hotel.data.reservation.entity.NucleusReservation",
"reservationId" : "21464163",
"clientCode" : "HILTON-GROUP",
"propertyCode" : "NEW-YORK",
"correlationId" : "52307154",
"bookingDate" : "2019-01-09T17:06:16.000",
"status" : "IN_HOUSE",
"nationality" : "US",
"channel" : "PMS",
"roomStays" : [
{
"arrivalDate" : ISODate("2019-06-14T05:00:00.000+0000"),
"departureDate" : ISODate("2019-06-15T05:00:00.000+0000"),
"marketCode" : "CT",
"sourceBookingCode" : "HOTEL",
"numberOfChildren" : NumberInt(0),
"numberOfAdults" : NumberInt(1),
"bookingType" : "CHECKED IN",
"invTypeCode" : "Q",
"bookedAccomTypeCode" : "CREW",
"rateCode" : "RATE01",
"invBlockCode" : "XXX12",
"roomNumber" : "201",
"rates" : [
{
"rateValue" : NumberDecimal("54"),
"startDate" : ISODate("2019-06-14T05:00:00.000+0000"),
"endDate" : ISODate("2019-06-15T05:00:00.000+0000"),
"grossRate" : NumberDecimal("54")
}
],
"services" : [],
"originalCurrencyCode" : "USD",
"analyticalMarketSegmentCode" : "CT"
}
],
"numberOfRooms" : NumberInt(1),
"sharers" : [],
"profileId" : "99999",
"versionId" : "52307154",
"createDate" : ISODate("2019-06-15T02:38:39.858+0000"),
"lastModifiedDate" : ISODate("2019-06-15T04:49:28.435+0000"),
"statisticsCorrelationId" : "5d04bd79dda81d87a0fc5e57"
});
}
;
db.hotelReservation_original.ensureIndex({
"clientCode" : 1,
"propertyCode" : 1,
"reservationId" : 1
});
// short Name document
for(var count = 0;count<10000000; count++ ){
db.hotelReservation_shortName.insert({
"_class" : "com.org.reservation.hotel.data.reservation.entity.NucleusReservation",
"rId" : "21464163",
"cCode" : "HILTON-GROUP",
"pCode" : "NEW-YORK",
"corrId" : "99999999",
"booking" : "2019-01-09T17:06:16.000",
"status" : "IN_HOUSE",
"nation" : "US",
"channel" : "PMS",
"rStays" : [
{
"arrival" : ISODate("2019-06-14T05:00:00.000+0000"),
"departure" : ISODate("2019-06-15T05:00:00.000+0000"),
"mCode" : "CT",
"sBookingCode" : "HOTEL",
"children" : NumberInt(0),
"adult" : NumberInt(1),
"bType" : "CHECKED IN",
"invTypeCode" : "Q",
"bAccTypeCode" : "CREW",
"rateCode" : "RATE01",
"invBlkCode" : "XXX12",
"rNumber" : "201",
"rates" : [
{
"rate" : NumberDecimal("54"),
"start" : ISODate("2019-06-14T05:00:00.000+0000"),
"end" : ISODate("2019-06-15T05:00:00.000+0000"),
"gRate" : NumberDecimal("54")
}
],
"services" : [],
"oCurrency" : "USD",
"alMsgCode" : "CT"
}
],
"ooms" : NumberInt(1),
"sharers" : [],
"profileId" : "99999",
"vId" : "99999999",
"created" : ISODate("2019-06-15T02:38:39.858+0000"),
"modified" : ISODate("2019-06-15T04:49:28.435+0000"),
"sCorrId" : "5d04bd79dda81d87a0fc5e57"
});
}
;
db.hotelReservation_shortName.ensureIndex({
"cCode" : 1,
"pCode" : 1,
"rId" : 1
});// short Name no _class document
for(var count = 0;count<10000000; count++ ){
db.hotelReservation_shortName_noClass.insert({
"rId" : "21464163",
"cCode" : "HILTON-GROUP",
"pCode" : "NEW-YORK",
"corrId" : "99999999",
"booking" : "2019-01-09T17:06:16.000",
"status" : "IN_HOUSE",
"nation" : "US",
"channel" : "PMS",
"rStays" : [
{
"arrival" : ISODate("2019-06-14T05:00:00.000+0000"),
"departure" : ISODate("2019-06-15T05:00:00.000+0000"),
"mCode" : "CT",
"sBookingCode" : "HOTEL",
"children" : NumberInt(0),
"adult" : NumberInt(1),
"bType" : "CHECKED IN",
"invTypeCode" : "Q",
"bAccTypeCode" : "CREW",
"rateCode" : "RATE01",
"invBlkCode" : "XXX12",
"rNumber" : "201",
"rates" : [
{
"rate" : NumberDecimal("54"),
"start" : ISODate("2019-06-14T05:00:00.000+0000"),
"end" : ISODate("2019-06-15T05:00:00.000+0000"),
"gRate" : NumberDecimal("54")
}
],
"services" : [],
"oCurrency" : "USD",
"alMsgCode" : "CT"
}
],
"ooms" : NumberInt(1),
"sharers" : [],
"profileId" : "99999",
"vId" : "99999999",
"created" : ISODate("2019-06-15T02:38:39.858+0000"),
"modified" : ISODate("2019-06-15T04:49:28.435+0000"),
"sCorrId" : "5d04bd79dda81d87a0fc5e57"
});
}
;
db.hotelReservation_shortName_noClass.ensureIndex({
"cCode" : 1,
"pCode" : 1,
"rId" : 1
});// short name, no class and no clientCode/propertyCode(instead used mapped pId(propertyId))for(var count = 0;count<10000000; count++ ){
db.hotelReservation_shortName_noClass_noClientPropertyCode.insert({
"rId" : "21464163",
"pId" : NumberInt(1),
"corrId" : "99999999",
"booking" : "2019-01-09T17:06:16.000",
"status" : "IN_HOUSE",
"nation" : "US",
"channel" : "PMS",
"rStays" : [
{
"arrival" : ISODate("2019-06-14T05:00:00.000+0000"),
"departure" : ISODate("2019-06-15T05:00:00.000+0000"),
"mCode" : "CT",
"sBookingCode" : "HOTEL",
"children" : NumberInt(0),
"adult" : NumberInt(1),
"bType" : "CHECKED IN",
"invTypeCode" : "Q",
"bAccTypeCode" : "CREW",
"rateCode" : "RATE01",
"invBlkCode" : "XXX12",
"rNumber" : "201",
"rates" : [
{
"rate" : NumberDecimal("54"),
"start" : ISODate("2019-06-14T05:00:00.000+0000"),
"end" : ISODate("2019-06-15T05:00:00.000+0000"),
"gRate" : NumberDecimal("54")
}
],
"services" : [],
"oCurrency" : "USD",
"alMsgCode" : "CT"
}
],
"ooms" : NumberInt(1),
"sharers" : [],
"profileId" : "99999",
"vId" : "99999999",
"created" : ISODate("2019-06-15T02:38:39.858+0000"),
"modified" : ISODate("2019-06-15T04:49:28.435+0000"),
"sCorrId" : "5d04bd79dda81d87a0fc5e57"
});
}
;
db.hotelReservation_shortName_noClass_noClientPropertyCode.ensureIndex({
"pId" : 1,
"rId" : 1
});
//
Hope this would be helpful.
Happy Coding, Thank you :)