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

Paul MaurielloSoftware Programmer Developer Analyst EngineerAsked:
Who is Participating?
 
Scott PletcherSenior DBACommented:
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) ...
0
 
Scott PletcherSenior DBACommented:
Make sure the udtfGetQ88VesselData function is an in-line tvf and not a multi-statement tvf.
0
 
Paul MaurielloSoftware Programmer Developer Analyst EngineerAuthor Commented:
Its a pretty sophisticated function, but ultimately returns 1 row can you elaborate?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Scott PletcherSenior DBACommented:
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.
0
 
Paul MaurielloSoftware Programmer Developer Analyst EngineerAuthor 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
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Are you having performance issues with this query?
If so, please post the query execution plan.
0
 
Paul MaurielloSoftware Programmer Developer Analyst EngineerAuthor 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
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Definitively we need to see the code of that function. The heavy load comes all from there.
0
 
Paul MaurielloSoftware Programmer Developer Analyst EngineerAuthor 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.