Indexing & Performance Interview Questions
Comprehensive indexing & performance interview questions and answers for SQL. Prepare for your next job interview with expert guidance.
Questions Overview
1. What is an index in SQL and what is its primary purpose?
Basic2. What is the difference between clustered and non-clustered indexes?
Basic3. What is index selectivity and why is it important?
Moderate4. How do composite indexes work and when should they be used?
Moderate5. What is the impact of NULL values on index performance?
Moderate6. What is index fragmentation and how does it affect performance?
Advanced7. How do you optimize queries using execution plans?
Advanced8. What are covering indexes and when should they be used?
Advanced9. How does index maintenance affect database performance?
Advanced10. What are filtered indexes and when are they beneficial?
Advanced11. How do you identify and resolve index-related blocking issues?
Advanced12. What is parameter sniffing and how does it affect query performance?
Advanced13. How do statistics impact query performance and index usage?
Advanced14. What are the best practices for indexing foreign keys?
Moderate15. How do you optimize performance for large table operations?
Advanced16. What is index intersection and when does it occur?
Advanced17. How do you handle indexing for temporal tables?
Advanced18. What are bitmap indexes and when are they appropriate?
Advanced19. How do you optimize query performance in reporting scenarios?
Advanced20. What is the impact of GUID clusters keys on performance?
Advanced21. How do you optimize performance for hierarchical data queries?
Advanced22. What are the considerations for indexing text or VARCHAR(MAX) columns?
Advanced23. How do you handle deadlocks in high-concurrency scenarios?
Advanced24. What is index key compression and when should it be used?
Advanced25. How do you optimize performance for merge operations?
Advanced26. What are the best practices for indexing partitioned tables?
Advanced27. How do you optimize performance for dynamic search conditions?
Advanced28. What are the considerations for indexing temporal data?
Advanced29. How do you handle performance tuning in cloud database environments?
Advanced1. What is an index in SQL and what is its primary purpose?
BasicAn index is a data structure that improves the speed of data retrieval operations by providing quick access to rows in a database table. It creates a pointer to data based on the values of specific columns, similar to a book's index, reducing the need for full table scans.
2. What is the difference between clustered and non-clustered indexes?
BasicA clustered index determines the physical order of data in a table and can only exist once per table. Non-clustered indexes create a separate structure that points to the data and multiple can exist per table. Clustered indexes are typically faster for retrievals but slower for inserts.
3. What is index selectivity and why is it important?
ModerateIndex selectivity is the ratio of unique values to total rows in an indexed column. High selectivity (many unique values) makes an index more effective as it better narrows down the result set. Low selectivity indexes might be ignored by the query optimizer.
4. How do composite indexes work and when should they be used?
ModerateComposite indexes include multiple columns in a specific order. They're useful for queries that filter or sort by multiple columns, following the leftmost principle. The order of columns should match common query patterns and consider column selectivity.
5. What is the impact of NULL values on index performance?
ModerateNULL values in indexed columns can affect performance by increasing index size and complexity. Some databases store NULL values in the index, while others don't. Understanding NULL handling is crucial for optimal index design and query performance.
6. What is index fragmentation and how does it affect performance?
AdvancedIndex fragmentation occurs when the logical order of index pages doesn't match their physical order, or when pages have empty space. It can degrade performance by causing extra I/O operations. Regular maintenance (rebuilding or reorganizing) helps maintain optimal performance.
7. How do you optimize queries using execution plans?
AdvancedExecution plans show how SQL Server processes a query, including index usage, join types, and estimated costs. Analyze plans to identify full table scans, inefficient joins, or missing indexes. Use this information to optimize queries through index creation or query restructuring.
8. What are covering indexes and when should they be used?
AdvancedCovering indexes include all columns needed by a query in the index itself, eliminating the need to access the table. They improve performance by reducing I/O but increase storage space and maintenance overhead. Use them for frequently run queries that access a limited set of columns.
9. How does index maintenance affect database performance?
AdvancedIndex maintenance operations (rebuilding, reorganizing) can impact performance by consuming resources and blocking operations. Schedule maintenance during low-usage periods, consider online operations, and balance frequency against database performance needs.
10. What are filtered indexes and when are they beneficial?
AdvancedFiltered indexes include only a subset of rows based on a predicate. They're smaller and more efficient for queries matching the filter condition. Use them when queries frequently access a specific subset of data or for implementing row-level security.
11. How do you identify and resolve index-related blocking issues?
AdvancedMonitor blocking using dynamic management views, identify long-running transactions or lock escalation issues. Solutions include optimizing transaction duration, using appropriate isolation levels, implementing row versioning, or adjusting index design.
12. What is parameter sniffing and how does it affect query performance?
AdvancedParameter sniffing occurs when SQL Server reuses an execution plan optimized for specific parameter values. It can lead to poor performance when data distribution varies significantly. Solutions include using RECOMPILE hints or local variables.
13. How do statistics impact query performance and index usage?
AdvancedStatistics provide the query optimizer with data distribution information to choose efficient execution plans. Outdated or missing statistics can lead to poor plan choices. Regular updates and appropriate sampling rates are crucial for optimal performance.
14. What are the best practices for indexing foreign keys?
ModerateIndex foreign key columns to improve JOIN performance and maintain referential integrity efficiently. Consider column order in composite indexes, include frequently queried columns, and evaluate the impact on write operations.
15. How do you optimize performance for large table operations?
AdvancedStrategies include partitioning, batch processing, minimizing logging, using appropriate isolation levels, and considering index impact. For maintenance operations, use minimal logging, tempdb optimization, and parallel execution when possible.
16. What is index intersection and when does it occur?
AdvancedIndex intersection occurs when the query optimizer uses multiple indexes to satisfy a query. While it can be efficient for some queries, too many index intersections might indicate the need for a better composite index.
17. How do you handle indexing for temporal tables?
AdvancedTemporal table indexing requires consideration of both current and history tables. Index historical columns based on query patterns, consider filtered indexes for active records, and maintain appropriate statistics for both tables.
18. What are bitmap indexes and when are they appropriate?
AdvancedBitmap indexes use bit arrays to track row locations for specific values. They're efficient for low-cardinality columns and complex AND/OR operations but perform poorly with frequent updates. Common in data warehousing scenarios.
19. How do you optimize query performance in reporting scenarios?
AdvancedUse covering indexes for common report queries, consider indexed views, implement partitioning for large tables, and evaluate materialized views. Balance real-time needs against data freshness requirements.
20. What is the impact of GUID clusters keys on performance?
AdvancedGUID clustered keys can cause page splits and fragmentation due to random value insertion. This impacts performance through increased I/O and maintenance overhead. Consider sequential GUIDs or alternative key designs for better performance.
21. How do you optimize performance for hierarchical data queries?
AdvancedUse appropriate indexing for parent-child relationships, consider materialized path or nested sets models, implement covering indexes for common traversal patterns, and evaluate graph database features for complex hierarchies.
22. What are the considerations for indexing text or VARCHAR(MAX) columns?
AdvancedFull-text indexes for text search, filtered indexes for non-NULL values, and careful evaluation of included columns. Consider partial indexing strategies and impact on maintenance operations.
23. How do you handle deadlocks in high-concurrency scenarios?
AdvancedMonitor deadlocks using trace flags or extended events, analyze deadlock graphs, optimize transaction patterns, adjust isolation levels, and ensure consistent access order for resources. Consider index design impact on lock types.
24. What is index key compression and when should it be used?
AdvancedIndex key compression reduces storage space by eliminating redundant key values. It's beneficial for indexes with many duplicate values or long key values, but increases CPU usage. Evaluate compression benefits against performance impact.
25. How do you optimize performance for merge operations?
AdvancedUse appropriate indexes for join conditions, consider batch processing, implement proper transaction handling, and evaluate MERGE statement alternatives. Monitor lock escalation and consider impact on existing indexes.
26. What are the best practices for indexing partitioned tables?
AdvancedAlign indexes with partition scheme, consider local vs. global indexes, implement filtered indexes for partition elimination, and maintain statistics at the partition level. Balance maintenance overhead against query performance needs.
27. How do you optimize performance for dynamic search conditions?
AdvancedImplement proper parameter handling, consider filtered indexes for common conditions, use dynamic SQL carefully, and evaluate index impact of different search patterns. Consider using OPTION (RECOMPILE) for highly variable queries.
28. What are the considerations for indexing temporal data?
AdvancedInclude date/time columns in appropriate index position, consider partitioning for historical data, implement sliding window maintenance, and evaluate impact of timezone handling on query performance.
29. How do you handle performance tuning in cloud database environments?
AdvancedConsider elastic resources, monitor DTU/vCore usage, implement appropriate scaling strategies, evaluate cost-based optimization, and understand cloud-specific indexing limitations. Balance performance against cloud resource costs.