Help Center> Document Database Service> Best Practices> How Do I Improve DDS Performance by Optimizing SQL Statements?
Updated on 2024-01-18 GMT+08:00

How Do I Improve DDS Performance by Optimizing SQL Statements?

DDS is inherently a NoSQL database with high performance and strong extensibility. Similar to relational databases, such as RDS for MySQL, RDS for SQL Server, and Oracle, DDS instance performance may also be affected by database design, statement optimization, and index creation.

The following provides suggestions for improving DDS performance in different dimensions:

Creating Databases and Collections

  1. Use short field names to save storage space. Different from an RDS database, each DDS document has its field names stored in the collection. Short name is recommended.
  2. Limit the number of documents in a collection to avoid the impact on the query performance. Archive documents periodically if necessary.
  3. Each document has a default _id. Do not change the value of this parameter.
  4. Capped collections have a faster insertion speed than other collections and can automatically delete old data. You can create capped collections to improve performance based on your service requirements.

For details, see Usage Suggestions in the Document Database Service Developer Guide.

Query

Indexes

  1. Create proper number of indexes for frequently queried fields based on service requirements. Indexes occupy some storage space, and the insert and indexing operations consume resources. It is recommended that the number of indexes in each collection should not exceed 5.

    If data query is slow due to lack of indexes, create proper indexes for frequently queried fields.

  2. For a query that contains multiple shard keys, create a compound index that contains these keys. The order of shard keys in a compound index is important. A compound index support queries that use the leftmost prefix of the index, and the query is only relevant to the creation sequence of indexes.
  3. TTL indexes can be used to automatically filter out and delete expired documents. The index for creating TTL must be of type date. TTL indexes are single-field indexes.
  4. You can create field indexes in a collection. However, if a large number of documents in the collection do not contain key values, you are advised to create sparse indexes.
  5. When you create text indexes, the field is specified as text instead of 1 or -1. Each collection has only one text index, but it can index multiple fields.

Command usage

  1. The findOne method returns the first document that satisfies the specified query criteria from the collection according to the natural order. To return multiple documents, use this method.
  2. If the query does not require the return of the entire document or is only used to determine whether the key value exists, you can use $project to limit the returned field, reducing the network traffic and the memory usage of the client.
  3. In addition to prefix queries, regular expression queries take longer to execute than using selectors, and indexes are not recommended.
  4. Some operators that contain $ in the query may deteriorate the system performance. The following types of operators are not recommended in services. $or, $nin, $not, $ne, and $exists.
    • $or: The times of queries depend on the number of conditions. It is used to query all the documents that meet the query conditions in the collection. You are advised to use $in instead.
    • $nin: Matches most of indexes, and the full table scan is performed.
    • $not: The query optimizer may fail to match a specific index, and the full table scan is performed.
    • $ne: Selects the documents where the value of the field is not equal to the specified value. The entire document is scanned.
    • $exists: matches each document that contains the field.

    For more information, see official MongoDB documents.

Precautions

  1. Indexes cannot be used in operators $where and $exists.
  2. If the query results need to be sorted, control the number of result sets.
  3. If multiple field indexes are involved, place the field used for exact match before the index.
  4. If the key value sequence in the search criteria is different from that in the compound index, DDS automatically changes the query sequence to the same as index sequence.
    • Modification operation

      Modifying a document by using operators can improve performance. This method does not need to obtain and modify document data back and forth on the server, and takes less time to serialize and transfer data.

    • Batch insert

      Batch insert can reduce the number of times data is submitted to the server and improve the performance. The BSON size of the data submitted in batches cannot exceed 48 MB.

    • Aggregated operation

      During aggregation, $match must be placed before $group to reduce the number of documents to be processed by the $group operator.