IdentityMap
This section is relevant only if you are using Mongoid 3. IdentityMap
has been removed in Mongoid 4 because it now includes Mongoid::Relations::Eager
.
An IdentityMap
pattern is a design pattern where the object gets loaded only once from the database, and is then stored in the map. So, the next lookup is directly from the map and hence faster. The IdentityMap
pattern has been implemented in Mongoid (and removed in the latest version).
Note
Ignore the rest of this section if you use Mongoid4. Only if you are using Mongoid 3, is the remaining section relevant.
If enabled properly, IdentityMap
can greatly improve the performance of queries. Whenever a document is loaded from the database, it is classified by class and ID kept in the IdentityMap
pattern. Then, when that document is looked up the next time, Mongoid looks into the IdentityMap
pattern before looking into the database.
So, how do we know that the IdentityMap
pattern is there? First, enable it.
# config/mongoid.yml development...