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,
			[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
			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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
Make sure the udtfGetQ88VesselData function is an in-line tvf and not a multi-statement tvf.
Paul MaurielloSoftware Programmer Developer Analyst EngineerAuthor Commented:
Its a pretty sophisticated function, but ultimately returns 1 row can you elaborate?
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.
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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

                  (b.IsHide IS NULL OR b.IsHide = 0) ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Are you having performance issues with this query?
If so, please post the query execution plan.
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
Vitor MontalvãoMSSQL Senior EngineerCommented:
Definitively we need to see the code of that function. The heavy load comes all from there.
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.