I've got the following View, called ViewGoods:
SELECT
G.Gid,
SI.[$Id] AS FirstSiteInId,
SI.Date AS FirstSiteInDate,
SI.Comments AS FirstSiteInComments,
S.[$Id] AS FirstSiteId,
S.[$Refex] AS FirstSiteRefex,
SI.Client AS ClientId,
C.[$Refex] AS ClientRefex,
CASE WHEN SI.Contract IS NULL THEN (SELECT Contract.[$Id]
FROM StockType AS ST
INNER JOIN StockTypeContract AS STC ON ST.[$Id] = STC.[$ParentId]
INNER JOIN Contract ON STC.Contract = Contract.[$Id]
WHERE ST.[$Id] = VGST.StockType
AND SI.Date >= STC.StartDate)
ELSE SI.Contract END AS Contract,
CASE WHEN SI.Contract IS NULL THEN (SELECT Contract.[$Refex]
FROM StockType AS ST
INNER JOIN StockTypeContract AS STC ON ST.[$Id] = STC.[$ParentId]
INNER JOIN Contract ON STC.Contract = Contract.[$Id]
WHERE ST.[$Id] = VGST.StockType
AND SI.Date >= STC.StartDate)
ELSE CT.[$Refex] END AS ContractRefex,
CASE WHEN COALESCE (Q.Quantity, 0) > 0 THEN L.SiteId ELSE NULL END AS SiteId,
CASE WHEN COALESCE (Q.Quantity, 0) > 0 THEN L.SiteRefex ELSE NULL END AS SiteRefex,
CASE WHEN COALESCE (Q.Quantity, 0) > 0 THEN L.Lid ELSE NULL END AS Lid,
ISNULL(W.Weight, VGSA.Weight * Q.Quantity) AS Weight,
COALESCE (Q.Quantity, 0) AS Quantity,
VGSA.Article,
VGSA.ArticleName,
VGST.StockType,
VGST.StockTypeRefex
FROM dbo.Goods AS G
INNER JOIN dbo.SiteIn AS SI ON G.SiteIn = SI.[$Id]
INNER JOIN dbo.Client AS C ON C.[$Id] = SI.Client
INNER JOIN dbo.Site AS S ON SI.Site = S.[$Id]
LEFT OUTER JOIN dbo.Contract AS CT ON SI.Contract = CT.[$Id]
LEFT OUTER JOIN dbo.ViewGoodsLocation AS L ON G.Gid = L.Gid
LEFT OUTER JOIN dbo.ViewGoodsWeight AS W ON G.Gid = W.Gid
LEFT OUTER JOIN dbo.ViewGoodsQuantity AS Q ON G.Gid = Q.Gid
LEFT OUTER JOIN dbo.ViewGoodsSingleArticle AS VGSA ON G.Gid = VGSA.Gid
LEFT OUTER JOIN dbo.ViewGoodsStockType AS VGST ON VGST.Gid = G.Gid
When querying that View with the parameter Client or the parameter Lid, individually, everything runs well. But if I try to mix the two of them, the View times out with no results. Below is the query getting the timeout:
SELECT [t0].[Gid], [t0].[FirstSiteInId], [t0].[FirstSiteInDate], [t0].[FirstSiteInComments], [t0].[FirstSiteId], [t0].[FirstSiteRefex], [t0].[ClientId], [t0].[ClientRefex], [t0].[Contract], [t0].[ContractRefex], [t0].[SiteId], [t0].[SiteRefex], [t0].[Lid], [t0].[Weight], [t0].[Quantity], [t0].[Article], [t0].[ArticleName], [t0].[StockType], [t0].[StockTypeRefex]
FROM [ViewGoods] AS [t0]
WHERE ([t0].[Lid] IS NOT NULL) AND (([t0].[ClientId]) = 70)
Where have I gone wrong?
EDIT: I included here the Actual Execution Plan http://pastebin.com/PMY0aLE1 .