Test your understanding of key database indexing concepts, including composite and covering indexes, and how to interpret output from the EXPLAIN command. This quiz is designed to help beginners evaluate their knowledge of efficient query performance, indexing strategies, and query optimization.
What is the primary benefit of using an index in a database table?
Explanation: Indexes help improve the speed of data retrieval by allowing the database to find rows faster. The other options are incorrect: encryption and compression are not main purposes of indexes, and indexes do not remove duplicates automatically.
Which of the following best describes a composite index in a database?
Explanation: A composite index is created on two or more columns of a table, enabling queries filtered by those columns to execute more efficiently. An index on a single numeric column is not necessarily composite. A backup copy of the primary key and compressed indexes refer to different concepts.
If you often query a table using conditions on both 'first_name' and 'last_name', what type of index would improve performance the most?
Explanation: A composite index on both columns is optimized for queries that filter by both 'first_name' and 'last_name'. Indexing only one column helps only for conditions on that column. A single index on the entire table is not a valid indexing strategy.
What is the main advantage of a covering index when running SELECT queries?
Explanation: A covering index includes all columns referenced in the query, allowing the database to return results directly from the index. Covering indexes do not guarantee uniqueness, nor do they always use less space. They do not repair corrupt data.
What is the purpose of using the EXPLAIN command in SQL?
Explanation: The EXPLAIN command shows the execution plan for a query, including which indexes are used and how rows are accessed. It does not alter table structure, insert records, or encrypt data.
If the EXPLAIN output for a query shows 'Using index' in the Extra column, what does this mean?
Explanation: 'Using index' indicates a covering index is in effect, and the table's data rows do not need to be accessed. The other options are incorrect: 'No index' would be stated if not used, syntax errors are not related, and index rebuilding is not shown this way.
Which statement is TRUE about a unique index?
Explanation: Unique indexes enforce uniqueness on the indexed columns, preventing duplicate values. They are unrelated to data compression, do not necessarily create composite indexes, and can apply to just one column.
Given a composite index on ('city', 'state'), which query will benefit most from this index?
Explanation: With a composite index on ('city', 'state'), queries filtering both columns in order will benefit most. Filtering solely on 'state' isn’t as optimal due to index ordering. The other queries do not use the indexed columns.
Which feature accurately describes a non-clustered index?
Explanation: A non-clustered index is stored separately from the actual table and points to the data rows. It does not sort the whole table, prevent updates, or automatically compress data.
What is a potential drawback of adding too many indexes to a single table?
Explanation: Each additional index must be maintained during insert or update, slowing those operations. Queries do not ignore all indexes, indexes are not combined automatically, and they do not delete data.