Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo Updates being super slow

We are facing a timeout issue with our mongo updates. Our collection currently contains around 300 thousand documents. When we try to update a record via the UI, the server times out and the UI is stuck in limbo.

Lead.updateOne({
      _id: body.CandidateID
    }, {
      $set: {
        ingestionStatus: 'SUBMITTED',
        program: body.program,
        participant: body.participant,
        promotion: body.promotion,
        addressMeta: body.addressMeta,
        CreatedByID: body.CreatedByID,
        entryPerson: body.entryPerson,
        lastEnteredOn: body.lastEnteredOn,
        zipcode: body.zipcode,
        state: body.state,
        readableAddress: body.readableAddress,
        promotionId: body.promotionId,
        programId: body.programId,
        phone1: body.phone1,
        personId: body.personId,
        lastName: body.lastName,
        hasSignature: body.hasSignature,
        firstName: body.firstName,
        city: body.city,
        email: body.email,
        addressVerified: body.addressVerified,
        address: body.address,
        accountId: body.accountId
      }

This is how we update a single record. We are using mlab and Heroku in our stack. Looking for advice on how to speed this up considerably.

Thank you.

like image 360
aliirz Avatar asked Feb 25 '19 19:02

aliirz


People also ask

Why MongoDB is so slow?

NoSQL databases like MongoDB are often structured without a schema to make writes convenient, and it's a key part what also makes them so unique and popular. However, the lack of a schema can dramatically slows down reads, causing problems with query performance as your application scales.

How long does MongoDB take to update?

Mongodb Update opearation takes long time to insert new array filed in the existing collection documents. Save this question. Show activity on this post. Above update takes around 2 minutes to update the entire collection.Is there any other way to optimize this update query.

Is MongoDB update deprecated?

Like remove() , the update() function is deprecated in favor of the more explicit updateOne() , updateMany() , and replaceOne() functions. You should replace update() with updateOne() , unless you use the multi or overwrite options.

How does updates work in MongoDB?

MongoDB Update method is used to update the document from the collection, and the update method will update the value of the existing document. We have used a $set operator at the time of updating the document. We can update a single document by using an update or updateOne method.


3 Answers

If your indexes are fine then you could try rebuilding indexes on this collection. collection indexes from the mango command line: For example, rebuild the lead collection indexes from the mongo command line:

db.lead.reIndex();

Reference:

https://docs.mongodb.com/v3.2/tutorial/manage-indexes/ https://docs.mongodb.com/manual/reference/command/repairDatabase/

like image 119
Thamaraiselvam Avatar answered Oct 09 '22 05:10

Thamaraiselvam


if you are not using this then try this one Index builds can block write operations on your database, so you don’t want to build indexes in the foreground on large tables during peak usage. You can use the background creation of indexes by specifying background: true when creating.

db.collection.createIndex({ a:1 }, { background: true })

This will ultimately take longer to complete, but it will not block operations and will have less of an impact on performance.

like image 24
Mohsin Jillani Avatar answered Oct 09 '22 03:10

Mohsin Jillani


1) Shard Lead collection by id as shard key. 2) Check if the memory taken by mongodb due to index is less than the memory of the mongoDb server.

like image 41
Rahul Kumar Avatar answered Oct 09 '22 03:10

Rahul Kumar