Skip to content

Instantly share code, notes, and snippets.

@boazadato
Created February 17, 2022 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boazadato/09c526c184c41f1ba34e9181d282d916 to your computer and use it in GitHub Desktop.
Save boazadato/09c526c184c41f1ba34e9181d282d916 to your computer and use it in GitHub Desktop.
Reproducing Mongoid 7.3.3 simple find possible performance issue
require 'mongoid'
Mongoid.configure do |config|
config.clients.default = {
hosts: ['localhost:27017'],
database: 'junk_db',
}
config.raise_not_found_error = false
end
class Cat
include Mongoid::Document
end
logger = Logger.new(STDOUT)
logger.level = :debug
Mongoid.logger = logger
Mongo::Logger.logger = logger
Cat.collection.drop
cat_id = BSON::ObjectId.from_string('620e1d1e4ca89036e25d2b6f')
Cat.collection.insert_one({ _id: cat_id })
# According to STDOUT logger, the following will use:
# {"find"=>"cats", "filter"=>{"_id"=>BSON::ObjectId('620e1d1e4ca89036e25d2b6f'), "$and"=>[{"_id"=>BSON::ObjectId('620e1d1e4ca89036e25d2b6f')}]}
#
# corresponding mongodb log, shows IXSCAN usage:
# "msg":"Only one plan is available; it will be run but will not be cached","attr":{"query":"ns: junk_db.cats query: { _id: ObjectId('620e1d1e4ca89036e25d2b6f'), $and: [ { _id: ObjectId('620e1d1e4ca89036e25d2b6f') } ] } sort: {} projection: {}","planSummary":"IXSCAN { _id: 1 }"}}
Cat.find(cat_id)
# According to STDOUT logger, the following will use:
# {"find"=>"cats", "filter"=>{"_id"=>BSON::ObjectId('620e1d1e4ca89036e25d2b6f')}
#
# corresponding mongodb log, shows idhack usage:
# msg":"Using idhack: {canonicalQuery_Short}","attr":{"canonicalQuery_Short":"ns: junk_db.cats query: { _id: ObjectId('620e1d1e4ca89036e25d2b6f') } sort: {} projection: {}"}}
Cat.collection.find({_id: cat_id}).to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment