Link to home
Start Free TrialLog in
Avatar of Paul Mauriello
Paul MaurielloFlag for United States of America

asked on

At a casual glance is there any way I can improve these SQL Joins?

Most notably the outer apply join that returns 1 row per call from a tabular function that crosses databases


SELECT a.Field1,
              b.Field2,
              c.Field3,
              d.Field4,
              e.Field5,
              f.Field6
		FROM
			[dbo].[vwVessel] a
			JOIN [dbo].[vwVesselOpenPosition] b ON b.VesselID = a.VesselID
			JOIN [dbo].[vwPort] c ON c.PortID=b.PortID
			JOIN [dbo].[tbl_Ref_Port] d on d.PortID = c.PortID
			LEFT JOIN [dbo].[vwUserDictionary] e ON e.UserDictionaryID = d.AreaUserDictionaryID
			OUTER APPLY [dbo].[udtfGetQ88VesselData](a.VesselName, a.IMO) f 
			LEFT JOIN [dbo].[udtfGetUserVesselList](@paramUserID) g on g.VesselID = a.VesselID
		WHERE
			ISNULL(b.IsHide,0)=0
			AND a.UserCompanyID = @paramUserCompanyID
			AND (@paramIsMyVessel = 0 OR g.VesselID IS NOT NULL)

Open in new window

Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Make sure the udtfGetQ88VesselData function is an in-line tvf and not a multi-statement tvf.
Avatar of Paul Mauriello

ASKER

Its a pretty sophisticated function, but ultimately returns 1 row can you elaborate?
The function should be a single RETURN ( ...query... ) statement, with as complex a query as needed to produce the final result.  Those types of functions are vastly more efficient than those that construct a table variable and return it to the caller.
It already does. Is the Outer Apply the best way to go? I tried switching it to a left join but then I got the "unique identifier could not be bound error" on the params I am passing into the function
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you having performance issues with this query?
If so, please post the query execution plan.
Here is the overall execution plan. Looks fine until it tries to reach over into the other database. Wasn't sure if Outer Apply was the best way to go. But if that's the best I can do given the fact its crossing databases, then that's the best I can do. I attached the file
PositionListExecutionPlan.sqlplan
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for all your help. The function is the one your both previously helped me optimize. So I think we have come full circle. Until we do a redesign of that database, I believe that is the best we can do for now.