Test your knowledge on database indexing basics—covering concepts like composite and covering indexes, the use of EXPLAIN, and the balance between read and write performance. Ideal for learners seeking to strengthen their understanding of how indexing impacts database queries and efficiency.
Which option best describes a composite index in a database?
Explanation: Composite indexes are built on multiple columns, allowing efficient lookups when queries filter on those columns together. They are not limited to just the primary key or automatically generated for all tables. Preventing duplicate rows is the job of unique constraints, not standard composite indexes.
In the context of indexing, what is a covering index?
Explanation: A covering index contains all fields required to satisfy a particular query, so the database engine can return results from the index itself. It does not span multiple tables, is not solely for sorting, and typically speeds up reads but may not improve writes.
What is the main purpose of using the EXPLAIN statement before a query?
Explanation: The EXPLAIN statement shows the query execution plan and reveals whether indexes will be used, helping to optimize queries. It does not rename columns, modify data, or create new indexes.
How do indexes generally affect the performance of write operations such as INSERT, UPDATE, or DELETE?
Explanation: Indexes must be updated when data changes, so they usually introduce some overhead to write operations. They do not make writes faster, have no effect, or prevent them entirely.
Which index type would best improve the speed of a query sorting rows by two columns, for example, ORDER BY last_name, first_name?
Explanation: A composite index that matches the ORDER BY columns allows the database to efficiently sort and retrieve data. A single-column index or an index on unrelated columns will not fully optimize such a sort, and full-text indexes are for searching text, not sorting.
When a query can use an appropriate index, how does this generally affect read operation speed?
Explanation: Proper indexing can drastically reduce the amount of data scanned, speeding up queries. Indexes generally do not slow down reads, prevent results, or control the display output.
If you have separate single-column indexes on 'username' and 'email', which type of query may not be fully optimized?
Explanation: Single-column indexes are effective for queries filtering on one column, but a composite index is better for queries filtering on both 'username' and 'email' at the same time. Filtering by just one column will still use the corresponding single-column index, and sorting by an unindexed column is unrelated.
In which situation is it usually best to avoid adding an index on a column?
Explanation: Indexing columns not used in search conditions wastes resources without speeding up queries. High uniqueness or frequent usage in queries can benefit from an index, and indexing is advisable for columns regularly queried for fast lookups.
If EXPLAIN output for a query shows 'Full Table Scan', what does this mean?
Explanation: A full table scan means no suitable index was used, so each row is checked individually. This is slower than direct index usage. Partial index use or syntax errors are unrelated, and direct lookups suggest indexed queries.
Why does the order of columns in a composite index matter for query optimization?
Explanation: The order affects which columns can be used efficiently in searches; only the leading columns support fast searches or sorts. Indexes are always ordered, not random, and all included columns can be utilized if queries match the order. There is no requirement for alphabetical order.
What is a typical trade-off when adding more indexes to a table?
Explanation: While more indexes improve query speed, they add overhead to data changes because all relevant indexes must update. Adding indexes does not slow both reads and writes, nor does it speed up writes at the expense of reads. There is always some performance effect.
What is a likely outcome if an unused index on a large table is dropped?
Explanation: Removing an unused index can speed up inserts, updates, and deletes since there’s one less data structure to update. Dropping an index doesn't speed up queries depending on it, cause corruption, or create a new primary key, which is a separate table property.
Given a query 'SELECT age FROM users WHERE last_name = ? AND first_name = ?', how can a covering index be achieved?
Explanation: Including all columns referenced in SELECT and WHERE clauses within one index lets the database resolve the query with just the index. Indexing only 'age' is ineffective because it does not cover the WHERE clauses. Unique constraints and foreign keys serve different database integrity purposes.
What is the effect of having two identical indexes on the same columns in a table?
Explanation: Duplicate indexes add unnecessary storage and maintenance cost, with no added performance benefit. Having identical indexes doesn't increase speed, enforce uniqueness, or cause the database to disable them.
Which type of query benefits most from a single-column index on 'customer_id'?
Explanation: A query filtering by 'customer_id' matches the index, speeding lookups for that value. Filtering by another column, ordering by date, or fetching all rows without filters does not use the single-column 'customer_id' index efficiently.
Which index would speed up a query like 'SELECT * FROM sales WHERE date u003E= ? AND date u003C= ?'?
Explanation: A single-column index on the 'date' field optimizes searches bounded by date ranges. Composite indexes on unrelated columns, unique indexes on different fields, or primary keys unrelated to 'date' will not help for this type of range query.