all groups > sql server programming > october 2007 >
You're in the

sql server programming

group:

query performance


query performance Raan
10/21/2007 7:26:35 PM
sql server programming: Hi All

i have one query

i have information about some people , these people have mulitple
addresses(2-4) and multiiple phone numbers(3-10).


I have created table like following

people_table
peopleid , name , age , profession , sex


Address_table
address_Id,peopleid , address


phone_number_table

phone_number_Id,
peopleid , phone


The problem is that if i want to know the information about people
with name "john"

then i have to run many queries

first query is
select peopleid , name , age from people_table;

second query
in loop according to the rslt obtained
select address from Address_table where peopleId = ID_OBTAINED

third query.. again in loop
select address from phone_number_table where peopleId = ID_OBTAINED

Is there any better for solving my problem. I think my db design or
way of querying is poor.


thnx
Raan
Re: query performance Jay
10/21/2007 8:56:00 PM
[quoted text, click to view]

This is the genisus of the problem. If you want to avoid the loops, you have
to select a single 'peopleid'

Then you can run simpler queries on the rest:

select *
from Address_Table
Where peopleid = ?
order by ?

select *
from phone_number_table
Where peopleid = ?
order by ?

I suspose you could also do:

select *
from Address_Table
Where peopleid in (select peopleid from people_table where name = 'john')
order by ?

select *
from phone_number_table
Where peopleid in (select peopleid from people_table where name = 'john')
order by ?

But you will have trouble ordering the output.

[quoted text, click to view]

RE: query performance Mattias Lind, Sweden
10/22/2007 4:34:00 PM
Why not change this into a new design instead?

tab_People
PID (Non-clustered Index), Name (Clustered Index), Age, Sex, Profession

tab_Phone
PhID (Non-clustered Index), Phone (Clustered Index)

tab_Address
AdID (Non-clustered Index), Address (Clustered Index)

join_People_Phone
PID (Clustered Index), PhID (Non-clustered Index)

join_People_Address
PID (Clustered Index), AdID (Non-clustered Index)

Then solve the query with one joined operation
SELECT
p.Name,
p.Age,
p.Sex,
p.Profession,
ad.Address,
ph.Phone
FROM tab_People p
INNER JOIN join_People_Address jpa ON p.PID = jpa.PID
INNER JOIN tab_Address ad ON jpa.AdID = ad.AdID
INNER JOIN join_People_Phone jpp ON p.PID = jpp.PID
INNER JOIN tab_Phone ph ON jpp.PhID = ph.PhID
WHERE Name LIKE 'John'

AddThis Social Bookmark Button