Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slow Join Query - Can it be improved? 1

Status
Not open for further replies.

forumposters

Programmer
Aug 31, 2006
61
US
Here's my SQL:

Code:
SELECT *
FROM A, B
WHERE A.address IN ('X')
OR B.address IN ('X')
ORDER BY B.address, A.address,B.[date], A.[date]

Is there anything that sticks out here that could be improved so that this query takes less time to run?
 
well, something sticks out a lot, but by its absence -- the join condition

look up cross join and cartesian product

r937.com | rudy.ca
 
Thanks for bringing that to my attention. So, I actually don't want to join the two tables and I titled this thread wrong, sorry about that. I just want to get records from both of the tables. The SQL statement produces the results I want, but it takes a long time. Is there anyway to rewrite that SQL statement to do the same thing, but to run faster?
 
maybe this is what you want?
Code:
SELECT address
     , [date]
     , [i]othercolumns[/i]
     , 'A' AS source
  FROM A
 WHERE address IN ('X')
UNION ALL
SELECT address
     , [date]
     , [i]othercolumns[/i]
     , 'B' AS source
  FROM B
 WHERE address IN ('X')
ORDER 
    BY address
     , [date]



r937.com | rudy.ca
 
That's exactly what I needed. Thanks!

One more question:
If I want to also select a field from from table A that does not exist in table B, how can I do that? Is there some SQL code that will add this field to table B for this query and add a default value of my choice? So, I would have something like this(psuedocode):

Code:
SELECT fieldOnlyinTableA, address
..
..
FROM A

UNION ALL 

SELECT (some SQL code here that adds the field fieldOnlyinTableA so that an error doesn't occur  )
...
...

Or, is there another way to do this? I suppose that I can add the fieldOnlyinTableA to the B table and set a default value if I have too, but I'd rather not have to do that.
 
i'm not sure i understand your question...

perhaps you mean this:
Code:
SELECT tableA.foo
     , COALESCE(tableB.bar,'default if no B for this A') AS bar
  FROM ...

r937.com | rudy.ca
 
I suggest you don't use the IN condition. It may not substantially affect your query but it is generally a performance killer.
 
Let me try and explain the problem in another way. The UNION ALL only works if the two tables have the same columns. If one table has an extra column and you want to select it, then an error occurs. I want to have that extra column in table A in the rows returned from table A.
 
when you have an extra column in one table that you want returned, but no corresponding column in the other...

... use NULL :)

SELECT x, y
FROM table1
UNION ALL
SELECT z, NULL
FROM table2



r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top