mongodb查询非常慢

时间:2016-09-10 07:17:09

标签: mongodb performance azure bitnami

我有一个多线程应用程序,每秒执行数百个事务,但一段时间后性能下降,查询执行时间太长。我已经使用以下代码优化连接以避免错误,但这导致性能不佳。

我的查询因插入,删除和多次更新而异;集合不超过100,000行。

我有一个由mongoldb组成的4个虚拟机,每个虚拟机在Azure上有4个核心和28GB内存。我使用bitnami production(https://azure.microsoft.com/en-us/marketplace/partners/bitnami/production-mongodbdefault/

构建了集群
private static MongoClientOptions options = MongoClientOptions.builder()
.connectionsPerHost(1000)
.threadsAllowedToBlockForConnectionMultiplier(15)
.connectTimeout(60 * 1000)
.maxWaitTime(60 * 1000)
.socketTimeout(60 * 1000)
.connectTimeout(60 * 1000)
.build();

我没有使用任何索引,这是我的应用流程:

  1. 使用db故事排队和处理消息。每个作业都保存在一个单独的集合中,每个msg的文档看起来像这样(状态在此阶段为msgs“准备好”):
  2. {
    "_id": ObjectId("57cd303743ffe80f3728fcf5"),
    "_class": "com.mongodb.BasicDBObject",
    "job_id": "57cd3031d9991f8639487013",
    "priority": 1,
    "title": "1",
    "sender_id": "sender 1",
    "account_id": "57c2d556d9991fbc15897275",
    "schedule_date": ISODate("2016-09-05T08:43:00Z"),
    "utf8": false,
    "content": "text to be sent",
    "number": "962799000001",
    "status": "ready",
    "user_id": "57c2d602d9991fbc1589727b",
    "adv": true,
    "number_of_sms_msgs": 1,
    "uuid": "57cd3031d9991f8639487013_57cd303743ffe80f3728fcf5",
    "msg_id": "1955559517"
    }
    
    1. 然后我根据每个作业从“准备好”状态到“排队”的优先级移动批处理,并将它们添加到内存队列中进行处理:
    2. List<DBObject> batch = scaffoldingRepository.findPageNoSort(dataType, page, next_batch_size, query, null);
      if (batch != null && batch.size() > 0) {    
          BasicDBList ids = new BasicDBList();
          for (final DBObject msg : batch) {
              msg.put("status", "queued");
              msg.put("uuid", job_id + "_" + msg.get("_id"));
              ids.add(new ObjectId(msg.get("_id").toString()));
          }
          BasicDBObject search = new BasicDBObject();
          search.put("_id", new BasicDBObject("$in", ids));
          BasicDBObject update = new BasicDBObject();
          update.put("$set", new BasicDBObject("status", "queued"));
          scaffoldingRepository.updateObjects(search, update, dataType);
      }
      
      1. 然后另一个线程从内存队列发送实际的msgs并分别更新每个msg的状态(发送/失败);我在一个单独的表中添加了这个msg的索引,所以我可以在发件人发回给我最终状态后找到它。
      2. 最后,我从发件人那里得到关于msg(已发送/未发送)的最终结果,并相应地更新此msg,然后在上一步骤中从(job_index)集合中删除索引。
      3. ========================== 的更新: ======================

        我注意到我在Java日志中遇到了这个错误:

        com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
        at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:475)
        at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:226)
        at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:105)
        at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:438)
        at com.mongodb.connection.WriteCommandProtocol.receiveMessage(WriteCommandProtocol.java:262)
        at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:104)
        at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64)
        at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37)
        at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
        at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
        at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:143)
        at com.mongodb.operation.UpdateOperation.executeCommandProtocol(UpdateOperation.java:76)
        at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:142)
        at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:134)
        at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:232)
        at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:223)
        at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:134)
        at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:61)
        at com.mongodb.Mongo.execute(Mongo.java:827)
        at com.mongodb.Mongo$2.execute(Mongo.java:810)
        at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:333)
        at com.mongodb.DBCollection.updateImpl(DBCollection.java:495)
        at com.mongodb.DBCollection.update(DBCollection.java:455)
        at com.mongodb.DBCollection.update(DBCollection.java:432)
        at com.mongodb.DBCollection.update(DBCollection.java:522)
        at com.mongodb.DBCollection.updateMulti(DBCollection.java:552)
        at java.util.TimerThread.mainLoop(Timer.java:555)
        at java.util.TimerThread.run(Timer.java:505)
        

        这是我的Mongodb配置:

        # mongod.conf
        # for documentation of all options, see:
        #   http://docs.mongodb.org/manual/reference/configuration-options/
        
        # Where and how to store data.
        storage:
          dbPath: /opt/bitnami/mongodb/data/db
          journal:
            enabled: true
            #engine:
          mmapv1:
            smallFiles: true
        #  wiredTiger:
        
        # where to write logging data.
        systemLog:
          destination: file
          logAppend: true
          path: /opt/bitnami/mongodb/logs/mongodb.log
        
        # network interfaces
        net:
          port: 27017
          bindIp: 0.0.0.0
          unixDomainSocket:
            enabled: true
            pathPrefix: /opt/bitnami/mongodb/tmp
        
        # replica set options
        replication: 
          replSetName: replicaset
        
        # process management options
        processManagement:
           fork: false
           pidFilePath: /opt/bitnami/mongodb/tmp/mongodb.pid
        
        # set parameter options
        setParameter:
           enableLocalhostAuthBypass: true
        
        # security options
        security:
          authorization: disabled
          #keyFile: replace_me
        
        #profiling
        #operationProfiling:
          #slowOpThresholdMs: 100
          #mode: slowOp
        

1 个答案:

答案 0 :(得分:1)

几点提示


- 在MongoDB分析器中,您检查了慢速运行的查询。

- 您尝试索引文档(使用上面的输入)

- 你使用的是哪个版本的MongoDB,哪个是存储引擎。

- 你完成了服务器的复制。如果是,请重新审核写作关注部分https://docs.mongodb.com/manual/core/replica-set-write-concern/

- 你可以检查mongodb内存中的实现是否可以帮助https://docs.mongodb.com/manual/core/inmemory/

- 你可以在这里看到一些重要的提示 - https://docs.mongodb.com/manual/administration/analyzing-mongodb-performance/