MongoDB – Insertion Speed

mongodb

Suppose you have a collection of students of the following form:

{"_id" : ObjectId("50c598f582094fb5f92efb96"),
"first_name" : "John",
"last_name" : "Doe",
"date_of_admission" : ISODate("2010-02-21T05:00:00Z"),
"residence_hall" : "Fairweather",
"has_car" : true,
"student_id" : "2348023902",
"current_classes" : [
   "His343",
   "Math234",
   "Phy123",
   "Art232"
]}

Now suppose that basic inserts into the collection, which only include the last name, first name and student_id, are too slow (we can't do enough of them per second from our program). What could potentially improve the speed of inserts. Check all that apply.

1) Add an index on last_name, first_name if one does not already exist.

2) Remove all indexes from the collection, leaving only the index on _id in place

3) Provide a hint to MongoDB that it should not use an index for the inserts

4) Set w=0, j=0 on writes

5) Build a replica set and insert data into the secondary nodes to free up the primary nodes.

I'm sure that the number 4 is correct. The numbers 1,3,5 are not correct. I'm not sure on the number 2. Is it correct?

Best Answer

1) Add an index will slow inserts.

2) It will speed up inserts

3) Inserts don't use indexes

4) It will speed up writes but w=0 risk consistency and j=0 risk durability

5) You can't insert data to secondaries, insert/update/delete applies only to primary

If you care for insert speed you should use hash based sharding which evenly distribute writes among shards and increase write throughput. (http://docs.mongodb.org/manual/tutorial/shard-collection-with-a-hashed-shard-key/)