The normal query which is running fine within10 seconds.
SELECT productUri.AttributeValue AS ProductUri, orderUri.AttributeValue AS OrderUri, COUNT (*) OVER () AS TotalResultsCount, productUri.GroupId AS GroupId FROM OrdersLookup AS productUri INNER JOIN OrdersLookup AS orderUri ON orderUri.GroupId=productUri.GroupId AND orderUri.Attribute='OrderUri' INNER JOIN OrdersLookup AS isRootInOrder ON isRootInOrder.GroupId=productUri.GroupId AND isRootInOrder.Attribute='IsRootInOrder' INNER JOIN OrdersLookup AS alternateIdentifier ON alternateIdentifier.GroupId=productUri.GroupId AND alternateIdentifier.Attribute='AlternateIdentifier' INNER JOIN OrdersLookup AS collectionTicketReference ON collectionTicketReference.GroupId=productUri.GroupId AND collectionTicketReference.Attribute='CollectionTicketReference' LEFT JOIN OrdersLookup AS fareCategoryClassification ON fareCategoryClassification.GroupId=productUri.GroupId AND fareCategoryClassification.Attribute='FareCategoryClassification' INNER JOIN OrdersLookup AS departureDateUtc ON departureDateUtc.GroupId=productUri.GroupId AND departureDateUtc.Attribute='DepartureDateUtc' WHERE productUri.Attribute = 'ProductUri' AND (isRootInOrder.AttributeValue = 'True' AND (alternateIdentifier.AttributeValue = 'two' OR alternateIdentifier.AttributeValue = 'four' AND collectionTicketReference.AttributeValue = '356d6e9c-2681-47a8-9863-94dc27e1e583') AND (fareCategoryClassification.AttributeValue IS NULL OR fareCategoryClassification.AttributeValue<>'season')) GROUP BY productUri.AttributeValue, orderUri.AttributeValue, productUri.GroupId ORDER BY MIN(departureDateUtc.AttributeValue) ASC OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
My New query with View which is taking more than 5 minutes for execution.
SELECT productUri.AttributeValue AS ProductUri, orderUri.AttributeValue AS OrderUri, COUNT (*) OVER () AS TotalResultsCount, productUri.GroupId AS GroupId
FROM v_OrdersLookup_New AS productUri
INNER JOIN v_OrdersLookup_New AS orderUri ON orderUri.GroupId=productUri.GroupId AND orderUri.Attribute='OrderUri'
INNER JOIN v_OrdersLookup_New AS isRootInOrder ON isRootInOrder.GroupId=productUri.GroupId AND isRootInOrder.Attribute='IsRootInOrder'
INNER JOIN v_OrdersLookup_New AS alternateIdentifier ON alternateIdentifier.GroupId=productUri.GroupId AND alternateIdentifier.Attribute='AlternateIdentifier'
INNER JOIN v_OrdersLookup_New AS collectionTicketReference ON collectionTicketReference.GroupId=productUri.GroupId AND collectionTicketReference.Attribute='CollectionTicketReference'
LEFT JOIN v_OrdersLookup_New AS fareCategoryClassification ON fareCategoryClassification.GroupId=productUri.GroupId AND fareCategoryClassification.Attribute='FareCategoryClassification'
INNER JOIN v_OrdersLookup_New AS departureDateUtc ON departureDateUtc.GroupId=productUri.GroupId AND departureDateUtc.Attribute='DepartureDateUtc'
WHERE productUri.Attribute = 'ProductUri'
AND (isRootInOrder.AttributeValue = 'True' AND (alternateIdentifier.AttributeValue = 'two' OR alternateIdentifier.AttributeValue = 'four' AND collectionTicketReference.AttributeValue = '46184dba-134d-4586-b1f6-5a84229674d1') AND (fareCategoryClassification.AttributeValue IS NULL OR fareCategoryClassification.AttributeValue<>'season'))
GROUP BY productUri.AttributeValue, orderUri.AttributeValue, productUri.GroupId
ORDER BY MIN(departureDateUtc.AttributeValue) ASC
OFFSET 0 ROWS FETCH NEXT 20 ROWS ONLY
My View structure is as below.
Create view [dbo].[v_Orderslookup_New] as
SELECT [GroupId]
,[Attribute]
,[AttributeValue]
FROM [c22_OrderSearchService].[dbo].[Orderslookup]
union all
SELECT [GroupId]
,[Attribute]
,[AttributeValue]
FROM [c22_OrderSearchService].[dbo].[Orderslookup_New]
GO
The execution plan has HAsh Match and multiple concatenations. The estimation and actual row value is differing may be update statistics will help. however any other suggestions considering the view structure and the query written. When I used union in the view the query time reduced to 50 seconds but still 50 seconds is a huge time.