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
 Other SQL Server Topics (2005)
 general optimization doubt

Author  Topic 

dev2dev
Starting Member

48 Posts

Posted - 2008-01-03 : 06:59:16
let me explain the scenario first

say table1 has columns col1, col2 and col3
where col1 and col2 together is PK

and col1 is same for for entire table and col2 is unique (well some may get doubt why to use col+col2 for pk when col2 alone is unique, to clarify this doubt, we have two dbs where in one db col1 does not repeat (in other words there are multiple dbs created based on this col1) and in other db it repeats (all dbs merged together))

sample data
col1--col2
==========
a--1
a--2
a--3
a--4
a--5
a--6
a--7
.
.
. so on

now my question, does both of the below queries takes same time

ex1:
select * from tabel1
select * from tabel1 where col1 = 'a'

in other words, does WHERE clause has any effect in performance when the column used in the query will always the same value with what it is compared?

does it has any effect when i join also?

ex2:
select a.*
from tabel1
inner join tabel2 a
on a.col1 = b.col1
and a.col2 = b.col2

select a.*
from tabel1
inner join tabel2 a
on a.col2 = b.col2 -- note: col1 is not used in join

and please explain how it is same/different

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2008-01-03 : 07:13:02
select a.*
from tabel1
inner join tabel2 a
on a.col2 = b.col2


This will NOT be efficient as there is no suitable INDEX that can assist the join. in effect you will be nearly getting a cross join. To see the effect of your queries/indices, have a look at the estimated/actual execution plans (Ctrl-K in Query Analyser)
Go to Top of Page

dev2dev
Starting Member

48 Posts

Posted - 2008-01-03 : 09:38:16
thanks adrew, i just checked in Query analyser using ctl+k,

i dont know how to read and understand these pictures/terminology

there were two difference in the 1st and 2nd

in the 1st example

1st one's physical operation was "Clustered Index Scan" (desc: scanning entire or a range of index)
2nd one's physical operation was "Clustered Index Seek" (desc: scanning a particular range of rows of index)

but rest of the things are same (cpu cost, i/o cost etc)

in 2nd example

1st one used "Merge Join" and the other used "Hash Join"

again for testing purpose, i create two tables without index and test 2nd example

now the 1st query is bit faster and execution plan is small
but 2nd query shows lot new things like "Parallelism/Gather Stream" "Parallelism/Repartion Stream" "Bitmap/Bitmap Create"

got to understand all these terms

any links you can provide?
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2008-01-03 : 11:14:25
seek is better than scan.
in phone book terms, seek uses the index at the back to quick jump (close) to the proper person-address-number page. scan means you read the entire book to find 1 phone number.

not sure of the practical differences between "merge join" and "hash join"...(google them???)


note re testing...make sure you have realistic volumes....small tables may be easier/simpler/faster to scan in bluk than for SQL to bother to try to use the indices.

re the execution plan terms....searching here for advice from members tkizer, kristen, nr, peso and some of the other top posters would be a start. they have posted links to various books over time which they recommend....my own reading style is 99.9% fiction, so I can't recommend anything in particular. most on my know-how has been "picked up" from hanging around here.
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-01-03 : 11:21:29
for a great explanation of all the different types of joins, see this blog:

http://blogs.msdn.com/craigfr/default.aspx

just start from his very first post and work towards the present, and you'll come out richer.


elsasoft.org
Go to Top of Page

dev2dev
Starting Member

48 Posts

Posted - 2008-01-04 : 03:45:42
Thanks for the discussion

quote:
Originally posted by AndrewMurphy


not sure of the practical differences between "merge join" and "hash join"...(google them???)



Yes, i queried table with over 130K rows
Go to Top of Page
   

- Advertisement -