1. 程式人生 > >MongoDB索引管理——創建索引,查看索引,刪除索引,重建索引

MongoDB索引管理——創建索引,查看索引,刪除索引,重建索引

大小 exsi ngs 索引 mongodb reat 新建 mon 集合

先給users集合插入兩條記錄,然後用users集合來進行索引管理的演示:

> user1={"name":"liming","age":20,"gender":"F"}
{ "name" : "liming", "age" : 20, "gender" : "F" }
> db.users.insert(user1)
WriteResult({ "nInserted" : 1 })
> user2={"name":"zhangsan","age":25,"gender":"F"}
{ "name" : "zhangsan", "age" : 25, "gender" : "F" }
> db.users.insert(user1)
WriteResult({ "nInserted" : 1 })
> db.users.count()
2

創建索引:

mongodb使用createIndex()和ensureIndex()方法來創建索引,前者用於3.0及以上版本,後者用於3.0以下版本。
例1:給name字段創建正序索引

> db.users.createIndex({"name":1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}

例2:給name字段創建倒序索引

> db.users.createIndex({"name":-1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
}

例3:給name,age字段創建組合索引

> db.users.createIndex({"name":1,"age":1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 3,
    "numIndexesAfter" : 4,
    "ok" : 1
}

例4:在後臺給age字段創建索引

> db.users.createIndex({age:1},{background:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 4,
    "numIndexesAfter" : 5,
    "ok" : 1
}

在後臺創建索引的原因:
在前臺創建索引期間會鎖定數據庫,會導致其它操作無法進行數據讀寫,在後臺創建索引是,會定期釋放寫鎖,從而保證其它操作的運行,但是後臺操作會在耗時更長,尤其是在頻繁進行寫入的服務器上。

查看索引:

MongoDB提供的查看索引信息的方法:
getIndexes()方法可以用來查看集合的所有索引,
getIndexKeys()方法查看索引鍵。
totalIndexSize()查看集合索引的總大小,
getIndexSpecs()方法查看集合各索引的詳細信息
例1: getIndexes()的用法

> db.users.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : -1
        },
        "name" : "name_-1",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1,
            "age" : 1
        },
        "name" : "name_1_age_1",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test1.users",
        "background" : 1
    }
]

例2:getIndexKeys()的用法

> db.users.getIndexKeys()
[
    {
        "_id" : 1
    },
    {
        "name" : 1
    },
    {
        "name" : -1
    },
    {
        "name" : 1,
        "age" : 1
    },
    {
        "age" : 1
    }
]

例3:totalIndexSize()的用法

> db.users.totalIndexSize()
81920

例4:getIndexSpecs()的用法

> db.users.getIndexSpecs()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : -1
        },
        "name" : "name_-1",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1,
            "age" : 1
        },
        "name" : "name_1_age_1",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test1.users",
        "background" : 1
    }
]

刪除索引:

不再需要的索引,我們可以將其刪除,mongodb提供兩種刪除索引的方法:
dropIndex()方法用於刪除指定的索引
dropIndexes()方法用於刪除全部的索引
例1:dropIndex()的用法

> db.users.dropIndex("name_1")
{ "nIndexesWas" : 5, "ok" : 1 }
> db.users.dropIndex("name_1_age_1")
{ "nIndexesWas" : 4, "ok" : 1 }
> db.users.getIndexSpecs()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : -1
        },
        "name" : "name_-1",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "age" : 1
        },
        "name" : "age_1",
        "ns" : "test1.users",
        "background" : 1
    }
]

我們可以看到,name字段的索引和name與age字段的組合索引皆被刪除

例2:dropIndexes()的用法

> db.users.dropIndexes()
{
    "nIndexesWas" : 3,
    "msg" : "non-_id indexes dropped for collection",
    "ok" : 1
}
> db.users.getIndexSpecs()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test1.users"
    }
]

在使用了dropIndexes()方法後,我們之前建的所有索引都被刪除掉了

索引重建:

我們之前把users的索引全部刪除了,現在在name字段上建立一個正序索引,然後在name字段上重建倒序索引,可以看到重建索引是把之前name字段的索引刪掉再新建一個索引的,重建之前name字段還是只有一個索引.

> db.users.createIndex({name:1})
{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}
> db.users.getIndexSpecs()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test1.users"
    }
]
> db.users.reIndex({name:-1})
{
    "nIndexesWas" : 2,
    "nIndexes" : 2,
    "indexes" : [
        {
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "test1.users"
        },
        {
            "key" : {
                "name" : 1
            },
            "name" : "name_1",
            "ns" : "test1.users"
        }
    ],
    "ok" : 1
}
> db.users.getIndexSpecs()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "test1.users"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "name" : "name_1",
        "ns" : "test1.users"
    }
]

MongoDB索引管理——創建索引,查看索引,刪除索引,重建索引