I using MongoId reading through the given answer for the question I understood Mongoid
does not cache any query
Now when running the below query
PartPriceRecord.destroy_all
PartPriceRecord.count()
==> 31721
How that is possible i just delete all the record ,this get me a feel mongo is caching certain query
In my mongoid query log I can see for every delete two query operating
MONGODB (0ms) decisiv_staging['system.indexes'].insert([{:name=>"part_number_1", :ns=>"decisiv_staging.part_price_records", :key=>{"part_number"=>1}, :unique=>true}])
MONGODB (0ms) decisiv_staging['part_price_records'].remove({:_id=>BSON::ObjectId('4fa39b35edd7f4306b0042bf')})
Signify that index are created and record is getting removed.
so how come mongo is caching the query is there any way from mongoid to clear mongo cache
If mongo is caching the query
Caveat: I am not a mongoid user.
Mongo itself doesn’t do any caching like that.
If you look at the queries executed on the db:
Then execute your code, you’ll see something like this in the log:
Looking directly at the db will allow you to know/understand exactly what queries are issued and if there is caching – where it is (it won’t be in the db itself).
However, the big difference above to the query log you’ve shown in the question is that
.remove({:_id=>BSON::ObjectId('4fa39b35edd7f4306b0042bf')})will remove at most one object, the conditions above ({}) will truncate the collection.Looking at the docs for destroy all, it runs callbacks on each deleted row – which probably means it deletes them one at a time (which with 30k rows will take a significant time). You can use delete all and it will (I assume) directly issue a query to delete all rows (
db.part_price_records.remove({})) instead. If you need destroy callbacks, you need to investigate why your code apparently only finds one row to delete, or which callback is aborting the delete for the other existing rows.