CodexBloom - Programming Q&A Platform

SQL Server: implementing Index Usage and Query Performance in a Complex Join Scenario

πŸ‘€ Views: 293 πŸ’¬ Answers: 1 πŸ“… Created: 2025-07-03
sql-server performance indexing sql

I'm trying to implement I'm relatively new to this, so bear with me... I'm working with performance optimization with a query that involves multiple joins across several tables in SQL Server 2019. The query is taking an unusually long time to execute, and I suspect that it's not utilizing the indexes I created appropriately. Here’s the query that I have: ```sql SELECT a.*, b.Description, c.TotalAmount FROM Orders a JOIN Customers b ON a.CustomerID = b.CustomerID JOIN Payments c ON a.OrderID = c.OrderID WHERE b.Country = 'USA' AND c.PaymentDate >= '2023-01-01' ORDER BY c.TotalAmount DESC; ``` I've created indexes on the `CustomerID` and `OrderID` fields as follows: ```sql CREATE INDEX idx_CustomerID ON Customers(CustomerID); CREATE INDEX idx_OrderID ON Orders(OrderID); CREATE INDEX idx_PaymentDate ON Payments(PaymentDate); ``` Despite these indices, when I check the execution plan, it shows that the query is performing a table scan on the `Orders` table instead of using the index on `OrderID`. Additionally, I’m noticing the following behavior in the execution plan: "Missing Index Suggestion: A nonclustered index on 'Orders' could improve performance." I’ve tried updating statistics and even rebuilding the indexes, but the performance hasn't improved significantly. I also ran the `DBCC SHOW_STATISTICS` command to check the distribution of the `CustomerID` and `OrderID` columns, and the statistics seem reasonable. Can anyone suggest why my indexes are not being used as expected? Are there additional techniques I could employ to diagnose and resolve this performance scenario? Any insights would be greatly appreciated! For context: I'm using Sql on Ubuntu 20.04. What are your experiences with this?