My new View query is taking long for execution as compared to the actual table query.

Mahesh Kharawatkar 0 Reputation points
2023-03-29T13:22:15.2266667+00:00

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.execution_Plan

SQL Server Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-03-29T21:33:23.33+00:00

    I get the impression that you have a so-called EAV design. EAV = Entity-AttributeValue.

    Such designs have their place, as they permit a flexible schemas, and new attributes can easily be added.

    However, they are not always the right choice. In many cases, it is better to make a "real" table design where the attribute are columns and not rows. This means that a misspelling of an attribute will yield an error and not just no row being returned. And moreover, EAV often leads to poor performance, because of all the joins you need to make.

    So I would suggest that this performance issue is a wakeup call that it's time to make a redesign.

    0 comments No comments

  2. LiHongMSFT-4306 31,566 Reputation points
    2023-03-30T03:12:57.2633333+00:00

    Hi @Mahesh Kharawatkar

    The normal query which is running fine within10 seconds. My New query with View which is taking more than 5 minutes for execution.

    Then why you use view as source dataset?

    How about use temporary table instead of view?

    Best regards,

    Cosmog Hong


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.