How do I modify an existing MongoDB index?
I have a series of indexes in my MongoDB and I think that one of the reasons I'm running the system at such a high CPU is that updating the indexes is blocking. (AWS micro instance running at 50%+ CPU during normal operation, 99.9% during heavy write operations).
I've got a good handful of indexes in place for fast queries, and now I'm thinking that I might be able to show some further improvements by moving index-building into a background operation.
I don't want to delete the indexes entirely (at least I don't think I do) instead I'd be happy if just "future operations" ran in the background.
I've looked at the mongo index building documentation http://www.mongo开发者_运维问答db.org/display/DOCS/Indexes and see the flags for turning on background operation (see below) but I don't see anything about how to modify an existing index.
db.things.ensureIndex({x:1}, {background:true});
> db.things.ensureIndex({name:1}, {background:true, unique:true,
... dropDups:true});
I tried running the original index command a second time with the updated parameter, but when I re-execute the db.colection.getIndexes();
comant, it does not show the 'background' parameter in the output.
{
"_id" : ObjectId("4de2c1a5c9907a4e77467826"),
"ns" : "mydb.items",
"key" : {
"itemA" : 1,
"itemB" : -1,
"itemC" : -1
},
"name" : "itemA_1_itemB_-1_itemC_-1",
"v" : 0
}
do I have to drop the index and start again ? or is the parameter of index-in-background simply not shown?
@TTT from comments: You want async updating of the index after you inserted, updated or deleted a document?
I think so. I'd like the system's query speed to be maxed at all times, so I definitely need indexes, but there are also times when I'm running tens of thousands of inserts. This becomes a problem for the whole system as Mongo's CPU usage shoots up to 99.9% for the duration of the update.
I think that updating indexes in the background is the answer, but I was reading in the MONGO docs that if an index is being recalculated, it isn't used in querying until the recalc is done.
My ideal "dream situation" is that the system would use the "last best index" until the background update process was complete (or even just always using the current best known index).
Your index system as it is sounds pretty sane. The issue you are seeing looks to be a symptom of the server setup you are using.
EC2 Micro instances are notoriously poor performing when it comes to any sort of sustained operation. Serving web pages is fine, but any prolonged CPU usage will see deteriorating output over time. This is a result of the sustained CPU available being a lot smaller than the burst CPU available for shorter operations.
From Amazon's EC2 page:
Instances of this family provide a small amount of consistent CPU resources and allow you to burst CPU capacity when additional cycles are available. They are well suited for lower throughput applications and web sites that consume significant compute cycles periodically.
And:
Up to 2 EC2 Compute Units (for short periodic bursts)
I'd recommend moving to a setup with more available CPU, such as a small instance, or if cost is one of your primary concerns, a Linode VPS.
精彩评论