I was looking at the SSMS Tools Execution Plan Analyzer and it said to try breaking it up into smaller chunks. Is the following query the best way I can do “this”? Any help is much appreciated!
SELECT t1.ID,
t1.col2,
t1.col3,
t1.col4,
t2.ID AS table2ID,
t2.col2,
t1.col5,
t1.col6,
t2.StatusID as table2StatusID
CASE WHEN LEN(t2.ErrorMessage) > 0 THEN t3.StatusName + ' ' + t2.ErrorMessage
ELSE t3.StatusName
END AS SomeStatus,
t3.StatusTypeID AS StatusTypeID
FROM table1 t1
INNER JOIN table2 t2 ON t1.ID = t2.ID
INNER JOIN table3 t3 ON t2.StatusID = t3.StatusID`
You can replace your case when with an ISNULL() which might reduce the per row processing cost.
Without knowing a great deal about your schema setup it’s difficult to tell where optimizations can be made. What might be intuitive optimizations might go against business logic and be useless. The alternative is true, there could be some short cuts to getting the data that isn’t obvious without knowing the business logic.