UNPKG

sqlserver-mcp-colossal

Version:

MCP Server for SQL Server database operations with comprehensive CRUD functionality, views, stored procedures, execution plan analysis, and safety features

333 lines (250 loc) • 8.06 kB
# Query Execution Plan Analysis Guide ## Overview The `analyze_query_plan` tool provides comprehensive analysis of SQL Server query execution plans, helping you optimize query performance and identify potential issues. ## How Query Execution Plans Work ### What is an Execution Plan? An execution plan is a detailed roadmap that SQL Server creates to execute your query. It shows: - How data is accessed (scans vs seeks) - Join algorithms used - Index usage - Estimated vs actual row counts - Resource costs ### Types of Operations **Table Scans vs Index Seeks:** - **Table Scan**: Reads entire table (expensive) - **Clustered Index Scan**: Reads entire clustered index - **Index Seek**: Uses index to find specific rows (efficient) - **Clustered Index Seek**: Uses clustered index efficiently **Join Types:** - **Nested Loops**: Good for small datasets - **Hash Join**: Good for larger datasets - **Merge Join**: Good for sorted data ## Understanding the Analysis Output ### Execution Plan Summary ``` šŸ“Š Execution Plan Summary: - Estimated Cost: 1.25 - Estimated Rows: 1000 - Actual Rows: 850 ``` **Key Metrics:** - **Estimated Cost**: Relative cost of the operation - **Estimated Rows**: How many rows SQL Server expects - **Actual Rows**: How many rows were actually processed ### Optimization Recommendations The tool provides prioritized recommendations: **High Priority Issues:** - Missing indexes - Table scans on large tables - Expensive operations (>10% of total cost) **Medium Priority Issues:** - Implicit data type conversions - Suboptimal join algorithms - High estimated costs **Low Priority Issues:** - Minor optimization opportunities - Statistics update suggestions ### Common Warning Types - **Missing Index**: Suggests creating indexes - **Implicit Conversion**: Data type mismatches - **Parameter Sniffing**: Query plan reuse issues - **Statistics Outdated**: Need to update statistics ## Optimization Strategies ### Index Optimization **When to Add Indexes:** - Frequent WHERE clause columns - JOIN columns - ORDER BY columns - GROUP BY columns **Index Types:** - **Clustered Index**: Primary key (one per table) - **Non-Clustered Index**: Secondary indexes - **Covering Index**: Includes all needed columns - **Filtered Index**: For specific value ranges **Example Index Recommendations:** ```sql -- Missing index suggestion CREATE NONCLUSTERED INDEX IX_Customer_LastName_FirstName ON Customers (LastName, FirstName) INCLUDE (Email, Phone) ``` ### Query Rewriting **Common Patterns:** 1. **Avoid SELECT *** ```sql -- Bad SELECT * FROM Orders -- Good SELECT OrderID, CustomerID, OrderDate FROM Orders ``` 2. **Use Proper WHERE Clauses** ```sql -- Bad (causes scan) SELECT * FROM Products WHERE LEFT(Name, 3) = 'ABC' -- Good (can use index) SELECT * FROM Products WHERE Name LIKE 'ABC%' ``` 3. **Optimize JOINs** ```sql -- Ensure JOIN columns are indexed SELECT c.Name, o.OrderDate FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID ``` ### Parameter Sniffing Issues **Problem:** Query plan optimized for first parameter values **Solution:** Use query hints or local variables ```sql -- Problematic CREATE PROCEDURE GetOrders @CustomerID INT AS SELECT * FROM Orders WHERE CustomerID = @CustomerID -- Solution 1: Use OPTION (RECOMPILE) CREATE PROCEDURE GetOrders @CustomerID INT AS SELECT * FROM Orders WHERE CustomerID = @CustomerID OPTION (RECOMPILE) -- Solution 2: Use local variable CREATE PROCEDURE GetOrders @CustomerID INT AS DECLARE @LocalCustomerID INT = @CustomerID SELECT * FROM Orders WHERE CustomerID = @LocalCustomerID ``` ## Azure SQL Database Considerations ### Performance Tiers - **Basic**: Limited resources, basic monitoring - **Standard**: Better performance, more monitoring - **Premium**: High performance, advanced features ### Monitoring Tools - **Query Store**: Automatic query performance tracking - **Azure Monitor**: Performance metrics and alerts - **Dynamic Management Views**: Real-time performance data ### Azure-Specific Optimizations 1. **Use Query Store** ```sql -- Enable Query Store ALTER DATABASE [YourDB] SET QUERY_STORE = ON ``` 2. **Monitor DTU Usage** - Track Database Transaction Units - Scale up during peak usage - Use auto-scaling features 3. **Optimize for Cloud** - Use connection pooling - Implement retry logic - Monitor network latency ## Best Practices ### Regular Maintenance 1. **Update Statistics** ```sql UPDATE STATISTICS TableName ``` 2. **Rebuild Indexes** ```sql ALTER INDEX ALL ON TableName REBUILD ``` 3. **Monitor Query Performance** - Use Query Store - Track slow queries - Set up alerts ### Development Guidelines 1. **Test with Real Data** - Use production-like data volumes - Test with various parameter values 2. **Use Execution Plans** - Always review plans for complex queries - Compare before/after optimization 3. **Monitor Performance** - Set up performance baselines - Track improvements over time ## Troubleshooting Common Issues ### High CPU Usage **Causes:** - Missing indexes - Inefficient queries - Parameter sniffing **Solutions:** - Add appropriate indexes - Rewrite problematic queries - Use query hints ### Memory Pressure **Causes:** - Large hash joins - Excessive sorting - Memory grants **Solutions:** - Optimize JOIN algorithms - Add indexes for ORDER BY - Monitor memory grants ### Blocking Issues **Causes:** - Long-running transactions - Lock escalation - Deadlocks **Solutions:** - Optimize transaction scope - Use appropriate isolation levels - Implement retry logic ## Advanced Analysis ### Reading XML Execution Plans The tool provides raw XML execution plans for detailed analysis: ```xml <ShowPlanXML> <BatchSequence> <Batch> <Statements> <StmtSimple StatementText="SELECT * FROM Orders"> <QueryPlan> <RelOp NodeId="0" PhysicalOp="Table Scan"> <OutputList> <ColumnReference Database="[DB]" Table="[Orders]" Column="OrderID"/> </OutputList> </RelOp> </QueryPlan> </StmtSimple> </Statements> </Batch> </BatchSequence> </ShowPlanXML> ``` ### Key XML Elements - **RelOp**: Relational operation - **PhysicalOp**: Physical operation type - **EstimatedTotalSubtreeCost**: Total cost - **EstimatedRows**: Estimated row count - **OutputList**: Columns returned ## Performance Monitoring ### Key Metrics to Track 1. **Query Duration** - Target: < 100ms for simple queries - Target: < 1s for complex queries 2. **CPU Usage** - Monitor spikes - Identify expensive operations 3. **Memory Usage** - Track memory grants - Monitor hash operations 4. **I/O Operations** - Logical reads - Physical reads - Page I/O ### Setting Up Monitoring ```sql -- Enable Query Store with optimal settings ALTER DATABASE [YourDB] SET QUERY_STORE = ON ( OPERATION_MODE = READ_WRITE, CLEANUP_POLICY = (STALE_QUERY_THRESHOLD_DAYS = 30), DATA_FLUSH_INTERVAL_SECONDS = 900, INTERVAL_LENGTH_MINUTES = 60, MAX_STORAGE_SIZE_MB = 1000, QUERY_CAPTURE_MODE = AUTO, SIZE_BASED_CLEANUP_MODE = AUTO ) ``` ## Conclusion Query execution plan analysis is essential for maintaining optimal SQL Server performance. By understanding execution plans and following optimization best practices, you can: - Identify performance bottlenecks - Optimize query performance - Reduce resource consumption - Improve application responsiveness Regular analysis and optimization will ensure your database performs efficiently as your application grows.