In DDS, indexes can be used to speed up query operations. Index is a data structure that sorts specified fields in a collection and creates a quick find for those fields. DDS supports multiple types of indexes, such as single-field indexes, multi-field joint indexes, full-text indexes.
Index Classification
Single-field index: Single-field index is the most common type of index, which sorts and finds based on a single field in the collection.
Composite index: Composite index is an index type that sorts and finds based on multiple fields. A composite index can contain multiple fields, and the sorting method of the index keys can be specified based on the order of these fields.
Multi-key index: In DDS, a document can contain multiple arrays or nested documents with the same values, which can be treated as multiple keys. Multi-key index can sort and find documents based on these multiple keys. Here's an example:
Suppose there is a collection books that contains information about each book, and one of the tags fields is an array containing multiple tags. Example:
{
"_id" : ObjectId("6146c03f6c9d8f5fb9da0e3c"),
"title" : "The Great Gatsby",
"author" : "F. Scott Fitzgerald",
"tags" : [ "Fiction", "Classic", "Romance" ]
}To speed up query operations on the tags field, you can create a multi-key index on the tags field. Example:
db.books.createIndex({ tags: 1 })This will create an ascending multi-key index on the tags field. Now, you can use the following query to find books that contain specific tags:
db.books.find({ tags: "Fiction" })This query will use the multi-key index on the tags field to speed up the query and return all books that contain the "Fiction" tag. It is important to note that the efficiency of a multi-key index can be affected by the number and type of values in the array field. If the array field contains a large number of values or nested documents, a multi-key index may not be the best choice. When using multi-key index, it is necessary to evaluate its effect and performance according to the actual situation.
Geospatial index: Geospatial index is a type of index optimized for geospatial data, which can accelerate the query and aggregation operations for geospatial data. Geospatial index can be created using the createIndex() command.
Text index: Text index is a type of index that is optimized for text data to speed up full-text retrieval operations. Text index can only be used for string field. Text index can be created using the createIndex() command.
Hash index: Hash index is an index type based on hash function, which can speed up the lookup operation of hash key, such as password. Hash index can be created using the createIndex() command.
TTL index: TTL index is a timestamp-based index type, which can automatically delete documents that meet certain time conditions. TTL index can be created using the createIndex() command.
Index Name
The default name of the index is a connection between the index key and the direction (i.e., 1 or -1) of each key in the index, using underscore as the delimiter. For example, the index name created on { item : 1, quantity: -1 } is item1_quantity-1.
You can create an index with a custom name, such as one that is easier to read than the default name. For example, consider an application that frequently queries the products collection to populate existing inventory data. The following createIndex() method creates an index on the item and quantity named in the query.
db.products.createIndex( { item: 1, quantity: -1 } , { name: "query for inventory" })You can use the db.collection.getIndexes () method to view the index name. Once an index is created, you cannot rename it. Instead, you must delete and recreate the index with a new name.
DDS provides many different index types to support specific types of data and queries.
Create Index
In DDS, you can use the createIndex() method to create an index. Here are the general steps for creating an index:
A database connected to DDS.
Select the collection for which you want to create an index.
Use the createIndex() method to create an index. The createIndex() method takes two parameters:
Index Keys: A document containing one or more fields that specify which fields to create an index on. Index keys can be specified using object literals or string representations.
Options: A document containing one or more options that specify the properties and behavior of an index. The options object can be omitted and default values can be used.
Here are some commonly used optional parameters:
background: Specifies whether to run the index in the background when it is created. If set to true, the process of creating the index will run in the background and won't block other operations. The default value is false.
unique: Specifies whether the index is unique. If set to true, the index key value must be unique. The default value is false.
sparse: Specifies whether the index is sparse. If set to true, only documents that contain the index key will be indexed. The default value is false.
name: Specifies the name of the index. If no name is specified, DDS will automatically generate a unique index name.
expireAfterSeconds: For TTL index, specifies how long after the document should expire (in seconds).
partialFilterExpression: Specifies a filter expression that will be applied to a subset of the indexed documents. Only documents that satisfy the expression are included in the index.
collation: Specifies the collation that the index should use. You can use collations to control how case, accent, and specific characters are sorted.
storageEngine: Specifies the storage engine that the index should use. You can use the storage engine to control how indexes are stored and their performance
For example, here is an example of creating an ascending index for the username field in the users collection:
db.users.createIndex({ username: 1 })To create a composite index for the username and email fields in the users collection, you can use the following code:
db.users.createIndex({ username: 1, email: 1 })It should be noted that creating an index may take some time, depending on the size of the collection and the complexity of the index. When creating an index, the explain() method can be used to view query plans and performance statistics, in order to evaluate the effectiveness and performance of the index.