Explore the essentials of secondary indexing in CouchDB with this quiz, designed to evaluate your understanding of creating, querying, and optimizing views for efficient data retrieval. Strengthen your grasp of document-based database indexing concepts and best practices through scenario-based questions.
What is the primary purpose of creating secondary indexes in CouchDB for your document database?
Explanation: Secondary indexes are designed to allow quick queries on document fields other than the document’s unique identifier. They do not affect storage size significantly, nor do they replace primary keys. Automatic backup is unrelated to indexing, which is specifically about data retrieval speed.
How is a secondary index typically defined in CouchDB when you want to search documents by a 'status' field?
Explanation: A map function in a design document emits key-value pairs for indexing, allowing secondary queries, such as searching by 'status'. Duplicating fields into the document ID or renaming the database does not create a secondary index. Indexing only the primary key cannot support searches on other fields.
Which type of index is commonly used in CouchDB to facilitate efficient searches based on custom document fields?
Explanation: A view index is built from map and reduce functions, making searches based on custom document properties efficient. Hash index and key lock are terms from other database systems or have different uses, and a table scan does not involve any indexing, leading to slower queries.
What happens to a CouchDB secondary index when documents are added, updated, or deleted?
Explanation: CouchDB automatically updates its secondary indexes as documents change, ensuring the indexes remain consistent. Manual deletion or recreation is not needed, and waiting for a restart or requiring a primary key designation is not part of the indexing mechanism.
In a CouchDB secondary index, what does the map function typically emit when creating a view to index a document's 'type' field?
Explanation: The correct pattern is to emit the field value as the key and often null for the value unless additional data is needed. Emitting null as the key, reversing arguments, or using undefined variables does not create the intended index.
If you want to retrieve all documents where the 'category' field equals 'books', how would a secondary index help in CouchDB?
Explanation: Secondary indexes provide efficient querying capabilities by avoiding full scans for filtering by indexed fields. They do not force inefficient searching, encrypt fields, or limit data retrieval based on indexed fields.
After modifying the map function in a CouchDB design document, what must occur for the secondary index (view) to reflect those changes?
Explanation: Views are rebuilt upon the next query after the map function is changed, automatically updating the index. Deleting the database or reverting the map function is unnecessary, and reassigning primary keys is unrelated to index rebuilding.
Why would you emit a composite key, such as [doc.author, doc.date], in a CouchDB secondary index view?
Explanation: Using composite keys enables multi-dimensional queries, such as retrieving documents for a specific author within a date range. This is not done to encrypt fields or save space, and single-field indexes are permitted.
What is the primary advantage of including a reduce function in a CouchDB secondary index view?
Explanation: Reduce functions aggregate values emitted by the map function, supporting operations like summing or counting. They do not increase document returns, remove duplicates at the document level, or alter document keys.
Which of the following is a key limitation of secondary indexes in CouchDB compared to some relational databases?
Explanation: CouchDB secondary indexes do not enforce uniqueness on indexed values. They do not automatically validate data types, are not limited to numeric fields, and do not provide default encryption for indexed fields.