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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Which type of query is more efficient?

Author  Topic 

biznick
Starting Member

2 Posts

Posted - 2008-02-15 : 10:00:40
Lets say I have two tables that are very large that I'm going to do a left join on. If I only want to get a couple of columns out of each table which type of query would be more efficient?

A)

Select
ColumnsofInterest
From
TableOne Left Join
TableTwo On XYZ

B)

Select
ColumnsofInterest
From
(Select ColumnsOfInterestInTableOne From TableOne) Left Join
(Select ColumnsOfInterestInTableTwo From TableTwo) On XYZ

Thank you in advance for the assistance :)







harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-02-15 : 10:18:08
I don't believe there would be any difference in terms of performance for either query.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

biznick
Starting Member

2 Posts

Posted - 2008-02-15 : 10:26:51
Thanks for the Help harsh athalye!
Go to Top of Page

Qualis
Posting Yak Master

145 Posts

Posted - 2008-02-15 : 11:09:00
I did a comparison for you with real numbers. The execution plans are the same except on B, there is an extra Compute Scalar. This is for tables WITH NO INDEXES.

-- Generate Test Data
-- Make the Column of Inerest different data for each
-- And TableOne have more data than TableTwo
SET STATISTICS IO Off
SET STATISTICS TIME Off

Drop Table #TableOne
Drop Table #TableTwo

Create Table #TableOne (id int, ColumnOfInterest varchar(50))
Create Table #TableTwo (id int, ColumnOfInterest varchar(50))

Declare @cnt int
Set @cnt = 0

While @cnt < 1000000 Begin
Insert Into #TableOne Select @cnt, NewID()
If @cnt % 4 = 0 Insert Into #TableTwo Select @cnt, NewID()
Set @cnt = @cnt + 1
End

Now I run my tests.

--Turn statistics on so you can see how each performs
SET STATISTICS IO ON
SET STATISTICS TIME ON

--Run this before every query execution so you get accurate results
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE


Select
T1.ColumnOfInterest, T2.ColumnOfInterest
From
#TableOne T1
Left Join #TableTwo T2 On T1.id = T2.id

/*
SQL Server parse and compile time:
CPU time = 46 ms, elapsed time = 66 ms.

(1000000 row(s) affected)
Table '#TableTwo'. Scan count 3, logical reads 1713, physical reads 0, read-ahead reads 0,
lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table '#TableOne'. Scan count 3, logical reads 6850, physical reads 0, read-ahead reads 0,
lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0,
lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 2922 ms, elapsed time = 14848 ms.
*/

Select
T1.ColumnOfInterest, T2.ColumnOfInterest
From
(Select id, ColumnOfInterest From #TableOne) T1
Left Join (Select id, ColumnOfInterest From #TableTwo) T2 On T1.id = T2.id

/*
SQL Server parse and compile time:
CPU time = 63 ms, elapsed time = 85 ms.

(1000000 row(s) affected)
Table '#TableTwo'. Scan count 3, logical reads 1713, physical reads 0, read-ahead reads 0,
lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table '#TableOne'. Scan count 3, logical reads 6850, physical reads 0, read-ahead reads 0,
lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0,
lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

SQL Server Execution Times:
CPU time = 2765 ms, elapsed time = 15404 ms.
*/

As you can see, the performance is nearly identical. This is to be expected with the execution plans being nearly identical. The lesson here is to review the Execution Plans and test the queries using Statistics. They will help you to learn how to craft good queries.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-02-15 : 12:36:41
@biznick:
i've asked myself this question more than once. SQL Server is very good at short circuiting these kinds of statements.
so the perf is basicaly the same on the inner join.
now left join... that's a different story

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page
   

- Advertisement -