一秒钟一百条数据,mongodb 占用CPU太高
发布于 1 个月前 作者 zsw050536 691 次浏览 来自 问答

问题: 使用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),所有电脑一共开机几次

  1. 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});	
    
  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){}));
    
  2. 机器配置

QQ图片20151201104538.png

4 回复

如果单纯一次性插入,建议先插入完毕后,再加索引

@youyudehexie 单纯的直接插入的话,数据库空间增长过快,所以想改成现在这样的方式

mongodb:3.1.5 mongoose:4.0.6

做这种 log 没必要用mongo 可以考虑 influxdb,或者 CouchDB 之类的,他们可以直接设置 空间大小,根据你的业务推测,我觉得influxdb 适合你

mongodb不适合频繁操作的

回到顶部