Test your understanding of fundamental database indexing concepts, including composite and covering indexes, and how to interpret EXPLAIN and QUERY PLAN outputs. This quiz is designed to help you review key principles for optimizing database queries and improving performance.
Which of the following best describes a composite index in a database table?
Explanation: A composite index involves more than one column to optimize searches that require filtering or ordering by several fields. An index created using only the primary key is not composite, as it contains just one column. Storing duplicate data is not a defining feature of composite indexes. An index that includes only one column is a single-column index, not composite.
What is a main benefit of a covering index for certain database queries?
Explanation: A covering index contains all the columns needed for a query, so the database can fulfill the request without accessing the main table, improving speed. Covering indexes do not always improve updates and deletes, which can become slower. While they may increase storage usage, this is not universal or their main feature. Far from slowing SELECT statements, covering indexes are designed to make them faster.
Which statement best describes the purpose of the EXPLAIN or QUERY PLAN command in databases?
Explanation: EXPLAIN plans reveal the steps a database engine will take to execute a query, so users can analyze and optimize performance. It does not alter data storage or modify table records. The command does not automatically create indexes or perform encryption, both of which require separate actions.
If an EXPLAIN plan indicates 'Index Scan' for a particular query, what does this mean?
Explanation: An 'Index Scan' means the database is leveraging an existing index to search for data, which is more efficient than scanning the entire table. If the plan said 'Seq Scan' instead, it would indicate no index use. An 'Index Scan' does not mean an error or that the query is ignored—it is actually a sign of performance optimization.
When can a database use an index to optimize a query with an ORDER BY clause?
Explanation: Indexes can help ORDER BY performance if the ordering columns align with the first (leading) columns of an index. If the columns are not present in any index, or listed in reverse, the index cannot be fully utilized for sorting. The presence or absence of a WHERE clause does not affect whether the ORDER BY can use an index.
In what situation might the order of columns in a composite index matter for query optimization?
Explanation: The order of columns in a composite index is important because the database can quickly use the index only when the first columns are included in search or sort conditions. If no WHERE clause is present, indexing is usually less helpful. If all columns are always used, order is less significant, but partial matches are common. An unused index provides no optimization regardless of column order.
How can creating an index on a large table affect query performance?
Explanation: Indexes are designed to make data retrieval on indexed columns much faster, which is especially valuable in large tables. They do not double the data size, as the index typically only references key data, not the entire row. Indexes do not reduce columns in a table. Claiming no effect is incorrect, as indexes do impact performance.
Suppose a SELECT query consistently performs a full table scan and is slow; what might this indicate?
Explanation: Slow full table scans for queries that filter or sort could mean there is no appropriate index on the relevant columns. Slow performance is not always by design and can often be improved. Databases do not prohibit indexes nor automatically create one for every column, as this would waste resources.
What is a primary characteristic of a unique index in a database?
Explanation: A unique index enforces uniqueness, meaning each value in the indexed columns must be distinct. It definitely stores information about the columns it covers. Rather than causing duplicates, unique indexes are designed to prevent them. They also make searching possible, not impossible.
What is a possible downside of creating too many indexes on a single database table?
Explanation: While indexes speed up searches, having many of them can slow down INSERT, UPDATE, and DELETE operations because the database must update each index entry on data changes. It is untrue that indexes always speed up every query; some are unaffected or even slowed down. Indexes neither make tables read-only nor cause automatic data deletion.