# Batched Searching With a JOIN

**URL:** <https://www.ruby-forum.com/t/batched-searching-with-a-join/186557>\
**Category:** Ruby\
**Created:** [April 8, 2010, 9:41pm UTC](https://www.ruby-forum.com/t/batched-searching-with-a-join/186557 "2010-04-08T21:41:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [April 8, 2010, 9:41pm UTC](https://www.ruby-forum.com/t/batched-searching-with-a-join/186557/1 "2010-04-08T21:41:04Z")

</div>

I have two tables (IdmLdapAccounts and IdmAdAccounts) each with  
20000-30000 records in them. What I’m trying to do is join these tables  
so that I can find out what records from the LDAP table do NOT exist in  
the AD table.

uid is indexed in the LDAP table and samaccountname is indexed in the AD  
table.

I tried using some LEFT OUTER JOINs…but couldn’t figure out how to  
get that to work with the Model.find\_each that does batched queries.

So this is what I do now…it takes about 25 seconds to run…and  
that’s using SQLPlus in my dev environment. I would think it would be  
faster when I move to a real MySQL db.

```
accounts = []
IdmLdapAccount.find_each do |ldap_account|
  if !IdmAdAccount.exists?(:samaccountname => ldap_account.uid)
    accounts << ldap_account
  end
end
ap accounts.size
exit

```

This produces the result I want, but I don’t know if it’s the best way  
to do this. This queries the AD table once for every row in the LDAP  
table (about 23000 single queries)…it’s pretty quick…but seems  
inefficient. Wouldn’t a true JOIN work better?

Any thoughts?

Thanks,  
Matt
