一秒钟一百条数据,mongodb 占用CPU太高
问题:
使用nodejs+mongodb处理数据,大约一秒钟一百条数据的样子,mongodb占用cpu太高。
需求:
统计pc客户端软件的使用次数,提交数据格式如下:
http://127.0.0.1:8080/union?userName=ceshi&appName=yewu01&appId=2&wip=15.15.15.15&mac=FF-FF-FF-FF&nip=1.1.1.1&ver=20151120&time=xxxxx
需要统计每天 ceshi(userName) 这个用户有几个外网ip(wip), 几台电脑(mac),所有电脑一共开机几次
- model如下:
- user:
var UserSchema = new Schema({ name: { type: String}, pass:{type:String}, desInfo:{type: String}, create_at: { type: Date, default: Date.now }, update_at: { type: Date, default: Date.now } }); UserSchema.index({name: 1}, {unique: true});
- wip
var WipSchema = new Schema({ userId: {type: ObjectId}, appId:{type: Number, default:-1}, appName: { type: String}, wipStr: { type: String}, create_at: { type: Date, default: Date.now }, update_at: { type: Date, default: Date.now } }); WipSchema.index({wipStr: 1, update_at:-1});
- mac
var MacSchema = new Schema({ userId: {type: ObjectId}, appId:{type: Number, default:-1}, appName: { type: String}, macStr: { type: String}, ipStr:{type:String}, verStr:{type: String}, create_at: { type: Date, default: Date.now }, update_at: { type: Date, default: Date.now } }); MacSchema.index({appId: 1, appName:1, macStr:1, update_at:-1});
- union
var UnionSchema = new Schema({ userId: {type: ObjectId}, appId:{type: Number, default:-1}, appName: { type: String}, wipNum :{type: Number, default:0}, macNum:{type: Number, default:0}, opeNum:{type: Number, default:0}, create_at: { type: Date, default: Date.now } }); UnionSchema.index({appId: 1, appName:1, create_at:-1});
- 代码如下
var userUnion = {userName:'', user:null, appId:-1, appName:'', wip:'', wip:null, isWip:false, mac:'', nip:'', ver:'', mac:null, isMac:false, union:null, dateNow:tools.formatDate(new Date(), "YYYY-MM-DD")}; userUnion.userName = req.query.userName?req.query.userName:''; userUnion.appId = req.query.appId?req.query.appId:-1; userUnion.appName = req.query.appName?req.query.appName:''; userUnion.wip = req.query.wip?req.query.wip:''; userUnion.mac = req.query.mac?req.query.mac:''; userUnion.nip = req.query.nip?req.query.nip:''; userUnion.ver = req.query.ver?req.query.ver:''; var proxyLine = new eventproxy(); proxyLine.assign("user", function(_user){ userUnion.user = _user; var render = function(wip, mac, union){ if(userUnion.isWip){ union.wipNum += 1; } if(userUnion.isMac){ union.macNum += 1; } union.opeNum += 1; union.save(); return res.end("success"); } //获取wip,mac,union 可以同步 var proxy = eventproxy.create('wip', 'mac', 'union', render); proxy.fail(function(){ return res.end("error"); }); WipApi.getWipByStr( userUnion.user._id, userUnion.appId, userUnion.appName, userUnion.wip, proxy.done(function(data){})); MacApi.getMacByStr(userUnion.user._id, userUnion.appId, userUnion.appName,userUnion.mac, proxy.done(function(data){})); UnionApi.getUnionById(userUnion.user._id, userUnion.appName, userUnion.dateNow,proxy.done(function(data){})); }); UserApi.getUserByName( userUnion.userName, function(userErr,userData){}));
- 机器配置
4 回复