Database Reference
In-Depth Information
This approach has the advantage of simplicity, since we can use MongoDB's “upsert” func-
tionality to have the documents spring into existence as the hits are recorded.
There are, however, significant problems with this approach. The most significant issue is that
as you add data into the
hourly
and
monthly
fields, the document grows. Although Mon-
goDB will pad the space allocated to documents, it must still reallocate these documents mul-
tiple times throughout the day, which degrades performance, as shown in
Figure 4-2
.
The solution to this problem lies in
pre-allocating
documents with fields holding
0
values be-
fore the documents are actually used. If the documents have all their fields fully populated at
pre-allocation time, the documents never grow and never need to be moved. Another benefit
is that MongoDB will not add as much padding to the documents, leading to a more compact
data representation and better memory and disk utilization.
Figure 4-2. Performance with growing documents
One problem with our approach here, however, is that as we get toward the end of the day, the
updates still become more expensive for MongoDB to perform, as shown in
Figure 4-3
. This
is because MongoDB's internal representation of our
minute
property is actually an array of