Glavin001 / Cobra

API Server for storing and graphing real-time time-series data in MongoDB
MIT License
18 stars 1 forks source link

Switch to using an Array instead of Object #3

Closed Glavin001 closed 9 years ago

Glavin001 commented 9 years ago

See http://stackoverflow.com/a/21924176/2578205

{
  timestamp_hour: ISODate("2013-10-10T23:00:00.000Z"),
  type: “memory_used”,
  values: [
    [ 999999, 999999, …, 1000000 ],
    [ 2000000, 2000000, …, 1000000 ],
    …,
    [ 1600000, 1200000, …, 1100000 ],
    [ 1300000, 1400000, …, 1500000 ]
  ]
}
Glavin001 commented 9 years ago
{
  timestamp_hour: ISODate("2013-10-10T23:00:00.000Z"),
  type: “memory_used”,
  values: [
    [ 999999, 999999, …, 1000000 ],
    [ 2000000, 2000000, …, 1000000 ],
    …,
    [ 1600000, 1200000, …, 1100000 ],
    [ 1300000, 1400000, …, 1500000 ]
  ],
  stats: [
    [ count, total, min, max], // Minute 0
    [ count, total, min, max], // Minute 1
    // ....
  ],
  count: 0,
  total: 0,
  min: 0,
  max: 0
}
Glavin001 commented 9 years ago

Benchmarking

Before (using subdocument schema)

image

After (using array schema)

image

Glavin001 commented 9 years ago

I notice that the min field is not getting set, because BSON comparison order puts null as less than a number. Instead of storing it in the order of [count, total, min, max], store it as [count, total, max, min] where min is empty by default, not null, such that on insert/default it is [0,0,0] for [count,total,max,min] and the $min update modifier will detect that the 3rd index is missing and initialize it with the minimum value.