Olá pessoal.
A minha query está com certa lentidão, levando em torno de 4 segundos para trazer os dados sendo que as tabelas possuem cerca de 15 registros apenas e o servidor é novo. Haveria alguma outra forma de reduzir essa latência editando a query?
SELECT y.bequa - y.jynx as totalizador
FROM (
SELECT X.yuni AS yuni_CTB,
X.carmt AS yuni_DESC,
( X.codempl + ' - ' + X.nominalemp ) AS breakinsd,
X.carmt AS carmtICAO,
Isnull(X.result3, 0) AS closte,
Isnull(X.result4, 0) AS ferfav,
(Isnull(X.result3, 0) + Isnull(X.result4, 0) )
AS loftp,
CASE WHEN Isnull(X.result3, 0) < Isnull(X.result4, 0) THEN 'C' ELSE 'D' END
AS bayner,
Isnull(X.result1, 0) AS klemad,
Isnull(X.result2, 0) AS jynx,
Abs((Isnull(X.result3, 0) + Isnull(X.result4, 0)) + (Isnull(X.result1, 0) - Isnull(X.result2, 0)))
AS bequa,
CASE WHEN Isnull(X.result3, 0) + Isnull(X.result1, 0) > Isnull(X.result4, 0) + Isnull(X.result2, 0)
THEN 'D' ELSE 'C' END AS troks
FROM (
SELECT DISTINCT O.codempl,
O.nominalemp,
(SELECT Sum(A.valorr)
FROM TPRECOS A
INNER JOIN TLANCAMENTOS F ON F.lak_des = A.lak_des
INNER JOIN TCADFORNEC K ON K.codempl = F.codempl
WHERE F.codempl = J.codempl
AND A.consr = E.consr
AND A.befo = E.befo
AND A.data < '20150101') AS result3,
(SELECT Sum(B.valorr)
FROM TPRECOS B
INNER JOIN TLANCAMENTOS G ON G.lak_des = B.lak_des
INNER JOIN TCADFORNEC L ON L.codempl = G.codempl
WHERE G.codempl = J.codempl
AND B.consr = E.consr
AND B.befo = E.befo
AND B.data < '20150101') AS result4,
(SELECT Sum(C.valorr)
FROM TPRECOS C
INNER JOIN TLANCAMENTOS H ON H.lak_des = C.lak_des
INNER JOIN TCADFORNEC M ON M.codempl = H.codempl
WHERE H.codempl = J.codempl
AND C.consr = E.consr
AND C.befo = E.befo
AND C.data >= '20150101'
AND C.data <= '20190828') AS result1,
(SELECT Sum(D.valorr)
FROM TPRECOS D
INNER JOIN TLANCAMENTOS I ON I.lak_des = D.lak_des
INNER JOIN TCADFORNEC N ON N.codempl = I.codempl
WHERE I.codempl = J.codempl
AND D.consr = E.consr
AND D.befo = E.befo
AND D.data >= '20150101'
AND D.data <= '20190828') AS result2,
E.befo AS wrap,
P.yuni AS yuni,
P.carmt AS carmt
FROM TPRECOS E
INNER JOIN TLANCAMENTOS J ON J.lak_des = E.lak_des
INNER JOIN TCADFORNEC O ON O.codempl = J.codempl
INNER JOIN TPLyuniBIL P ON P.wrap = E.befo
WHERE E.data >= '20150101'
AND E.data <= '20190828'
) X
) y
ORDER BY Y.totalizador ASC