Hi GuysI have a query that takes around 1 second to run until I join another table then it takes around 6 minutes.There is nothing unusual about the table I am joining, it joins without drama in other queries and I have indexed the buggery out of it. The query is below with the problem join to the OwnershipRoleDiscipline table commented out at the end. I have the query plans but cannot see how to attach them to a post (I am new here). One thing I notice from looking at them is that the Key Lookup for the ClientProductProfile table uses 25% of resources for the slow query and 3% for the fast query, but it is not actually even involved in the problem join. Somehow the join seems to have thrown the whole execution plan haywire and I do not know how to fix it. Any ideas would be appreciated.RegardsDavidSELECT cpa.ClientID, p2.Description AS Product, ppc2.ProfileColour FROM profile.ProfileValueColour ppc JOIN Profile.ClientProductProfile cpa ON cpa.ProfileValue = ppc.Actual AND cpa.ProfileTypeID = 13 AND ppc.ProductID = cpa.ProductID JOIN Profile.ClientProductProfile cpp ON cpp.ProductID = cpa.ProductId AND cpp.ProfileValue = ppc.Potential AND cpp.ProductID = ppc.ProductID AND cpa.clientid = cpp.ClientID AND cpp.ProfileTypeID = 14 JOIN dbo.Product p ON p.ProductID = ppc.ProductID join Profile.ProfileProductRule ppr on ppc.ownershiproleid = ppr.ownershiproleid and ppc.productid = ppr.productid AND ppc.ForDisplay = 1 AND ppc.ownershiproleid = 1 JOIN Profile.ClientProductProfile cpa2 ON cpa.clientid = cpa2.ClientID AND cpa2.ProfileTypeID = 13 JOIN Profile.ClientProductProfile cpp2 ON cpa2.clientid = cpp2.ClientID AND cpp2.ProfileTypeID = 14 JOIN profile.ProfileValueColour ppc2 ON ppc2.ProductID = cpa2.ProductID AND ppc2.ProductID = cpp2.ProductID AND cpp2.ProfileValue = ppc2.Potential AND cpa2.ProfileValue = ppc2.Actual AND cpp2.ProductID = ppc2.ProductID AND ppc2.OwnershipRoleID = 1 JOIN product p2 ON ppc2.ProductID = p2.ProductID JOIN dbo.OwnershipPosition op ON op.OwnershipRoleID = ppc.OwnershipRoleID JOIN dbo.CompanyPosition cp ON cp.CompanyPositionID = op.CompanyPositionID AND op.OwnershipPositionID = 92 JOIN dbo.OwnershipRole owr ON owr.OwnershipRoleID = op.OwnershipRoleID JOIN dbo.Geography g1 ON g1.GeographyID = cp.GeographyID AND g1.GeographyTypeID = 6 JOIN dbo.GeographyHierarchy gh1 ON g1.GeographyID = gh1.ParentGeographyID AND gh1.GeographyHierarchyTypeID = owr.GeographyHierarchyTypeID JOIN dbo.Geography g2 ON g2.GeographyID = gh1.ChildGeographyID AND g2.GeographyTypeID = 7 JOIN dbo.GeographyHierarchy gh2 ON g2.GeographyID = gh2.ParentGeographyID AND gh2.GeographyHierarchyTypeID = owr.GeographyHierarchyTypeID JOIN dbo.Geography g3 ON g3.GeographyID = gh2.ChildGeographyID AND g3.GeographyTypeID = 8 JOIN dbo.Address a ON a.GeographyBrickReferenceID = g3.ReferenceID JOIN client c ON c.PrimaryAddressId = a.AddressID AND a.AddressStatusID > 0 AND c.ClientStatusID > 0 AND cpa.clientid = c.ClientID JOIN dbo.Person per ON per.PersonID = c.ClientID AND per.StatusID > 0 --JOIN dbo.OwnershipRoleDiscipline ord --ON ord.DisciplineID = c.DisciplineID --AND ord.OwnershipRoleID = owr.OwnershipRoleID --AND ord.SRAGeographyID = g2.GeographyID
↧