this a poor performancing query I have ... what have I done so wrong? Please help me it is executed tons of times in my system, solving that will give me a ladder to heaven
I gave a check on the system with sp_Blitz and no mortal issues found
Here is the query :
SELECT MAX(F.id) OVER (PARTITION BY idstato ORDER BY F.id DESC) AS id
FROM jfel_tagxml_invoicedigi F
INNER JOIN jfel_invoice_state S ON F.id = S.idinvoice
WHERE S.idstato = @idstato
AND S.id = F.idstatocorrente
AND F.sequence_invoice % @number_service_installed = @idServizio
ORDER BY F.id DESC,
F.idstatocorrente OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;
Here is the query plan
https://www.brentozar.com/pastetheplan/?id=SyYL5JOeE
I can send you privately my system properties
update: Made some modification , it is better , but I think it could be better ... here is the new query :
SELECT MAX(F.id) AS id
FROM jfel_tagxml_invoicedigi F
INNER JOIN jfel_invoice_state S ON F.id = S.idinvoice
WHERE S.idstato = @idstato
AND S.id = F.idstatocorrente
AND F.sequence_invoice % @number_service_installed = @idServizio;
And the new plan: https://www.brentozar.com/pastetheplan/?id=SJ-5GDqeE
update: Made some modification , it is better , but I think it could be better ... here is the new query :
SELECT top 1 F.id as id
FROM jfel_tagxml_invoicedigi AS F
INNER JOIN jfel_invoice_state AS S
ON F.idstatocorrente = S.id
WHERE S.idstato= 1 AND S.id = F.idstatocorrente
and S.datastato > dateadd(DAY,-5,getdate())
AND F.progressivo_fattura % 1 = 0
ORDER BY S.datastato
And the new new plan https://www.brentozar.com/pastetheplan/?id=S1xRkL51S
SELECT MAX(F.id)
and bin theORDER BY
andOFFSET
? AMAX
of aMAX
is still theMAX
; I see no reason why you need to calculate theMAX
value byidstato
and then get theMAX
value of those. – Pincenez