Video Transcript (English)
Sometimes, we have to get even more granular and examine the specific collections.
In this video, we’ll use the db.collection.stats() helper method to provide information about a single collection since the last server restart. This is useful for assessing the performance of high-traffic collections.
To do this, we run the db dot collection name dot Stats() method in the MongoDB shell.
The output for collection stats is quite extensive, but we can also append a category name to the method to limit the results. Let’s look at some useful categories to monitor, including storageSize and IndexStats,.
Let’s run collection stats on the orders collection. We can look at storageSize to tell us how much disk space that a specific collection is using. If a collection has a disproportionately large storageSize, it might indicate inefficiencies in how we're storing data.
In this example, we see that the storageSize is surprisingly high. We might find that the orders collection contains a significant amount of historical order data that is rarely accessed. We should consider implementing a data archival strategy to move this older data to a less performance-critical storage tier.
The collection stats method also provides a breakdown of each index on the collection, including the total index size and index builds in progress. We can use this information to identify which indexes are being used effectively.
In another example, after running collection stats on the products collection, we can see that the ratings_1 index is the largest index, consuming almost the entire total index size.
We should investigate this index further to assess whether its large size is justified. To do that, we’ll use IndexStats to see how often it’s being used.
Here, we’re using IndexStats to learn more about the ratings_1 index. We can see that ratings_1 is never used so it’s not improving query performance.
While you may be tempted to immediately drop this index to reclaim the space and resources it uses, MongoDB recommends that you use the hideIndex() method to evaluate the impact of dropping an index before actually doing so.
Here, we’re using hideIndex() to hide the index from the query planner so that it will not be used to support our queries. If, after testing, we see that hiding the index has no impact on performance, we can use the dropIndex method to delete the index.
Congratulations! In this video we explored the db dot collection dot stats() method to examine specific collections, gaining insights into their storage characteristics and index usage for performance tuning.
