Document Database Service

Specifications on Developing DDS

2025-07-28 07:13:01

Database Connection

When using DDS for application development, you need to comply with some database connection specifications, to ensure the security and reliability of database connections. The following are some commonly used DDS connection specifications:

  • Connect using URI: DDS supports connections using connection strings in Uniform Resource Identifier (URI) format, which simplifies the setting of connection parameters, for example:


mongodb://username:password@hostname:port/database

Where, username and password are the username and password for logging into the database, hostname and port are the address and port number of the server that serves the document database, and database is the name of the database to which you want to connect.

  • Use connection pooling: To improve the efficiency and performance of connections, connection pooling technology can be used to manage DDS connections. Connection pooling can cache connection objects and reuse them when needed, avoiding frequent creation and destruction of connection objects. When the client connects to the database, it is necessary to calculate the total number of clients in the business, and the size of the connection pool configured for each client. The total number of connections should not exceed 80% of the maximum number of connections that the current instance can handle.

  • Do not use expired connections: The connection object of DDS has a certain lifecycle, and the connection should be closed in time after use to avoid consuming too much system resources. Also, don't use the expired connection object, you should create a new one for operation.

  • Set appropriate timeout: When connecting to DDS, an appropriate timeout should be set to avoid exceptions or timeouts during the connection process that may cause application problems. You can set different types of timeouts such as connection timeout, read timeout, and write timeout.

  • Avoid using too many connections: In applications, it is important to avoid using too many DDS connections to avoid consuming too much system resources. You can optimize the use of connections by using connection pooling, and setting connection timeouts.

  • Encrypt using SSL/TLS: To improve the security of database connections, encryption protocols such as SSL/TLS can be used to encrypt and transmit DDS connections to prevent data from being stolen or tampered with.

Reliability

When using DDS, you need to comply with some standards and best practices, to ensure the reliability and consistency of data. Here are some common DDS reliability specifications:

  • Use replica set: The replica set of DDS is a set of automatically synchronized document database instances that provide data redundancy and high availability. Replica sets can be used to ensure data reliability and recoverability.

  • Set write concern: The write concern of DDS specifies the Ack level of the write operation, that is, how many DDS instances are required to acknowledge the data before it is considered successful. For critical services, write concern is set to {w:n}, n>0, the higher the number, the better the consistency is, but the performance is poor. You can set different write concern levels based on service requirements, for example:

    •  w: 0 indicates that the write operation does not require Ack and does not wait for a response from the DDS instance.

    • w: 1 indicates that the write operation requires Ack from at least one DDS instance and waits for response from the DDS instance;

    • w: majority indicates that the write operation requires Ack from most DDS instances and waits for responses from most DDS instances;

    • w: n indicates that the write operation requires Ack from n DDS instances and waits for responses from n DDS instances.

  • Use transaction: DDS 4.0 or later supports transaction, which can be used to ensure the data consistency and reliability. Transactions can be used to ensure the atomicity of multiple operations, such as writing multiple documents or modifying multiple documents at the same time.

  • Use correct index: Proper use of indexes can improve the efficiency and performance of DDS queries and reduce the risk of data errors and loss.

  • Backup and restore data: Regularly backing up DDS data can ensure the data reliability and recoverability, you can use mongodump and mongorestore commands to back up and restore the data.

Performance Related

Specification

When using DDS, you need to comply with some performance related specifications and best practices, to improve the performance and efficiency of data. Here are some common DDS performance related specifications:

  • Proper index design: Proper index design can improve the query performance and efficiency of DDS, you can select the appropriate index types and fields based on the query requirements of your application.

  • Use right data model: DDS is a type of document database that can use embedded documents or referenced documents to store data. Using the right data model can improve query efficiency and speed of data access.

  • Avoid full table scanning: When performing queries, DDS should avoid scanning full tables and use indexes to speed up queries. You can use the explain command to analyze query performance and index usage.

  • Distributed extension: DDS supports distributed extension, and the sharding technology can be used to distribute data across multiple DDS instances to improve system throughput and performance.

  • Use right data type: DDS supports multiple data types, and the right data type should be selected based on the requirements of application. For example, you can use binary data type to store large files and arrays to store data with the same properties.

  • Use right query statement: When querying the DDS database, you should use right query statements and operators and avoid using unnecessary query conditions and operators, to improve query efficiency and performance.

  • During development, each operation on the collection should be checked by executing explain(), for example:

db.students.find({age: 20}, {name: 1, _id: 0}).explain();
  • Reasonable system configuration: To improve the performance and efficiency of DDS, you must configure the system parameters and resource limits, such as memory, disk, and CPU.

View execution plan

In DDS, you can use the explain() method to view the query execution plan and understand the performance and index usage of the query statement. After executing the explain() method, DDS will return a document containing details of the query execution.

Here are the steps to view the execution plan in DDS:

1.Execute query statement: First, you need to execute the query statement to view the execution plan, for example:

db.collection.find({field: "value"})

2.Execute explain() method: Add the explain() method to the end of the query statement, for example:

db.collection.find({field: "value"}).explain()

3.View execution plan. After executing the explain() method, DDS will return a document containing details of the query execution. You can view the queryPlanner, executionStats, and serverInfo fields in the document to learn about the execution plan, query performance, and index usage. Example:

{
    "queryPlanner" : {...},
    "executionStats" : {...},
    "serverInfo" : {...}
}

In execution plan document, common fields and properties include:

  • queryPlanner: Query planner, which represents how DDS executes queries.

  • executionStats: Execution statistics, which represent the execution and performance of queries.

  • winningPlan: Optimal query plan, which represents the optimal query plan selected by DDS.

  • rejectedPlans: Rejected query plan, which represents the query plan rejected by DDS.

  • totalKeysExamined: Number of index scans, which represents the number of indexes scanned during the query.

  • totalDocsExamined: Number of document scans, which represents the number of documents scanned during the query.

  • executionTimeMillis: Query execution time, which represents the time required for the query to complete.

Execution plan analysis:

  • Check the execution time: executionStats.executionStages.executionTimeMillisEstimate and executionStats.executionStages.inputStage. The shorter the executionTimeMillisEstimate, the better.

    •  executionStats.executionTimeMillis represents all the time that the execution plan is selected and executed.

    • executionStats.executionStages.executionTimeMillisEstimate represents the execution completion time of the optimal execution plan.

    • executionStats.executionStages.inputStage. executionTimeMillisEstimate represents the completion time of sub stage execution under the optimal execution plan.

  • Check the number of scanned entries: three entries with the same number is best.

    • executionStats. nReturned represents the number of documents that match the query condition.

    • executionStats .totalKeysExamined represents the number of index scan entries.

    • executionStats .totalDocsExamined represents the number of scanned document entries.

  • Check the stage status, the combinations of stage status with better performance are as follows.

    • Fetch+IDHACK

    • Fetch+ixscan

    • Limit+(Fetch+ixscan)

    • PROJECTION+ixscan

 Status Description:

 Status Name

 Status Name

 COLLSCAN

 Full table scanning

 SORT

 Sort in memory

 IDHACK

 Query based on _id

 TEXT

 Full text indexing

 FETCH

 Index scanning

 LIMIT

 Use Limit to limit the number of returns

 SUBPLA

 Unindexed $or query stage

 PROJECTION

 Unindexed $or query stage

 COUNT_SCAN

 Use index counting

Cursor Rules of Use

In DDS, the result returned by query operation is a Cursor, which can be used to traverse the query result set and perform some operations through the Cursor object. The following are some common rules for using DDS cursor:

  • Explicitly close cursor: When using a cursor to query the DDS database, the cursor object should be explicitly closed to free up the resources occupied by the cursor. You can use the close() method to close the cursor object, for example:

cursor = db.collection.find()
# Traverse cursor
for doc in cursor:
    print(doc)
# Close cursor
cursor.close()
  • Use iterator to traverse cursor: Cursor object iterators can be used to traverse cursors, avoiding loading all query results at once and consuming too much system resources. Example:

cursor = db.collection.find()
# Use iterator to traverse cursor
for doc in cursor:
    print(doc)
  • Limit query results: The limit() method can be used to limit the number of query results and avoid consuming too much system resources due to a large query result set. Example:

cursor = db.collection.find().limit(10)
# Traverse cursor
for doc in cursor:
    print(doc)
  • Skip query results: The skip() method can be used to skip the first few records of the query result in order to query the subsequent records. Example:

cursor = db.collection.find().skip(10)
# Traverse cursor
for doc in cursor:
    print(doc)
  • Sort query results: The sort() method can be used to sort the query results so that they are returned in the specified order. Example:

cursor = db.collection.find().sort("field", 1)
# Traverse cursor
for doc in cursor:
    print(doc)

In summary, when using DDS cursor, it is necessary to follow some rules and best practices to improve query performance and efficiency, and avoid consuming too much system resources.


oM8raLrwnhMz