Discover the most frequent SQL indexing mistakes and how to resolve them with this beginner-friendly quiz. Improve your understanding of index usage, creation, and optimization strategies for efficient database management.
If a table has a 'customer_email' column frequently used in WHERE clauses but lacks an index on it, what is a likely consequence?
Explanation: Queries filtering on a frequently searched column without an index will generally cause slower performance because the database must scan the entire table. Databases do not automatically index every frequently searched column. Lack of indexes does not lead to duplicate data or improved security; instead, it mainly affects speed and efficiency.
What is a common negative effect of creating excessive indexes on a single SQL table?
Explanation: Each index must be updated when data is modified, so too many indexes slow down operations like INSERT, UPDATE, or DELETE. Databases do not always remove unused indexes automatically. More indexes actually increase storage usage, not decrease it. While some queries may benefit, overall speed can be negatively impacted by too many indexes.
Why is placing an index on a column like 'is_active' (with only two values: 0 or 1) generally ineffective?
Explanation: Columns with only a few distinct values, like a boolean, have low selectivity, making indexes on them less useful for filtering. An index on such columns does not lock the table or prevent row deletion. Furthermore, indexing does not automatically encrypt data; it only affects query efficiency.
What should you do after dropping or renaming a column that is part of an index?
Explanation: If a column involved in an index is dropped or renamed, the index may become invalid or less efficient, so you should update or recreate it. Indexes do not always automatically adjust themselves fully. Adding more foreign keys or changing the primary key size are unrelated to the need for index maintenance after structure changes.
When a query frequently filters by both 'username' and 'created_at', but there are only single-column indexes, what improvement is most effective?
Explanation: Composite indexes help queries that filter or sort by multiple columns, so creating one improves such cases. Removing indexes would hinder performance further. Renaming the columns or partitioning the table by an unrelated column are not directly relevant to multi-column search performance.
How can you best determine whether your index is being used by a SQL query?
Explanation: The execution plan shows how the database processes the query, including whether an index is used. Counting rows or checking data types does not reveal index usage. Adjusting fill factor affects index storage efficiency but not immediate usage visibility.
Why might indexing a heavily updated column like 'last_login_time' cause problems?
Explanation: Indexes on frequently updated columns need to be modified every time the data changes, causing overhead and slowing updates. Indexing does not compress data, double values, or enforce uniqueness unless explicitly specified. Only the maintenance cost is directly related here.
What is a possible consequence of significant index fragmentation in an SQL database?
Explanation: Fragmented indexes can cause less efficient data access, slowing index scans. Fragmentation does not cause indexes to be deleted automatically, nor does it impact locking mechanisms or reset index values. The primary concern is reduced scan speed.
Why might only indexing primary key columns be insufficient for complex SELECT queries?
Explanation: Complex queries often require quick access to columns other than primary keys, so indexing those additional columns can improve performance. Primary keys are always indexed by default, and lack of other indexes does not cause errors or impact foreign key relationships directly.
What is a common misconception about indexes in SQL databases?
Explanation: Not all indexes improve every query; sometimes, indexes can slow down modifications or be unused for certain queries. Index maintenance and selective indexing are important concepts, and index usefulness depends on usage patterns. Thinking every index is always helpful is incorrect.