Reputation: 1418
I tried to think of a sql query to get results consisting of multiple columns from multiple records, which the table is like:
key_1 key_2
--------------------
1 2
1 3
1 4
5 6
Both key_1 and key_2 are primary keys from another table. And the result I want to obtain is getting every key that are related to the key '1', which is:
key
--------
1
2
3
4
I tried the following query:
SELECT key_1 as key FROM tbl_keys_link WHERE key_1 IN (1)
UNION
SELEVY key_2 as key FROM tbl_keys_link WHERE key_2 IN (1)
But I examined it using pgadmin's 'Explain' function, seems the process time of this query is not very optimal. Is there any other ways to construct a query to achieve this with a better performance? Thanks!
Upvotes: 1
Views: 185
Reputation: 1270693
You can try phrasing the query like this:
select a.key
from anothertable a
where exists (select 1 from tbl_keys_link p pairs.key_1 = a.key and pairs.key_2 = 1) or
exists (select 1 from tbl_keys_link p pairs.key_2 = a.key and pairs.key_1 = 1);
For performance, you will want two indexes: tbl_keys_link(key_1, key_2)
and tbl_keys_link(key_2, key1)
.
This eliminates the duplicate elimination step (needed for the distinct). Also, the query should just scan the other table and do one or two index lookups to identify the matching keys.
Upvotes: 1
Reputation:
You can try this:
with keys as (
select key_1, key_2
from tbl_keys_link
where key_1 = 1
or key_2 = 1
)
SELECT distinct unnest(array[key_1, key_2])
from keys
order by 1;
But I have no idea if that is more efficient. At least it only needs to scan the table once. If you have an index on each column, Postgres might be able to use those indexes for searching the "relevant" rows.
Upvotes: 0