If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > To subquery or not to subquery?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-05-03, 19:00
vwu98034 vwu98034 is offline
Registered User
 
Join Date: Oct 2002
Posts: 37
To subquery or not to subquery?

A query statement, I have, can be written using subqueries. If subquering are employed, they will query different fields of the same table along the query statement. Will the job can be done more effectively to have two queries instead of one query statement and use the first query to fetch data and pass it into the second query?
Reply With Quote
  #2 (permalink)  
Old 03-05-03, 20:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
that depends on the queries

i wonder what they look like


rudy
Reply With Quote
  #3 (permalink)  
Old 03-06-03, 11:39
vwu98034 vwu98034 is offline
Registered User
 
Join Date: Oct 2002
Posts: 37
Thanks Andy for your interests.

The query is something like the followings:

... AND (EXISTS (SELECT 1 FROM preference_type_table WHERE userid=p.userid AND type IN (SELECT type FROM profile_table WHERE userid='Tom') OR NOT EXISTS (SELECT 1 FROM preference_type_table WHERE userid=pf.userid) )

AND (EXISTS (SELECT 1 FROM preference_plan_table WHERE userid=p.userid AND plan IN (SELECT plan FROM profile_table WHERE userid='Tom') ) OR NOT EXISTS (SELECT 1 FROM "preference_plan_table WHERE userid=pf.userid) )

AND EXISTS (SELECT userid FROM preference_table WHERE p.userid=userid AND (2003 - (SELECT year FROM profile_table WHERE userid = 'Tom')) BETWEEN minage AND maxage) )

where the three subqueries

SELECT type FROM profile_table WHERE userid='Tom'

SELECT plan FROM profile_table WHERE userid='Tom'

SELECT year FROM profile_table WHERE userid = 'Tom'

are what I mentioned in my posting whether shall be obtained thorough a separated query, but not in this subquery form.

Thanks again for your advice.

v.
Reply With Quote
  #4 (permalink)  
Old 03-06-03, 11:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if they run fine, good performance, then leave them alone

i think some of the double EXISTS (if a preference exists and it's one of his, or if a preference doesn't exist) could be replaced by a left outer join

but don't rewrite the queries unless you have a real performnace problem

because then you may end up in a redesign, too

i can't really tell without seeing your data

like i said, if your queries run okay, leave them


rudy
Reply With Quote
  #5 (permalink)  
Old 03-06-03, 12:03
vwu98034 vwu98034 is offline
Registered User
 
Join Date: Oct 2002
Posts: 37
I raise this issue when I modified the query yesterday to add the type and plan as search criteria. I guess I shall use a common sense using a separated query to fetch data when the number of the subqueries is high.

Thanks Rudy for you opinion (sorry for reading your name wrong. I just got up from bed).

v.
Reply With Quote
  #6 (permalink)  
Old 03-06-03, 12:20
vwu98034 vwu98034 is offline
Registered User
 
Join Date: Oct 2002
Posts: 37
Rudy,

Also, I take a look at your suggestion that using left outer join instead of EXISTS in the subqueries. How to using outer join in different levels' query?


Quote:
Originally posted by r937
if they run fine, good performance, then leave them alone

i think some of the double EXISTS (if a preference exists and it's one of his, or if a preference doesn't exist) could be replaced by a left outer join

but don't rewrite the queries unless you have a real performnace problem

because then you may end up in a redesign, too

i can't really tell without seeing your data

like i said, if your queries run okay, leave them


rudy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On