此章节,是作者阅读网上资料,通过结合自身项目的实践。
引用:原文链接:http://iamcaihuafeng.blog.sohu.com/151638529.html
(本文是一篇转载文章,作者在对MongoDB文档进行了细致的阅读后,总结出了MongoDB的各种索引的用法。)
索引能提高检索数据的速度,你可以想像成在MySQL中创建索引一样,同样索引也是用B-Tree也实现的。
创建schedule的collection:
DB db = m.getDB( "ms_basic" );
DBCollection coll = db.getCollection( "schedule" );
//插入数据略过
List< DBObject > indexList = coll.getIndexInfo();  
for  ( DBObject  o : indexList) {  
        System. out .println( "index ---------"  + o);  
}
运行结果:
index ---------
{ "name" : "_id_" , "ns" : "ms_basic.schedule" , "key" : { "_id" : 1}}
此时可以看到,索引为_id,内置的,按照升序排列
System. out .println( "SELECT * FROM schedule WHERE rsc='ATM033101'" );
    startTime = System. currentTimeMillis ();  
     query  =  new  BasicDBObject();
     query .put( "rsc" , "ATM033101" );
    cur = coll.find( query );  
    System. out .println( "查询获得的长度" +cur.count());
System. out .println( "查询耗
:" +(System. currentTimeMillis ()-startTime)+ "毫秒." );
运行结果:
-----------华丽分隔线无rsc索引查询---------------
SELECT * FROM schedule WHERE rsc='ATM033101'
查询获得的长度1
查询耗时:63毫秒.
explain分析:{ "cursor" : "BasicCursor" , "indexBounds" : [ ] , "nscanned" : 4597 , "nscannedObjects" : 4597 , "n" : 1 , "millis" : 0 , "oldPlan" : { "cursor" : "BasicCursor" , "indexBounds" : [ ]} , "allPlans" : [ { "cursor" : "BasicCursor" , "indexBounds" : [ ]}]}
看看官方的解释:
public  DBObject   explain ()
Returns an object containing basic information about the exectution of the query that created this cursor This creates a DBObject with the key/value pairs: "cursor" : cursor type "nScanned" : number of records examined by the database for this query "n" : the number of records that the database returned "millis" : how long it took the database to execute the query
1、单列索引
在字段rsc上创建索引,1 (ascending) or -1 (descending)
MongoDb提供两种接口给我们创建索引:
ensureIndex createIndex
具体可以查查在线的API http://api.mongodb.org/java/2.0/index.html
coll.createIndex(new BasicDBObject("rsc", 1));
运行:
List<DBObject> indexList =  coll .getIndexInfo();  
     for  (DBObject o : indexList) {  
        System. out .println( "index ---------"  + o);  
}
结果为:
index ---------
{ "name" : "_id_" , "ns" : "ms_basic.schedule" , "key" : { "_id" : 1}}
index ---------
{ "name" : "rsc_1" , "ns" : "ms_basic.schedule" , "key" : { "rsc" : 1}}
再次查找: query .put( "rsc" , "ATM033101" );
-----------华丽分隔线 rsc索引查询---------------
SELECT * FROM schedule WHERE rsc='ATM033101'
查询获得的长度1
查询耗时:47毫秒.
explain分析:{ "cursor" : "BtreeCursor rsc_1" , "indexBounds" : [ [ { "rsc" : "ATM033101"} , { "rsc" : "ATM033101"}]] , "nscanned" : 1 , "nscannedObjects" : 1 , "n" : 1 , "millis" : 0 , "oldPlan" : { "cursor" : "BtreeCursor rsc_1" , "indexBounds" : [ [ { "rsc" : "ATM033101"} , { "rsc" : "ATM033101"}]]} , "allPlans" : [ { "cursor" : "BtreeCursor rsc_1" , "indexBounds" : [ [ { "rsc" : "ATM033101"} , { "rsc" : "ATM033101"}]]}]}
此时花费了: 47毫秒
explain分析中,可以看到: "cursor" : "BtreeCursor rsc_1" , "indexBounds" : [ [ { "rsc" : "ATM033101"} ,说明使用了索引来进行查询。
2、默认索引
刚刚看到,一共显示有2个索引,包括自己创建的, 其中_id是创建表的时候自动创建的索引,此索引是不能够删除的。
An index is always created on _id. This index is special and cannot be deleted. The _id index enforces uniqueness for its keys.