We help IT Professionals succeed at work.

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

Comment
Watch Question

Scott PletcherSenior DBA
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Make sure the udtfGetQ88VesselData function is an in-line tvf and not a multi-statement tvf.
Paul MaurielloSoftware Programmer Developer Analyst Engineer

Author

Commented:
Its a pretty sophisticated function, but ultimately returns 1 row can you elaborate?
Scott PletcherSenior DBA
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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.
Paul MaurielloSoftware Programmer Developer Analyst Engineer

Author

Commented:
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
Senior DBA
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Nothing wrong with an OUTER APPLY per se.  I would have to see at least all the code and a query plan to tune the query, as well as what indexes were on all tables.

You should get rid of the ISNULL() in the WHERE; luckily there is an easy rule for that one:

NEVER use ISNULL() in a WHERE or JOIN.
...
            WHERE
                  (b.IsHide IS NULL OR b.IsHide = 0) ...
Vitor MontalvãoIT Engineer
Distinguished Expert 2017

Commented:
Are you having performance issues with this query?
If so, please post the query execution plan.
Paul MaurielloSoftware Programmer Developer Analyst Engineer

Author

Commented:
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
Vitor MontalvãoIT Engineer
Distinguished Expert 2017
Commented:
Definitively we need to see the code of that function. The heavy load comes all from there.
Paul MaurielloSoftware Programmer Developer Analyst Engineer

Author

Commented:
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.