Avatar of Paul Mauriello
Paul Mauriello
Flag 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

Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Paul Mauriello

8/22/2022 - Mon
Scott Pletcher

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

ASKER
Its a pretty sophisticated function, but ultimately returns 1 row can you elaborate?
Scott Pletcher

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Paul Mauriello

ASKER
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
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vitor Montalvão

Are you having performance issues with this query?
If so, please post the query execution plan.
Paul Mauriello

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Paul Mauriello

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