Re: how to improve performance of 'LEFT JOIN'



lelandhuang@xxxxxxxxx (lelandhuang@xxxxxxxxx) writes:
I am developing reporting service and using lots of 'LEFT OUTER JOIN',
I am worried about the performance and want to use some subquery to
improve
the performance.
Could I do that like below,

[the origin source]
SELECT *
FROM TableA
LEFT OUTER JOIN TableB
ON TableA.item1 = TableB.item1
WHERE TableA.item2 = 'xxxx'
TableB.item2 > yyyy AND TableB.item2 < zzzz

I add the subquery to query every table before 'LEFT JOIN'
--------------------------------------------------------------------------
SELECT *
FROM
(SELECT *
FROM TableA
WHERE TableA.item2 = 'xxxx'
) TableC
LEFT OUTER JOIN
(SELECT *
FROM TableB
WHERE TableB.item2 > yyyy AND TableB.item2 < zzzz
) TableD
ON TableC.item1 = TableD.item1
WHERE TableC.item2 = 'xxxx'
TableD.item2 > yyyy AND TableD.item2 < zzzz
--------------------------------------------------------------------------

This is a meaningless rewrite of the query, that at worst could server
to confuse the optimizer to give you a worse query plan. At best, the
optimizer will recast the second query into the first.

As Tom notes, the outer join is probably not correctly written. Assuming
that the query should read:

SELECT *
FROM TableA
LEFT OUTER JOIN TableB
ON TableA.item1 = TableB.item1
AND TableB.item2 > yyyy AND TableB.item2 < zzz
WHERE TableA.item2 = 'xxxx'

The most important for the query to perform well, is that you have a
clustered index on TableA.item2 and an index (clustered or non-clustered)
on TableB.item1.

--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: CONTAINS performance
    ... How can I predict the scalability of the simple query given that I don't ... but will measure the FTS with multiple clients issuing random FTS queries. ... B on A.bid=B.id left outer join ... the SQL Server query optimizer executes this query in the optimal manner ...
    (microsoft.public.sqlserver.fulltext)
  • Re: *=
    ... > from customers c, orders o, items i ... As Marek said *= is an old syntax for outer join. ... the query returns 5 rows. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: How to query with both JOIN and aliases in Access?
    ... v.empID LEFT OUTER JOIN FirstDayWorked FROM ... This query performs in SQL Server without any problem. ... LEFT OUTER JOIN (SELECT empID, ...
    (microsoft.public.access.queries)
  • Re: combining inner and outer joins
    ... the query that works for you in Sybase, and the output you're looking for. ... > I've recently switched from Sybase to SQL Server, ... > of an outer join clause. ...
    (microsoft.public.sqlserver.programming)
  • RE: Preformance issue
    ... I'm not going to paste code for you but I am going to tell you that you will ... a join on your subquery as a table, in is effectively an "or" and is slow. ... > I’m currently working with SQL server and I’m SQL query generator that work ... In the last days im working on using my SQL query ...
    (microsoft.public.sqlserver.programming)