I have a select query that I need to improve to make it fast.
SELECT
st.SigHistUID, st.RelationType, st2.RelationType,
case
when st.RelationType <> st2.RelationType
then 'N'
else st.RelationType
end as [NewRelationType],
st.ToSigUID , sh.FullDescription, sh.SigHistUID AS ToSigHistUID
FROM
[Stackability] (nolock) st
INNER JOIN
[SignatureHistory] (nolock) sh ON sh.SigUID = st.ToSigUID
AND sh.SigHistUID = (SELECT TOP 1 SigHistUID FROM [SignatureHistory] (nolock) tmp where tmp.SigUID = sh.SigUID ORDER BY SigHistUID DESC)
INNER JOIN
[SignatureHistory] (nolock) sh2 ON st.SigHistUID = sh2.SigHistUID
INNER JOIN
Stackability (nolock) st2 on st2.ToSigUID = sh2.SigUID and st2.SigHistUID = sh.SigHistUID
WHERE
st.SigHistUID < 1000 and
st.RelationType <> st2.RelationType
ORDER BY
st.SigHistUID