How Execution Plan Operators Affect Query Performance

Understanding SQL Server Execution Plan Operators and Their Impact on Performance

Optimizing query performance in SQL Server requires understanding execution plans. Here’s a look at common operators that can impact performance, ranked by their frequency and impact, with examples using the opensource StackOverflow database.

1. Table Scans

  • Commonality: Very High
  • Impact: High
  • Description: Occurs when SQL Server reads every row in a table, often due to lack of indexes.
  • Example: SELECT * FROM Users WHERE Reputation > 10000;
    • Note: Results in a table scan if no index on Reputation.

2. Index Scans

  • Commonality: High
  • Impact: Moderate to High
  • Description: Reads every row in an index; less efficient if the index is not selective.
  • Example: SELECT DisplayName FROM Users WHERE CreationDate > ‘2020-01-01’;
    • Note: Might result if there is no index CreationDate.

3. Key Lookups

  • Commonality: High
  • Impact: Moderate to High
  • Description: Occurs when a non-covering index requires additional lookups to fetch complete rows.
  • Example: SELECT DisplayName, Location FROM Users WHERE location = ‘New York, NY’;
    • Note: Results in key lookups if the index on Location does not include DisplayName .

4. Nested Loops

  • Commonality: Moderate
  • Impact: Variable
  • Description: Efficient for small datasets; can be inefficient with larger datasets or costly inner table scans.
  • Example: SELECT p.Title, u.DisplayName FROM Posts p INNER JOIN Users u ON p.OwnerUserId = u.Id WHERE u.Reputation > 10000;
    • Note: Might use nested loops if the Users table is large and lacks a suitable index.

5. Sort Operations

  • Commonality: Moderate
  • Impact: High
  • Description: Required by ORDER BY clauses, certain joins, or DISTINCT operations.
  • Example: SELECT DisplayName FROM Users ORDER BY Reputation DESC;
    • Note: Requires sorting the Users table by Reputation.

6. Hash Joins

  • Commonality: Moderate
  • Impact: High
  • Description: Uses a hash table to join rows from two tables, typically when no suitable indexes exist.
  • Example: SELECT p.Title, u.DisplayName FROM Posts p INNER JOIN Users u ON p.OwnerUserId = u.Id WHERE p.CreationDate > ‘2020-01-01’ AND p.Score > 0 AND u.Reputation > 1000;
    • Note: Might use a hash join if there are no suitable indexes. (Had to force a Hash Join since the optimizer didn’t want to play ball)

7. Spools

  • Commonality: Moderate
  • Impact: Moderate to High
  • Description: Temporarily stores intermediate results to optimize certain queries.
  • Example: SELECT p.Title FROM Posts p WHERE LEN(p.Body) > (SELECT AVG(LEN(p2.Body)) FROM Posts p2 WHERE p2.OwnerUserId = p.OwnerUserId) AND p.OwnerUserId IN (SELECT DISTINCT u.Id FROM Users u WHERE u.Reputation > 1000);
    • Note: Complex queries with subqueries and joins might cause a spool operation.

8. Parallelism

  • Commonality: Variable
  • Impact: High
  • Description: Distributes query processing across multiple CPU cores.
  • Example: SELECT COUNT(*) FROM Posts;
    • Note: SQL Server might use parallelism to speed up the count operation for large tables.

Summary

  • Most Common: Table Scans, Index Scans, Key Lookups
  • Most Impactful: Table Scans, Sort Operations, Hash Joins, Parallelism

Leave a comment