Improves query speed by rewriting SQL, adding proper indexes, and analyzing execution plans.
SQL Server’s query optimizer chooses the fastest execution plan for a statement. Tweaking indexes, statistics, and query design guides the optimizer toward cheaper plans and shorter runtimes.
Run SET SHOWPLAN_XML ON;
or click the "Actual Execution Plan" button in SSMS, then execute your query. SQL Server returns XML or graphical output showing operators, costs, and missing-index suggestions.
Add a non-clustered index when a column appears in WHERE, JOIN, or ORDER BY clauses and the query filters on fewer than 30–40% of rows. Cover queries by including frequently selected columns in the index.
CREATE NONCLUSTERED INDEX IX_Orders_CustDate ON Orders(customer_id, order_date) INCLUDE(total_amount);
Use DMVs sys.dm_db_missing_index_details
, sys.dm_db_missing_index_groups
, and sys.dm_db_missing_index_group_stats
. Sort by avg_total_user_cost * avg_user_impact * user_seeks
to prioritize high-impact recommendations.
A SARGable predicate lets SQL Server seek rather than scan. Avoid functions on indexed columns, implicit conversions, and "<>" or "OR" across different columns.
-- BadSELECT * FROM Customers WHERE YEAR(created_at)=2023;-- GoodSELECT * FROM Customers WHERE created_at >= '2023-01-01' AND created_at < '2024-01-01';
1) Keep statistics current (UPDATE STATISTICS
). 2) Remove unused indexes. 3) Use appropriate data types and lengths. 4) Limit result sets with SELECT list and pagination. 5) Measure with SET STATISTICS IO, TIME ON
.
Using SELECT * forces wider I/O and prevents covering indexes. List needed columns instead.
Over-indexing slows writes and bloats storage. Add indexes only for frequent, high-cost queries.
Sometimes. Use OPTION (RECOMPILE) or optimized plan guides when data skew causes bad cached plans.
For high-throughput OLTP apps, it reduces compilation overhead but may hide sub-optimal plans. Test first.
Daily for volatile tables or after loading ≥20% new rows; otherwise, auto-update is usually sufficient.
Yes. Filtered indexes cover only rows that meet a predicate, reducing size and improving seek speed for highly selective queries.
It avoids locking but allows dirty reads and phantom rows. Use read-committed snapshot isolation instead for consistent, non-blocking reads.
Parameterize queries, avoid OPTION (RECOMPILE) unless necessary, and keep schema-binding stable to prevent unnecessary recompiles.