I have the following query:
declare @jobID NVARCHAR(20)
declare @dataGroup BIGINT
declare @lastSyncVer BIGINT
set @jobid='P-0001'
set @dataGroup= 5637144576
set @lastSyncVer = 316244734
SELECT us.[DATASTOREID], us.[UPLOADSESSIONID], us.[UPLOADPATH], us.[STATUS], us.[DATEUPLOADED], us.[DATECREATED], us.[CHECKSUM], us.[FILESIZE], us.[ID], us.[RERUNFOR]
FROM [dbo].[UPLOADSESSION] us
INNER JOIN CHANGETABLE(CHANGES [dbo].[UPLOADSESSION], @lastSyncVer) AS CT ON us.[ID] = CT.[ID]
INNER JOIN [dbo].[DATASTORE] ds ON us.[DATASTOREID] = ds.[ID]
INNER JOIN [dbo].[DATAGROUP] dg ON ds.[DATAGROUPID] = dg.[ID]
WHERE dg.[ID] = @dataGroup
AND us.[JOBID] = @jobID
AND us.[RERUN] = 0
ORDER BY us.[ID]
When I run a query cost analyze of the query I see that the Hash Match takes up 83% of the cost. I also see that the hash match has a really high number of executions(over 800k). The main problem with the query is that it is very slow. I have let it run for over 40 minutes, and it still didn't finish. The query is written by Microsoft, and is part of their functionally in their ERP system Microsoft Dynamics AX. Can anyone come up with some ideas how we can speed things up? This query used to take under 30 seconds.
Perhaps the problem isn't in the query itself, but in other problems with the DB? I have tried updating stats.