Hi SQL Experts,
I need some help in optimizing this SQL query, I need it to run faster.
I have already run out of ideas.
I am also a Developer and I know SQL pretty well, but maybe I have missed something that I could optimize here.
1) I have added all necessary indexes
2) I know that only needed columns should be selected, but I just added select all columns in the example bellow because I have a lot of columns there.
3) I have used "Include Actual Execution Plan".
I don't know what to do else. Please help.
Here is the code:
appointment AS APP
history AS HSTRY
ON HSTRY.id = APP.hc_srvc_intndd_dlvry_id
AND HSTRY.hstry_type_id = 2
AND APP.hc_srvc_prd_id IS NULL
(HSTRY.chng_dscrptn_id = 21
(HSTRY.chng_dscrptn_id = 18
(HSTRY.rsn_fr_chng_id <> 999
NOT EXISTS (SELECT 1
FROM history AS H
id = HSTRY.id
AND hstry_type_id = 2
AND chng_dscrptn_id = 21
AND status_time BETWEEN DATEADD(minute,-1,HSTRY.status_time) AND DATEADD(minute,1,HSTRY.status_time)))