Improving DDS Performance
DDS is inherently a NoSQL database with high performance and strong extensibility. Similar as RDS, such as RDS for MySQL, Microsoft SQL Server, and Oracle, DDS 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
- Use short field names to save storage space. Different from an RDS database, each DDS document has its field names stored in the collection. Therefore, short name has its advantages.
- Limit the number of documents in a collection to avoid the impact on the query performance. Archive documents periodically if necessary.
- Each document provides a default value of _id. Do not set this parameter to a user-defined value.
- Compared with other collections, capped collections have a faster insertion speed and can automatically delete old data. You can create a capped collection to improve performance based on service requirements.
- Query Operation
- Create proper number of indexes for frequently queried data fields based on service requirements. Indexes occupy some storage space, and inserts and updates consume resources. Therefore, it is recommended that the number of indexes in each collection should not exceed 5.
Case: Data query is slow. If no index is created, you are advised to create proper indexes for frequently queried data fields to improve the query speed.
- For a query that contains multiple shard keys, you are advised to 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.
- TTL indexes can be used to automatically filter out expired documents and delete them. The index for creating TTL must be of type date. TTL indexes are single-field indexes.
- You can create field indexes in a collection. However, if a large number of documents in the collection do not contain the key values, you are advised to create sparse indexes.
- 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 any fields.
- The findOne method returns the first document that satisfies the specified query criteria on the collection according to the natural order. To return multiple documents, use this method.
- 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.
- In addition to prefix queries, regular expression queries take longer to execute than using selectors, and indexes are not recommended.
- 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 query vary depending 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.
- If the query results need to be sorted, control the number of result sets.
- Indexes cannot be used in operators $where and $exists.
- If multiple field indexes are involved, place the field used for exact match before the index.
- 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.
- Create proper number of indexes for frequently queried data fields based on service requirements. Indexes occupy some storage space, and inserts and updates consume resources. Therefore, it is recommended that the number of indexes in each collection should not exceed 5.
- Modification Operation
Modify 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.
Last Article: Enabling Sharding on a Database
Next Article: Avoiding mongos Cache Problem
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.