Slow Log

Last updated: 2020-02-25 20:33:55

PDF

If you find the request latency is longer than expected when using MongoDB, you can troubleshoot the problem following the steps below.

  1. Check if the monitoring metric Latency for instances is exceptional.
    Check the instance monitoring data. The metric Latency mainly reflects the time from a request arriving at the access layer to it returning to the client after processed. If the request latency is high, check if there is a slow operation log on mongod.

    To view the slow operation log, see the following steps.

  2. Check if there is a slow operation log on mongod.
    Log in to the Console of TencentDB for MongoDB and view the slow operation log of the instance by using "Query statistics".
    Pay attention to keywords such as command, COLLSCAN, IXSCAN, keysExamined and docsExamined. For more log descriptions, see MongoDB official website.
    Keywords are described below.

    • Command indicates the operations recorded in a slow operation log.
    • COLLSCAN indicates a full table scan is performed. IXSCAN indicates an index scan is performed. For more information on field descriptions, see MongoDB official website.
    • keysExamined the number of index entries scanned. docsExamined indicates the number of documents scanned. Larger keysExamined and docsExamined values mean that no index is created or the index is less distinctive. Please confirm the fields for which an index is created.
  1. Confirm whether the request is locked due to an index created at the foreground.
    If there is no problem with the index used for business queries, check if an index is created at the foreground during peak business hours. An index is created at the foreground by default for a collection (the Background option is false), which will block all the other operations until the index is created at the foreground. If you select to create an index at the background, MongoDB can still provide read and write services during the creation of the index. However, it takes longer time to create an index in this way. For options to create an index, see MongoDB official website.

    You can view the progress of index creation using the currentOp command, as shown below:
db.currentOp(
    {
      $or: [
        { op: "command", "query.createIndexes": { $exists: true } },
        { op: "insert", ns: /\.system\.indexes\b/ }
      ]
    }
    )

The returned result is shown as follows. The msg field indicates the progress of index creation. The locks field indicates the lock type of the operation. For more information on locks, see MongoDB official website.



  1. Check if the mongos load is too high.
    The metric Latency mainly reflects the time from a request arriving at the access layer to it returning to the client after processed. If there is no slow operation log on mongod, but the request latency is high, it may result from high mongos load. There are many reasons for this, such as a large number of connections are established in a short time, or data in multiple shards needs to be summarized. In these cases, you can restart mongos on the console.

    All instance connections will be interrupted at the moment of restarting mongos, but the business can be directly reconnected. So restarting mongos will not continuously affect the business.