Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2005, 08:03 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default Join or not join

I have 4 tables. 1) tbl_administrators 2) tbl_designations 3) tbl_countries 4) tbl_admin_types.

There are 3 foreign keys in tbl_administrators corresponding to the primary keys of second, third and fourth tables.

To fetch the details of administrator from tbl_administrator, I can write the stored procedure in two ways.

Way 1
------
CREATE PROCEDURE [sp_show_logged_in_admin_user] (@param_admin_id int)
AS
SELECT
    tbl_administrators.full_name,
    (SELECT country_code FROM tbl_countries WHERE country_id=tbl_administrators.country_id) AS country_code,
    (SELECT designation FROM tbl_designations WHERE designation_id=tbl_administrators.designation_id) AS designation,
    (SELECT accessibility_level FROM tbl_admin_types WHERE admin_type_id=tbl_administrators.admin_type_id) AS accessibility_level
FROM
    tbl_administrators
WHERE
    tbl_administrators.administrator_id=@param_admin_i d
GO

Way 2
-----
CREATE PROCEDURE [sp_show_logged_in_admin_user1] (@param_admin_id int)
AS
SELECT
    tbl_administrators.full_name,
    tbl_countries.country_code,
    tbl_designations.designation,
    tbl_admin_types.accessibility_level
FROM
    (((tbl_administrators LEFT JOIN tbl_countries ON tbl_administrators.country_id=tbl_countries.countr y_id) LEFT JOIN tbl_designations ON tbl_administrators.designation_id=tbl_designations .designation_id) LEFT JOIN tbl_admin_types ON tbl_administrators.admin_type_id=tbl_admin_types.a dmin_type_id)
WHERE
    tbl_administrators.administrator_id=@param_admin_i d
GO

As data grows, which among them will give better performance ? (Currently, I cannot makeout any difference as there are only very few records)

Thanks

Madhu
 
Old February 2nd, 2005, 12:27 AM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello Madhu,

Its seems ur r requesting for only one record from administrator table. then there would not be much effect on performance, but

In ur way1:
U r using coorlated query, where all the 3 querys in select statement are executed for each row, so this will made ur query more slow if number of records increases in administrator table.

In ur way2:
where this will get all the records which r matched with administrator table, therefore much faster then way1.

and also put ur where clause ("tbl_administrators.administrator_id=@param_admin _id
") in ur first administrator joind ("tbl_administrators LEFT JOIN tbl_countries ON tbl_administrators.country_id=tbl_countries.countr y_id and tbl_administrators.administrator_id=@param_admin_i d").

Hope This will help u.

Amit Jain
Sr. Software Engineer
 
Old February 2nd, 2005, 12:50 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 463
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to madhukp
Default

Dear Amit,

Thanks for the answer.

I am to generalise this so that I can use it in queries which return multiple records also. In my DB, there are a lot of master tables. So, I was worried about the performance.

Also, I never knew that I can put where condition in Join clause. Thanks for giving this new information.
 
Old February 2nd, 2005, 01:11 AM
Authorized User
 
Join Date: Jan 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Its better to use way2, if u have to fetch multiple records from administrator table.

Amit Jain
Sr. Software Engineer





Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm new to join oneilsteven BOOK: ASP.NET Website Programming Problem-Design-Solution 0 February 9th, 2008 06:07 PM
join dhara_adh SQL Server 2000 2 January 2nd, 2007 12:39 AM
More than one join jaywhy13 Classic ASP Basics 2 January 31st, 2005 11:38 PM
INNER JOIN msmagied Classic ASP Databases 3 August 29th, 2004 12:28 PM
Oracle 8i inner join and left join problem puteri_84 Oracle 2 August 19th, 2004 07:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.