温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB数据量较大时如何构建索引--减少业务最少影响

发布时间:2020-07-14 15:42:06 来源:网络 阅读:4746 作者:flysize 栏目:MongoDB数据库

    在数据量较大或请求量较大,直接建立索引对性能有显著影响时,可以利用复制集(数据量较大时一般为线上环境,使用复制集为必然选择或者使用分片.)中部分机器宕机不影响复制集工作的特性,继而建立索引。

备注:添加索引的表使用WT引擎,数据量有1.5亿左右。


1. 副本集配置参数

节点1:

$ more shard1.conf 

dbpath=/data/users/mgousr01/mongodb/dbdata/shard1_1

logpath=/data/users/mgousr01/mongodb/logs/shard1_1.log

pidfilepath=/data/users/mgousr01/mongodb/dbdata/shard1_1/shard1-1.pid

directoryperdb=true

logappend=true

replSet=shard1

shardsvr=true 

bind_ip=127.0.0.1,x.x.x.x

port=37017

oplogSize=9024

fork=true

#noprealloc=true

#auth=true

journal=true

profile=1

slowms=10

maxConns=12000

storageEngine = wiredTiger

wiredTigerCacheSizeGB=96

#clusterAuthMode=keyFile

keyFile=/data/users/mgousr01/mongodb/etc/keyFilers0.key

wiredTigerDirectoryForIndexes=on

wiredTigerCollectionBlockCompressor=zlib

wiredTigerJournalCompressor=zlib


节点2:

$ more shard2.conf 

dbpath=/data/users/mgousr01/mongodb/dbdata/shard2_1

logpath=/data/users/mgousr01/mongodb/logs/shard2_1.log

pidfilepath=/data/users/mgousr01/mongodb/dbdata/shard2_1/shard2-1.pid

directoryperdb=true

logappend=true

replSet=shard1

shardsvr=true 

bind_ip=127.0.0.1,x.x.x.x

port=37017

oplogSize=9024

fork=true

#noprealloc=true

#auth=true

journal=true

profile=1

slowms=10

maxConns=12000

storageEngine = wiredTiger

wiredTigerCacheSizeGB=96

#clusterAuthMode=keyFile

keyFile=/data/users/mgousr01/mongodb/etc/keyFilers0.key

wiredTigerDirectoryForIndexes=on

wiredTigerCollectionBlockCompressor=zlib

wiredTigerJournalCompressor=zlib


节点3:

[mgousr01@pre-mongo-main-01 etc]$ more shard3.conf 

dbpath=/data/users/mgousr01/mongodb/dbdata/shard3_1

logpath=/data/users/mgousr01/mongodb/logs/shard3_1.log

pidfilepath=/data/users/mgousr01/mongodb/dbdata/shard3_1/shard3-1.pid

directoryperdb=true

logappend=true

replSet=shard1

shardsvr=true 

bind_ip=127.0.0.1,x.x.x.x

port=37017

oplogSize=9024

fork=true

#noprealloc=true

#auth=true

journal=true

profile=1

slowms=10

maxConns=12000

storageEngine = wiredTiger

wiredTigerCacheSizeGB=96

#clusterAuthMode=keyFile

keyFile=/data/users/mgousr01/mongodb/etc/keyFilers0.key

wiredTigerDirectoryForIndexes=on

wiredTigerCollectionBlockCompressor=zlib

wiredTigerJournalCompressor=zlib


2. 启动mongodb

mongod -f <参数名称>  启动


3. 配置副本集命令(登陆任意一台主机)

config={_id:'shard1',members:[{_id:0,host:'x.x.x.x:37017',priority:1,tags:{'use':'xxx'}},{_id:1,host:'x.x.x.x:37017',priority:1,tags:{'use':'xxx'}},{_id:2,host:'x.x.x.x:37017',priority:1,tags:{'use':'xxx'}}]}

rs.initiate(config)


4. 在primary库进行写操作  模拟线上

for(i=0;i<100000;i++){ db.users.insert( { "username":"user"+i, "age":Math.floor(Math.random()*120), "created":new Date() })}

比如,用户信息增长比较快,超过了1亿多数据量等状况……


5. 数据量较大时创建索引的通用方法说明

   创建索引不能再secondary节点创建,只能在主上创建索引

为了尽量降低建立索引对 MongoDB Server 的影响,有一种方法是把 MongoDB Server 转换成standalone模式后建立。具体做法如下:

(1)首先把 secondary server 停止,在取消 --replSet 参数,并且更改 MongoDB port 之后重新启动 MongoDB,这时候 MongoDB 将进入 standalone 模式;

(2).在 standalone 模式下运行命令 ensureIndex 建立索引,使用 foreground 方式运行也可以,建议使用background方式运行;

(3)建立索引完毕之后关闭 secondary server 按正常方式启动;

4.根据上述 1~3 的步骤轮流为 secondary 建立索引,最后把 primary server 临时转换为 secondary server,同样按 1~3 的方法建立索引,再把其转换为 primary server。

这种方式还是比较麻烦的,但可以把建立索引操作对 MongoDB 的影响降到最低,在有些情况下还是值得做的。


6. 具体做法

(1)停其中一台Secondary节点

   上述副本集是三节点:节点1是primary节点,节点2和节点3是secondary节点。

以节点3第二个secondary为例操作:

$ pwd

/data/users/mgousr01/mongodb/etc

$ mongod -f shard3.conf --shutdown   关闭mongod进程

(2)将shard3.conf配置文件的replSet=shard1注释掉

$ vim shard3.conf 

……

#replSet=shard1

……

(3)然后启动

mongod -f shard3.conf

(4)登陆进去创建索引

mongo x.x.x.x:37017/admin

> use chicago

Build the Index

> db.users.createIndex({username:1,created:1},{unique:true},{name:"username_created_unique"},{background:true}) 

【建议以后创建索引 要有命名规范】

(5)查看索引信息

> db.users.getIndexes()  

[

{

"v" : 1,

"key" : {

"_id" : 1

},

"name" : "_id_",

"ns" : "chicago.users"

},

{

"v" : 1,

"unique" : true,

"key" : {

"username" : 1,

"created" : 1

},

"name" : "username_1_created_1",

"ns" : "chicago.users"

}

]


(6)再次停掉副本集的mongod进程

$ pwd

/data/users/mgousr01/mongodb/etc

$ mongod -f shard3.conf --shutdown

(7)启动mongod进程

将shard3.conf配置文件的replSet=shard1注释去掉;然后启动

mongod -f shard3.conf

mongo ip:37017/admin


启动后,会将节点加入到副本集中,然后同步primary数据,secondary上的索引不会对主造成影响导致主从不一致状况发生。



7.对第二个secondary副本操作--节点2

重复第6步即可。


8.构建所有secondarys索引大致步骤

For each secondary in the set, build an index according to the following steps:

(1)Stop One Secondary

(2)Build the Index

(3)Restart the Program mongod


9.构建primary节点索引

(1)登陆到主节点

mongo ip:37017/admin

(2) 将主节点降级

shard1:PRIMARY> rs.stepDown(30)

2016-04-19T12:49:44.423+0800 I NETWORK  DBClientCursor::init call() failed

2016-04-19T12:49:44.426+0800 E QUERY    Error: error doing query: failed

    at DBQuery._exec (src/mongo/shell/query.js:83:36)

    at DBQuery.hasNext (src/mongo/shell/query.js:240:10)

    at DBCollection.findOne (src/mongo/shell/collection.js:187:19)

    at DB.runCommand (src/mongo/shell/db.js:58:41)

    at DB.adminCommand (src/mongo/shell/db.js:66:41)

    at Function.rs.stepDown (src/mongo/shell/utils.js:1006:15)

    at (shell):1:4 at src/mongo/shell/query.js:83

2016-04-19T12:49:44.427+0800 I NETWORK  trying reconnect to xxxx failed

2016-04-19T12:49:44.428+0800 I NETWORK  reconnect xxxx ok

shard1:SECONDARY> 

降级命令执行后,会主动变成secondary节点。上述两个secondary节点会有一个节点选举成为primary节点。

(3)后续构建索引的方法和第6步一样。


说明:

让primary降级:rs.stepDown(downseconds=60),primary降级期间,它不会参与选举,如果降级时限过后,副本集还是没有primary,它会参与选举。

Preventing Electoins

让secondaries保持状态:rs.freeze(seconds),这样就可以在这个时间内对primary做处理,而不用担心除非选举。

解除状态锁定:rs.freeze(0)。





      


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI