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 > Database Server Software > PostgreSQL > help with exclusion join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-09-12, 13:27
dtrobert dtrobert is offline
Registered User
 
Join Date: Feb 2010
Posts: 29
help with exclusion join

Hi,
I'm trying to make an efficient query which compares two tables and finds rows which are not in common. The tables are phones and callrecords. I want to see which phones have no entries in the callrecords (not been used in a while).

What I came up with works but isn't very efficient I feel

select * from phones p
where p.name not in (select cr.name from callrecord cr)


I thought perhaps doing a left join and then distinct might help. I don't want the actual callrecords in the result, just the list of phones. Any help is appreciated.

p.s. if I get this working, I was thinking to expand the query to include phones which have old records (calltime < say 30 days). Not sure if that might change the approach.

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-09-12, 13:37
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by dtrobert View Post
What I came up with works but isn't very efficient I feel
Don't "feel", test it and get a proof.
The execution plan will tell you if it's not efficient or if other statements are more efficient.

Code:
select * from phones p
where p.name not in (select cr.name from callrecord cr)
Note that this will return wrong results if cr.name can be NULL!

Quote:
I thought perhaps doing a left join and then distinct might help. I don't want the actual callrecords in the result, just the list of phones.
Use EXPLAIN ANALYZE on both statements and you'll see which one is better.

PostgreSQL's optmizier is pretty smart, and I wouldn't be surprised if both versions yield the same execution plan.

You should have an index on phones.name though.
Reply With Quote
  #3 (permalink)  
Old 01-12-12, 14:51
dtrobert dtrobert is offline
Registered User
 
Join Date: Feb 2010
Posts: 29
Okay but are these the only two options for finding records in one table which are not in another (distinct join and subselect). Am I missing another option?
Reply With Quote
  #4 (permalink)  
Old 01-12-12, 20:44
dtrobert dtrobert is offline
Registered User
 
Join Date: Feb 2010
Posts: 29
I actually found a good page showing the various options here

Finding records in one table not present in another table

for anyone with a similar issue.
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