Created
February 17, 2022 10:11
-
-
Save boazadato/09c526c184c41f1ba34e9181d282d916 to your computer and use it in GitHub Desktop.
Reproducing Mongoid 7.3.3 simple find possible performance issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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