I am with a performance problem in my query I'm need some tips for gain performance i tried with the UNION ALL but only won one seconds
S
ELECT GE.Id, V.Gid, V.EventOn, V.[Type], GE.SiteId, S.[$Refex] SiteRefex, V.Quantity, GE.IsIgnored
FROM GoodsEvent GE INNER JOIN
(/* 2. SI que gerem Quantity < 0*/ SELECT GE_SI_SO.Gid, GE_SI_SO.[Type], max(GE.EventOn) EventOn, GE_SI_SO.Quantity
FROM GoodsEvent GE INNER JOIN
(SELECT GGE.Gid, CASE WHEN SUM(GGE.Qtd) < 0 THEN 'SO' ELSE 'SI' END [Type], SUM(GGE.Qtd) Quantity
FROM (SELECT GE.Gid, CASE WHEN GE.[Type] = 'SI' THEN COUNT(GE.[Type]) ELSE 0 END nSI,
CASE WHEN GE.[Type] = 'SO' THEN COUNT(GE.[Type]) ELSE 0 END nSO, CASE WHEN GE.[Type] = 'SI' THEN SUM(GE.Quantity)
ELSE SUM(GE.Quantity) * - 1 END Qtd
FROM GoodsEvent GE
WHERE GE.IsDeleted = 0 AND GE.[Type] IN ('SI', 'SO')
GROUP BY GE.Gid, GE.[Type]) GGE
GROUP BY GGE.Gid
HAVING SUM(GGE.nSI) > SUM(GGE.nSO) + 1 OR
SUM(GGE.Qtd) < 0) GE_SI_SO ON GE.Gid = GE_SI_SO.Gid AND GE.[Type] = GE_SI_SO.[Type]
WHERE GE.IsDeleted = 0
GROUP BY GE_SI_SO.Gid, GE_SI_SO.Quantity, GE_SI_SO.[Type]
UNION
/* 1. Vários SI c/ ou s/ LO no meio*/ SELECT GE_BASE.Gid, 'SI' AS [Type], GE_Base.EventOn, 0 AS Quantity
FROM (SELECT ROW_NUMBER() OVER (ORDER BY GE.Gid, GE.EventOn) RowNumber, GE.Gid, GE.[Type], GE.EventOn
FROM GoodsEvent GE
WHERE GE.IsDeleted = 0) GE_BASE INNER JOIN
(SELECT ROW_NUMBER() OVER (ORDER BY GE.Gid, GE.EventOn) RowNumber, GE.Gid, GE.[Type]
FROM GoodsEvent GE
WHERE GE.IsDeleted = 0) GE_O ON GE_BASE.Gid = GE_O.Gid AND
GE_O.RowNumber = CASE GE_BASE.RowNumber WHEN 1 THEN 1 ELSE GE_BASE.RowNumber - 1 END
WHERE GE_BASE.RowNumber <> GE_O.RowNumber AND GE_BASE.[Type] = GE_O.[Type] AND GE_BASE.[Type] = 'SI' AND GE_O.[Type] = 'SI'
UNION
/* 3. LO sem SI a preceder*/ SELECT GE.Gid, 'LO' [Type], GE.EventOn, 0 Quantity
FROM GoodsEvent GE INNER JOIN
(SELECT GE.Gid, MIN(GE.EventOn) EventOn
FROM GoodsEvent GE
WHERE GE.IsDeleted = 0
GROUP BY GE.Gid) GGE ON GE.Gid = GGE.Gid AND GE.EventOn = GGE.EventOn
/*WHERE GE.[Type] = 'LO' AND GE.IsDeleted = 0*/ WHERE GE.[Type] <> 'SI' AND GE.IsDeleted = 0
UNION
/*4. IG Gids com Eventos de 'SI' Apos fecho de SiteIn */ SELECT GE.Gid, 'IG' [Type], GE.EventOn, 0 Quantity
FROM GoodsEvent GE INNER JOIN
(SELECT Gid, MIN(EventOn) AS EventOn
FROM GoodsEvent AS GE
WHERE GE.IsDeleted = 0
GROUP BY Gid) GGE ON GE.Gid = GGE.Gid AND GGE.EventOn = Ge.EventOn INNER JOIN
Goods G ON G.Gid = Ge.Gid INNER JOIN
SiteIn SI ON G.SiteIn = SI.[$Id] AND SI.Closed = 1 AND SI.ClosedOn < GE.EventOn
WHERE GE.IsDeleted = 0) V ON GE.Gid = V.Gid AND GE.EventOn = V.EventOn AND GE.IsDeleted = 0 INNER JOIN
[Site] S ON S.[$Id] = GE.SiteId