Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
learntsql
524 Posts |
Posted - 2009-12-16 : 04:02:13
|
| Hi..I have few queries to ask may be basic.1.When i write JOIN clause in select statement where should i apply the filter;whether after ON clause or WHERE clause for all kind of joins.2.If column is computed column with alias name and have order by clause in same statementShould i write computed column again in order by clause or alias name is enough as per performance.eg: select datediff(DD,EndDate,GetDate()) AS Duration FROM Tasks ORDER BY datediff(DD,EndDate,GetDate()) (OR) ORDER BY Durationwhich one is best.3.If SP is used for Report generation should i order the records in query itself using ORDER BY clause or do in Reporting service?any links for above.Thanks in advance. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-16 : 04:12:17
|
| 1 It depends on what you want. in INNER join both are same. In OUTER joins both will give different result2 Performancewise there is no difference3 Sort data in ReportMadhivananFailing to plan is Planning to fail |
 |
|
|
learntsql
524 Posts |
Posted - 2009-12-16 : 04:18:51
|
| Thanks Madhi,Could you please elaborate the first one.(Join clause)any stuff related to this. |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-12-16 : 04:21:43
|
| Using Order by If records are already sorted by an index, the processor needs to use only the index. Otherwise, the processor has to use a temporary work table to sort the records first. Such preliminary sorting can cause significant initial delays.-------------------------R... |
 |
|
|
denis_the_thief
Aged Yak Warrior
596 Posts |
Posted - 2009-12-16 : 11:41:53
|
| #1I guess in most cases, no difference performance-wise but look at the Execution Plans. I tried a simple query and wrote it 2 ways: 1st with WHERE and 2nd with JOIN/ON. I looked at both Execution Plans and they were identical meaning when it comes time to run the query, SQL Server's game plan in retrieving the results would be the same and therefore equal performance.But be careful with left-outer joins involving NULLs, the results will be different between the 2!#2No difference. SQL Server is smart enough to see that the Order Bys are equivalent.#3Hard to say from a Performance point of view. Probably pretty similar either way. But I do agree with Madhivanan since the sort order is more of a presentation issue so makes more sense in the Report. And that way you could have more than one Report feeding off the same Stored Proc and each Report could sort the Data how they wish. |
 |
|
|
learntsql
524 Posts |
Posted - 2009-12-16 : 23:42:51
|
| Thanks denis,still not clear with #1(with outer joins).Could you plz. provide some stuff for my better understand or any links.I want to know how the select statement behaves(executes) when i use join and whare clauses.TIA. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-17 : 01:55:05
|
quote: Originally posted by learntsql Thanks denis,still not clear with #1(with outer joins).Could you plz. provide some stuff for my better understand or any links.I want to know how the select statement behaves(executes) when i use join and whare clauses.TIA.
Run these twoselect t.* from pubs..titles as t left join pubs..titleauthor as taon t.title_id=ta.title_idwhere t.type='business'select t.* from pubs..titles as t left join pubs..titleauthor as taon t.title_id=ta.title_idand t.type='business'and see the resultMadhivananFailing to plan is Planning to fail |
 |
|
|
learntsql
524 Posts |
Posted - 2009-12-17 : 05:47:39
|
| Thanks Madhi,Sorry but still i didnt understandhow it internally executed.i.e,First Where clause followed by Join clause or vice versa.Could you please expalin execution steps for both LEFT and RIGHT joins.I am working on SQL 2005.I tried as followsdeclare @tab1 table(ID int identity,type varchar(50))declare @tab2 table(ID int identity,type varchar(50))insert into @tab1select 'A'unionselect 'B'insert into @tab2select 'A'unionselect 'B'unionselect 'C'select t.*from @tab1 as tright join @tab2 as taon t.id=ta.idwhere t.type='A'select t.*from @tab1 as tright join @tab2 as taon t.id=ta.idAND t.type='A' |
 |
|
|
denis_the_thief
Aged Yak Warrior
596 Posts |
Posted - 2009-12-17 : 09:46:06
|
quote: Originally posted by learntsql Thanks denis,still not clear with #1(with outer joins).Could you plz. provide some stuff for my better understand or any links.I want to know how the select statement behaves(executes) when i use join and whare clauses.TIA.
Try these 2 queries and you'll get 2 different results:1:with T (C1, C2) as (select 1, 3),T2(C2, C3) as (select 3, NULL)select * from T left outer join T2 on T.C2 = T2.C2where T2.C3 = 3 2:with T (C1, C2) as (select 1, 3),T2(C2, C3) as (select 3, NULL)select * from T left outer join T2 on T.C2 = T2.C2 and T2.C3 = 3 |
 |
|
|
|
|
|
|
|