MongoDB索引管理

创建索引,查看索引,删除索引,重建索引

Posted by Silence on April 21, 2018

创建索引

mongodb使用 ` createIndex() ` 和 ` ensureIndex() 方法来创建索引,前者用于3.0及以上版本,后者用于3.0以下版本。 语法: db.COLLECTION_NAME.ensureIndex(keys[,options]) `

keys:要建立索引的参数列表。如:{KEY:1},其中key表示字段名,1表示升序排序,也可使用使用数字-1降序。
options:可选参数,表示建立索引的设置。可选值如下:
backgroundBoolean,在后台建立索引,以便建立索引时不阻止其他数据库活动。默认值为false。
uniqueBoolean,创建唯一索引。默认值 false。
nameString,指定索引的名称。如果未指定,MongoDB会生成一个索引字段的名称和排序顺序串联。
partialFilterExpression, document.如果指定,MongoDB只会给满足过滤表达式的记录建立索引.
sparseBoolean,对文档中不存在的字段数据不启用索引。默认值是 false。
expireAfterSeconds,integer,指定索引的过期时间
storageEngine,document,允许用户配置索引的存储引擎

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
```
> 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
- 例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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
- 例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字段还是只有一个索引.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
```
> 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"
    }
]
```