Test your understanding of database indexing principles such as B-tree structures, composite and partial indexes, covering indexes, and performance analysis using EXPLAIN and ANALYZE statements. This quiz reinforces essential indexing concepts to help improve query efficiency and database performance.
Which of the following best describes the primary purpose of a B-tree index in a relational database?
Explanation: B-tree indexes organize data in a balanced tree structure, significantly speeding up search operations by efficiently locating records. While data redundancy, transaction management, and blob storage are important issues in databases, they are not directly related to the main purpose of B-tree indexes. Options about redundancy, blobs, or transactions do not accurately reflect the key use of B-trees.
What is a composite index in the context of relational databases?
Explanation: A composite index is built using more than one column, which helps queries filter by multiple columns more quickly. An index on a single column is not composite, while backups and temporary in-memory structures are unrelated distractors. The correct definition highlights the use of multiple columns, which sets composite indexes apart.
Which scenario best illustrates when to use a partial index?
Explanation: Partial indexes are useful when you only need to index a subset of rows, such as active users, rather than all records. Indexing the entire table would not be partial, and backups or encryption are unrelated features. Using partial indexes optimizes storage and improves performance for targeted queries.
What is a covering index in SQL terminology?
Explanation: A covering index stores all columns needed for a query so the database can satisfy the request using only the index, improving efficiency. Indexes for foreign keys or hash maps are distinct concepts, and backup uses are unrelated. The essence is that the index 'covers' the query without accessing the main table.
What does the EXPLAIN statement help database users achieve?
Explanation: EXPLAIN provides insight into how a database will execute a query, revealing indexing and access methods. Encryption, deletion, and schema creation are unrelated actions not associated with the EXPLAIN statement. The true value is in visualizing the query plan to optimize performance.
Which best describes the use of the ANALYZE statement in a relational database?
Explanation: ANALYZE helps maintain up-to-date statistics on table data, which assists the query planner in making efficient decisions. Deleting indexes, truncating tables, and sorting results are different operations not associated with this command. Accurate statistics are important for effective indexing and query performance.
When a database uses an index scan instead of a sequential scan, what benefit does it provide?
Explanation: An index scan can quickly find specific rows based on index keys, improving performance compared to scanning every row. Index scans do not provide encryption, compression, or deduplication features, which are handled differently. The main advantage is speeding up lookups.
Which factor is most important when deciding which columns to include in an index?
Explanation: Indexing columns commonly used in WHERE clauses can significantly speed up filtering in queries. SELECT-list-only columns or those with only nulls or no references benefit little from indexing. Relevant columns should match query patterns to maximize index effectiveness.
What is a potential drawback of creating too many indexes on a table?
Explanation: Maintaining many indexes increases overhead during data changes, slowing inserts and updates. More indexes do not necessarily boost speed, nor do they affect logging or read-only status. Balancing the number of indexes is key for performance.
What main function does a unique index serve in a database?
Explanation: Unique indexes enforce the rule that the indexed columns cannot have duplicate values. Sorting, compression, or backup features are not functions of unique indexes. The enforcement of uniqueness helps maintain data integrity.
Suppose a query requests columns A and B, and there is an index on (A, B). What is this scenario called?
Explanation: With both requested columns in the index, the database can satisfy the query using just the index—a scenario known as a covering index. Full table scan, primary key lookup, and partial index do not accurately describe this efficient access method.
When running EXPLAIN on a query, what indicates that an index is being used?
Explanation: Seeing 'Index Scan' or similar in EXPLAIN results means the query planner chooses to use an index. Database version, code listing, or column data types in the EXPLAIN output are not direct indications of index usage.
What is the main advantage of a partial index over a regular index?
Explanation: Partial indexes cover only part of a table, reducing space and improving performance for targeted queries. They do not necessarily cover more queries, auto-index foreign keys, or prevent all duplicates. Their main strength is focusing on relevant data.
If you have a composite index on (customer_id, order_date), which query will benefit most from this index?
Explanation: Composite indexes are most effective when queries filter by the indexed columns in order, so using both customer_id and order_date is optimal. Filtering only by non-indexed columns or just the second part (order_date) reduces index efficiency.
What is the primary difference between EXPLAIN and EXPLAIN ANALYZE statements?
Explanation: EXPLAIN offers a prediction of the query plan, while EXPLAIN ANALYZE executes the query and reports real timing and row counts. They do not produce the same output, ANALYZE is not mainly about output format, and both can be used with several types of statements.
How do indexes typically affect UPDATE statements on a table?
Explanation: Having indexes means that updates must also modify the index entries, potentially slowing down UPDATEs. Indexes do not block updates, encrypt values, or exclusively affect SELECTs. The maintenance of index structures upon updates is the main impact.