CodexBloom - Programming Q&A Platform

How can I optimize a T-SQL query that uses multiple LEFT JOINs and is running extremely slow?

πŸ‘€ Views: 325 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-13
sql-server t-sql performance sql

I'm collaborating on a project where I'm a bit lost with I'm getting frustrated with I've encountered a strange issue with I'm working with performance optimization with a T-SQL query that involves multiple `LEFT JOIN`s across several tables. The execution time for this query has been over 30 seconds, which is unacceptable for my application. Here’s a simplified version of my query: ```sql SELECT a.ID, a.Name, b.Description, c.Amount FROM TableA a LEFT JOIN TableB b ON a.ID = b.A_ID LEFT JOIN TableC c ON a.ID = c.A_ID WHERE a.Status = 'Active' AND b.Date > '2022-01-01' ``` I've checked the execution plan and noticed that the `TableB` join is particularly costly, even though it has indexes on `A_ID` and `Date`. I've tried rewriting the query to use `INNER JOIN` for certain conditions, but that altered the results. Additionally, I've ensured that statistics are updated for all involved tables, but the performance has not improved. I suspect that the scenario might be related to how SQL Server is handling the joins, especially with NULL values in `TableB` that could be causing unnecessary processing. I also considered breaking the query into smaller parts using temporary tables, but I'm unsure if that would actually yield better performance. What strategies could I employ to optimize this query while preserving the left join logic? I'm using SQL Server 2019, and the database is relatively small (around 100,000 rows across the tables), so I'm puzzled by the slow performance. This issue appeared after updating to Sql 3.9. I'm coming from a different tech stack and learning Sql. This is happening in both development and production on Linux. My development environment is Linux. I'd really appreciate any guidance on this.