May 23, 2014 at 9:52 am
Hi,
I am working with a SP. I need to tune that. In that SP, they use several left outer joins and used filtering (where A.a=B.a and A.a=B.c or A.a is null and B.C=..).
We can't change the filtering. It may affect the business logic but I am trying to avoid the left outer joins.
If any one has experienced with this situation, I need advice how can we avoid left outer joins. I think we can use derived table or temp table to reduce the left outer joins.
May 23, 2014 at 9:54 am
what you posted is a catch all query.
here's an excellent place to start:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
Lowell
May 23, 2014 at 10:20 am
Hi Lowell,
Thanks for your reply. I am looking for the different one. I do have that where clauses but I am trying to avoid the left outer joins. I didn't find the good example for that.
May 23, 2014 at 10:26 am
Lowell (5/23/2014)
what you posted is a catch all query.here's an excellent place to start:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/
What is posted isn't a catch-all query, at least not a typical one. I see no variables in the code posted.
OP: A properly constructed LEFT JOIN CANNOT be eliminated to just some form of INNER JOIN. You CAN change it to TWO queries - one that is an INNER JOIN and another that uses some type of NOT EXISTS check. Those two could be UNION ALLed together. I don't know that that will be more efficient - almost certainly not because you are hitting at least some if not most data pages twice.
Can you post the full query and what you wish to do and why? What are the actual performance problems you are seeing and what have you done to identify that these LEFT JOINs are the actual culprit?
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
May 23, 2014 at 10:29 am
Hey,
I can't post the query here but I can change the names and I will post that.
May 23, 2014 at 10:51 am
TheSQLGuru (5/23/2014)
What is posted isn't a catch-all query, at least not a typical one. I see no variables in the code posted.
i'm under the impression that WHERE SomeColumn = SomeValue OR SomeColumn Is NULL is a catch all query syntax, so it doesn't matter if it's a variable or a column name, or am i missing something?
where A.a=B.a
and A.a=B.c
or A.a is null
Isn't that pretty close to Gails example?, ie
WHERE (ProductID = @product Or @product IS NULL)
Lowell
May 24, 2014 at 7:23 pm
I don't consider them at all the same, or even that similar, but that could be just me. The variable one is subject to parameter sniffing issues and has obvious short-cut refactoring that the strictly column-based one isn't/cannot.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
May 24, 2014 at 7:41 pm
Hi,
I am sorry.
Actually that filters are column names ColumnA in tableA=.. and or..
May 25, 2014 at 3:52 pm
Hi,
I think I can use outer apply to substitute left outer join, but I am not sure do we need to give column name like join
Suppose in left outer join
select colm1,colum2
from TableA A
left outer join TableB B
on A.colm1=B.col2.
For this above query how to write outer apply
May 25, 2014 at 4:18 pm
Lowell (5/23/2014)
i'm under the impression that WHERE SomeColumn = SomeValue OR SomeColumn Is NULL is a catch all query syntax, so it doesn't matter if it's a variable or a column name
No. A 'catch-all' query is one where the where clause has predicates of the specific form (Column = Parameter OR Parameter = Constant)
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 25, 2014 at 4:20 pm
ramana3327 (5/25/2014)
Hi,I think I can use outer apply to substitute left outer join, but I am not sure do we need to give column name like join
Suppose in left outer join
select colm1,colum2
from TableA A
left outer join TableB B
on A.colm1=B.col2.
For this above query how to write outer apply
Please don't post the same question in multiple places
http://www.sqlservercentral.com/Forums/Topic1574415-2799-1.aspx
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 26, 2014 at 3:31 pm
Sorry. The topic name is different so I kept in different place.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply