Test your knowledge of database indexing basics, including composite and covering indexes, as well as how to read EXPLAIN and ANALYZE outputs. This beginner-friendly quiz helps you grasp essential indexing concepts and interpret query plans for efficient database performance.
What is a composite index in the context of databases?
Explanation: A composite index is defined as an index that involves more than one column of a table, helping speed up queries that filter on those columns. Compressed storage and foreign keys are unrelated to the concept of composite indexes. A single-column index is not composite by definition.
When is an index called a 'covering index' for a query?
Explanation: A covering index is one where all columns used in the query (SELECT, WHERE, etc.) are present in the index, so the database does not need to refer to the actual table. The uniqueness of an index or the exclusion of NULLs does not make an index covering. Only using indexed columns in the WHERE clause doesn't guarantee covering unless all selected columns are indexed as well.
What primary information does the EXPLAIN command provide for a database query?
Explanation: EXPLAIN helps visualize the steps the database engine plans to take when executing a query, including which indexes will be used. It doesn't provide timing information (that’s typically shown with ANALYZE) or show table structure or a full list of index definitions.
Which type of query benefits most from a composite index on (first_name, last_name)?
Explanation: Composite indexes are most effective when the WHERE clause matches the columns in the order the index is defined. Selecting only last_name or updating the primary key does not leverage the composite index properly. Sorting by another column isn't directly helped by this index either.
How can EXPLAIN or ANALYZE output help determine if a query uses an index?
Explanation: In the query plan, EXPLAIN or ANALYZE often specify terms like 'Index Scan' or 'Using Index' if an index is used. They do not display database structure, count rows, or detect syntax errors. Syntax errors are found before query planning, not through EXPLAIN or ANALYZE.
Why does the order of columns matter in a composite index (e.g., (city, age))?
Explanation: The most selective (distinct) columns should be first, and queries should filter or order by the leading columns to benefit. Indexes can be created on numeric columns, and the last column alone does not provide index benefit if leading ones aren't filtered. Encryption is unrelated to column order in indexes.
Which index type generally helps the most in speeding up SELECT queries that retrieve specific rows?
Explanation: B-tree indexes are efficient for retrieving specific rows and are the default for most relational databases. Hash map indexes are less common and typically for equality comparisons only. A unique constraint is a rule, not a search aid, and a full table scan is not an index at all and is generally slower.
Why do covering indexes often speed up query performance?
Explanation: When a covering index is used, the database can return query results using just the index, skipping extra reads of table rows. Compression, enforcing uniqueness, or storing data outside the database are not features of covering indexes.
If an EXPLAIN plan shows 'Index Scan using idx_student_name', what does this indicate?
Explanation: An 'Index Scan' specifies that the mentioned index, here idx_student_name, is involved in retrieving query results. If it were a full table scan, it would be labeled as such. Index rebuilding is unrelated to query planning, and the presence of the index shows indexes do exist.
Given a composite index on (country, city), which query would most benefit from this index?
Explanation: This query filters by both country and city, matching the index column order and allowing efficient searching. Filtering only by city is not as efficient unless country is included. The region column isn't indexed, and OR queries usually do not effectively use composite indexes.
How does ANALYZE differ from EXPLAIN when running a query?
Explanation: ANALYZE actually runs the query for more accurate feedback, including times and number of rows processed. EXPLAIN is a planning tool and does not risk running slow or unsafe queries. Neither command is for table statistics collection or SQL error reporting.
What does it mean if the EXPLAIN plan says 'Seq Scan' or 'Full Table Scan'?
Explanation: A sequential or full table scan means each row is checked, indicating indexes are not used for that query step. Syntax errors stop the query before reaching EXPLAIN. Composite indexes are not used here. Uniqueness of returned rows is fixed by query logic, not the scan type.
Which scenario makes a composite index less effective?
Explanation: If the query filters only by columns that are not the first in the composite index, the index can't be used efficiently. When both columns are filtered, or queries use the index columns for sorting or WHERE conditions, the composite index is beneficial.
What is a possible downside of adding too many columns to a covering index?
Explanation: The more columns an index contains, the larger and slower it can become for writes or updates, and it will use more disk space. While some queries may benefit from the index, others could see a negative impact. The database schema remains valid regardless, and speed increases are not guaranteed universally.
What does an 'Index Only Scan' signify in a query execution plan?
Explanation: An 'Index Only Scan' means the query is covered by the index, so the main table rows don't need to be accessed, speeding up reads. It's not about partial index use or duplication. The plan does not identify missing indexes in this step.