1

I have a table which gives a value between two items. The table has the following columns :

itemA | itemB | value
1     | 2     | 125
1     | 3     | 56
3     | 2     | 78
2     | 4     | 24
4     | 1     | 994
3     | 4     | 7

I highlight that the symetric values are not repeated. For example there is now row with itemA = 2 and itemB = 1 with value = 125 as this data is already stored with itemA = 1 and itemB = 2.

Now I would like to get all the values between items 1, 2 and 3 but with the symetric values as well. Something like that in the SQL result set :

itemA | itemB | value
1     | 2     | 125
1     | 3     | 56
2     | 1     | 125
2     | 3     | 78
3     | 1     | 56
3     | 2     | 78

I succeed to get that with the follwoing SQL query :

select itemA as iA, itemB as iB, value 
from table 
where itemA in (1,2,3) 
    and itemB in (1,2,3) 
union all 
select itemB as iA, itemA as iB, value 
from table 
where itemA in (1,2,3) 
    and itemB in (1,2,3) 
order by iA, iB;

However, it has been said to me that UNION operator is performance killer and this table has in fact more than 100 M rows. But after hours of brain storming, I don't find another way to get that. Please, could you see a better, more performant SQL query to get this result set.

Thank you in advance

2
  • looks more for codereview than stackoverflow. By the way, mentioning the DBMS really never hurts... Commented Sep 23, 2013 at 15:04
  • Yep, database is : Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production Commented Sep 23, 2013 at 15:15

1 Answer 1

2

UNION can be a performance killer because it is keeping only distinct items.
UNION ALL on the other hand simply concatenates the result sets, so there should be no significant performance hit.

But you really should measure it or check the explain plain to be sure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.