Explore essential concepts of database indexing with this quiz, designed to enhance your understanding of how indexes improve query performance and storage efficiency. Perfect for learners seeking foundational knowledge on types of indexes, how they work, and best practices for database optimization.
What is the primary purpose of an index in a relational database table?
Explanation: The main function of a database index is to enhance the speed of data retrieval operations, making searches more efficient. Preventing duplicate data is the job of constraints, not indexes. Encrypting stored data is handled by security mechanisms, not indexing. Although indexes may increase storage usage, their purpose is not to do so intentionally.
Which type of index only contains references to the storage location of data instead of storing the actual data rows?
Explanation: A non-clustered index stores pointers to the data rather than the data itself, facilitating faster searches without rearranging physical row order. Clustered indexes sort and store the actual data rows, not just references. Hashed and text indexes are specific types for certain queries and do not specifically refer to this approach.
If a query frequently searches for employees by last name, which column should be indexed for optimal performance?
Explanation: Indexing the 'last name' column is ideal if queries often filter or sort by last name, resulting in faster data retrieval. The other columns—first name, employee ID, or date of birth—would not provide the same benefit if they are not frequently queried, making them less appropriate for indexing in this scenario.
What is a potential disadvantage of creating too many indexes on a single database table?
Explanation: Too many indexes increase storage needs and can degrade performance for insert, update, or delete operations since each index must be updated. While well-chosen indexes improve query speed, over-indexing can have the opposite effect for write-heavy tables. Indexes do not cause accidental data deletion or decrease table size.
Which type of index ensures that no two rows have the same value in one or more columns?
Explanation: A unique index enforces uniqueness by preventing duplicate values in the indexed column(s). Partial and composite indexes have different purposes, such as indexing a subset of data or multiple columns respectively but do not enforce uniqueness. Bitmap indexes are typically used for columns with few distinct values and also do not ensure uniqueness.
When can a database make the most effective use of an index during a query?
Explanation: An index is most useful if it includes columns commonly used in search conditions (WHERE clause), as it speeds up locating relevant rows. Indexes on only SELECTed columns do not improve filtering speed. Creating an index after a query does not help that query, and having no indexes provides no performance gain.
Which data structure is most commonly used to implement a general-purpose database index for ordered retrieval?
Explanation: B-tree structures keep data sorted and allow for efficient range and equality searches, making them ideal for most database indexes. Linked lists and queues are better for sequential operations, while hash tables excel at direct lookups but not ordered retrieval, which is essential in many indexing scenarios.
How does the presence of indexes on a table affect delete operations?
Explanation: When deleting rows, all corresponding index entries must also be updated or removed, which can slow down the operation. Indexes do not make deletes inherently faster or unaffected. Deletions are never completely blocked by indexes; only the speed may be influenced.
If a composite index is created on (first_name, last_name), which query will utilize the index most efficiently?
Explanation: A composite index on (first_name, last_name) is best utilized when queries include both columns starting with the first component. Using only the second column or unrelated columns, as in other options, provides limited or no benefit from this index.
Are NULL values included in most default database indexes?
Explanation: Whether NULLs are indexed varies based on database settings and index type due to different treatment across systems. They are not always excluded or always included by default. Saying NULL indexes cannot exist is incorrect, as indexes can reference rows containing NULLs.