简体   繁体   中英

mongodb queries are very slow

I have a multi-threaded app that executes hundreds of transactions per second but after a while the performance drops and the queries are taking too long to execute. I have worked on optimising the connection with the following code to avoid errors, but this resulted in a poor performance.

My queries varies from inserts, delete and multi update; collections do not exceed 100,000 rows.

I have a cluster of 4 VMs for mongoldb each has 4 cores and 28GB of ram on Azure. I built the cluster using 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();

I am not using any indexes and here is my app flow:

  1. am using the db tales for queuing and processing msgs. Each job is saved in a separate collection, with a document for each msg that looks like this (status is 'ready' for the msgs at this stage):
{
"_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. Then I am moving batches from each job according to their priorities from status 'ready' to 'queued' and add them to on-memory queue to be processed:
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. Then another thread sends the actual msgs from the on-memory queue and updates each msg's status separately (sent/failed); I add an index for this msg in a separate table so I can find it once the sender send me back the final status.
  2. Finally I get a final result from the sender about the msg (delivered/undelivered) and I update this msg accordingly, then remove the index from (job_index) collection in the previous step.

========================== UPDATE: ======================

I noticed that I got this error in Java logs:

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)

And here is my Mongodb config:

# 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

Few tips



-In MongoDB profiler did you check the slow running queries.

-Did you try indexing the documents (use inputs from above step)

-Which version of MongoDB are you using and which is the storage engine.

-Have you done replication of the server. If yes, please revisit the write concern parthttps://docs.mongodb.com/manual/core/replica-set-write-concern/

-Can you check whether mongodb in-memory implementation can help https://docs.mongodb.com/manual/core/inmemory/

-You can see few important tips here - https://docs.mongodb.com/manual/administration/analyzing-mongodb-performance/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM