Test your knowledge of database indexing basics, including composite and covering indexes, and understanding EXPLAIN/query plans. This quiz helps users identify key concepts, best practices, and definitions crucial for efficient database query performance.
Which statement best describes a composite index in a relational database?
Explanation: A composite index is one that covers two or more columns from a table to improve multi-column query performance. While primary keys can be indexed, not all composite indexes are primary keys. They are not created automatically by the system but by user command, and they are not limited to unique values by default, unlike unique indexes or constraints.
What is a covering index in the context of query optimization?
Explanation: A covering index includes all the columns required to satisfy a query without needing to access the underlying table records. It is not a duplicate of another index or limited to primary keys. Covering indexes are active once created and used by the query planner when advantageous.
When examining an EXPLAIN plan, what does the term 'index scan' usually mean?
Explanation: 'Index scan' means the query engine is using an existing index to retrieve relevant rows, enhancing efficiency. Table scans indicate no index usage, while skipping an index means the engine does not utilize it at all. Rebuilding the index refers to maintenance tasks, not query execution.
For which type of queries does a composite index on columns (A, B) offer the best performance benefit?
Explanation: Composite indexes are most efficient when queries filter, sort, or search by both columns included in the index, such as (A, B). An index on (A, B) does not help with queries filtering only by column C. Changing table schemas or row counts doesn't directly relate to index usage.
If you have an index on columns (first_name, last_name), what will happen if you also create a separate index on first_name alone?
Explanation: Having both a composite index (first_name, last_name) and a single-column index on first_name may not benefit performance and could consume extra space unnecessarily. Creating additional indexes doesn't always increase speed, doesn't remove existing indexes, and doesn't convert them into unique indexes by default.
What distinguishes a composite unique index from a regular composite index?
Explanation: A composite unique index requires unique combinations across the indexed columns, ensuring data integrity. Regular composite indexes do not enforce uniqueness. Uniqueness does not inherently make an index faster or single-column, nor does it block queries from executing.
In a query such as SELECT id, name FROM users WHERE age = 30, what would make an index on (age, id, name) a covering index?
Explanation: A covering index contains every column referenced in the query, so (age, id, name) allows the query to be answered entirely from the index. An index on one column, or a partial or unique index, does not guarantee coverage of all columns needed.
What is the typical purpose of a primary key index in a relational database?
Explanation: Primary key indexes ensure each record in a table is unique and allow fast, reliable data retrieval. They do not cause redundant storage, block covering indexes, or hinder query speed; in fact, they boost efficiency.
Why does the order of columns in a composite index matter?
Explanation: Column order in a composite index determines which combinations of query filter conditions can use the index. The notion that all indexes are unordered, that only the last column is used, or that it directly alters column names, is incorrect.
When viewing a query plan, which clue indicates that a covering index is being used?
Explanation: A covering index is utilized when the plan indicates rows are returned by accessing just the index without accessing the underlying table rows. A full table scan or a cartesian join suggests inefficient query execution, and cache access is not specifically related to covering indexes.
If you have a composite index on (A, B), which WHERE clauses will the index help the most?
Explanation: A composite index on (A, B) is most effective with queries that filter first by column A, then by B in that sequence. Swapping the order in the WHERE clause may not optimize as well, depending on the database's optimizer. Queries on C or D do not match the indexed columns.
If a query filters only by the first column in a composite index, can the index still be used?
Explanation: An index on (A, B) can speed up queries filtering by A only, as the index is ordered starting with A. The index won't be ignored for single-column queries, and uniqueness is not required for this feature. Specifying all columns is not mandatory for index usage.
What should you examine in an EXPLAIN plan to determine if a composite index is being used?
Explanation: EXPLAIN plans often show the name of the index employed by the query, which identifies composite index usage. Table scans mean indexes are not used, duplicate keys relate to logic errors, and syntax errors do not relate to index selection.
Which index would be most effective for this query: SELECT email FROM customers WHERE first_name = 'Sam' AND last_name = 'Smith'?
Explanation: A composite index on the columns used in WHERE and SELECT clauses, like (first_name, last_name, email), can serve as a covering index and offer the best performance. Indexes on unrelated columns or omitting indexing altogether will not help this query.
What is a potential disadvantage of creating many indexes on a single table?
Explanation: Having too many indexes can decrease insert and update performance, as each change may require updating multiple indexes. Many indexes do not always guarantee speed, do not eliminate duplicate data, and have no effect on the number of columns in a table.
If a query plan shows a full table scan despite the presence of an index, what might be one cause?
Explanation: A table scan occurs when none of the query's filter conditions match the indexed columns, making the index ineffective. Indexes are not always ignored or disabled by default, and correct column spelling does not ensure index usage if query logic does not align with indexed fields.