Groups | Blog | Home
all groups > sql server programming > february 2004 >

sql server programming : performance : successive queries versus join


John A Grandy
2/9/2004 9:29:51 PM
i'm sure the following has been extensively benchmarked ...

a stored procedure to retrieve a single row .... @PrimaryKeyFieldValue is sp
input parameter

method 1 :

select @ForeignKeyFieldValue from Table1 where T1.PrimaryKeyFieldName =
@PrimaryKeyFieldValue
select * from Table2 where PrimaryKeyFieldName = @ForeignKeyFieldValue

method 2 :

select * from Table2 T2 inner join Table1 T1 where T2.PrimaryKeyFieldName =
T1.ForeignKeyFieldName and T1.PrimaryKeyFieldName = @PrimaryKeyFieldValue




John A Grandy
2/9/2004 10:19:06 PM
hi Damien and thanks for the response.

very interesting stuff. so, extrapolating from this example, unless a sp
locks the tables it's working with, or wraps its code in an explicit
transaction, there is no way to stop the sql-svr engine from multitasking
execution of t-sql lines in SPs run under different connections ... ?


[quoted text, click to view]

You may wish to consider that there is no guarantee that the row in table1
is not deleted between query 1 and 2... (Unless you wrap the whole lot
inside an explicit transaction, I believe)

By joining you get all the data that matches your criteria at the point in
time the query is executed, without the hassle of transient variables etc...


Regards,
Damien.


[quoted text, click to view]
i'm sure the following has been extensively benchmarked ...

a stored procedure to retrieve a single row .... @PrimaryKeyFieldValue is sp
input parameter

method 1 :

select @ForeignKeyFieldValue from Table1 where T1.PrimaryKeyFieldName =
@PrimaryKeyFieldValue
select * from Table2 where PrimaryKeyFieldName = @ForeignKeyFieldValue

method 2 :

select * from Table2 T2 inner join Table1 T1 where T2.PrimaryKeyFieldName =
T1.ForeignKeyFieldName and T1.PrimaryKeyFieldName = @PrimaryKeyFieldValue






Uri Dimant
2/10/2004 8:44:19 AM
John
In the second query you are using inavlid syntax to join the tables. You
cannot specify WHERE clause after JOIN condition ,instead use ON
table.pk=table1.fk



[quoted text, click to view]

Damien Laffan
2/10/2004 4:58:11 PM

You may wish to consider that there is no guarantee that the row in =
table1 is not deleted between query 1 and 2... (Unless you wrap the =
whole lot inside an explicit transaction, I believe)

By joining you get all the data that matches your criteria at the point =
in time the query is executed, without the hassle of transient variables =
etc...


Regards,
Damien.


[quoted text, click to view]
i'm sure the following has been extensively benchmarked ...

a stored procedure to retrieve a single row .... @PrimaryKeyFieldValue =
is sp
input parameter

method 1 :

select @ForeignKeyFieldValue from Table1 where T1.PrimaryKeyFieldName =
=3D
@PrimaryKeyFieldValue
select * from Table2 where PrimaryKeyFieldName =3D @ForeignKeyFieldValue

method 2 :

select * from Table2 T2 inner join Table1 T1 where =
T2.PrimaryKeyFieldName =3D
T1.ForeignKeyFieldName and T1.PrimaryKeyFieldName =3D =
@PrimaryKeyFieldValue





Damien Laffan
2/10/2004 5:35:21 PM

I am definitely not the best person to comment on the internal locking =
mechanisms of MSSQL... Have a read of Books Online, the "Accessing and =
Changing Data" chapter will probably be useful for you.

Joins are an integral part of SQL, and I don't think that avoiding them =
will provide many (or any) benefits.

Just my opinion...

Regards,
Damien.



[quoted text, click to view]
hi Damien and thanks for the response.

very interesting stuff. so, extrapolating from this example, unless a =
sp
locks the tables it's working with, or wraps its code in an explicit
transaction, there is no way to stop the sql-svr engine from =
multitasking
execution of t-sql lines in SPs run under different connections ... ?


"Damien Laffan" <iGetEnoughSpamAlreadyThankyou@nospam.com> wrote in =
message
news:Oksy4r57DHA.3112@tk2msftngp13.phx.gbl...

You may wish to consider that there is no guarantee that the row in =
table1
is not deleted between query 1 and 2... (Unless you wrap the whole lot
inside an explicit transaction, I believe)

By joining you get all the data that matches your criteria at the point =
in
time the query is executed, without the hassle of transient variables =
etc...


Regards,
Damien.


[quoted text, click to view]
i'm sure the following has been extensively benchmarked ...

a stored procedure to retrieve a single row .... @PrimaryKeyFieldValue =
is sp
input parameter

method 1 :

select @ForeignKeyFieldValue from Table1 where T1.PrimaryKeyFieldName =
=3D
@PrimaryKeyFieldValue
select * from Table2 where PrimaryKeyFieldName =3D @ForeignKeyFieldValue

method 2 :

select * from Table2 T2 inner join Table1 T1 where =
T2.PrimaryKeyFieldName =3D
T1.ForeignKeyFieldName and T1.PrimaryKeyFieldName =3D =
@PrimaryKeyFieldValue







John A Grandy
2/16/2004 12:52:54 AM
I noticed my typo after I posted ... unfortunately no way for an autor to
edit his newsgroup posts.

[quoted text, click to view]

AddThis Social Bookmark Button