3

In my rails project, I have a feature that shows users whether any of their facebook friends are already on the site. After authenticating, I load their facebook friends into @fb_friends and then do this:

  @fb_friends.each do |friend|
    friend_find = Authorization.find_by_uid_and_provider(friend[:identifier], 'facebook')
    if friend_find
      @fb_friends_on_cody << friend_find.user_id
      @fb_friends.delete(friend)
    end 
  end

Basically, if a match is found (via the Authorization table), I push that record to @fb_friends_on_cody. This proves to be a very expensive operation:

  1. This hits the database once per friend. So in my case there are a total of 582 queries executed.

  2. Each Authorization.find_by_uid_and_provider has poor performance. I have add an index like so:

    add_index "authorizations", ["uid", "provider"], :name => "index_authorizations_on_uid_and_provider", :unique => true
    

I imagine a lot of performance gains can be seen by addressing the first point. And perhaps there are some ways to make the query itself more efficient. Appreciate any tips on these fronts.

Solution

Using Unixmonkey's guidance, I was able to cut down the number of queries from 582 to 1! Here's the final code:

  friend_ids = @fb_friends.map{|f| f[:identifier] }
  authorizations = Authorization.where('provider = ? AND uid IN (?)','facebook',friend_ids)
  @fb_friends_on_cody = authorizations.map{ |a| { user_id: a.user_id, uid: a.uid }}
  @fb_friends.reject!{|f| @fb_friends_on_cody.map{|f| f[:uid]}.include?(f[:identifier]) } 
pejmanjohn
  • 1,057
  • 3
  • 12
  • 26

1 Answers1

2

I think this should work

friend_ids = @fb_friends.map(&:id)
authorizations = Authorization.where('provider = ? AND uid IN (?)','facebook',friend_ids)
@fb_friends_on_cody = authorizations.map(&:user_id)
@fb_friends.delete_all('id in (?)', @fb_friends_on_cody)
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
  • Thanks! It's not liking this line `@fb_friends.delete_all('identifier in (?)', @fb_friends_on_cody)` probably because @fb_friends is an array (not ActiveRecord). I'll need to find a similar "delete_all" method for an array. – pejmanjohn Jul 26 '12 at 20:50
  • `@fb_friends.reject!{|f| @fb_friends_on_cody.include?(f.id) }` – Unixmonkey Jul 26 '12 at 20:53
  • sweet. I had found something similar with `delete_if`. I'm actually hitting an issue with the first line `@fb_friends.map(&:id)` I had already run `map` and `sort` on @fb_friends before so its basically in this format when the call is made: `[{:identifier=>"520717406", :name=>"Aaron Burr", :picture=>"https://graph.facebook.com/520717406/picture"}...` Running '@fb_friends.map(&:identifier)' on this fails – pejmanjohn Jul 26 '12 at 21:00
  • `@fb_friends.map{|f| f[:identifier] }` – Unixmonkey Jul 26 '12 at 21:11
  • thanks! had to make a few adjustments but got it up-and-running. this cut down the number of queries (in my case) from 582 to just 4! I'll add the final code as an update to the question. – pejmanjohn Jul 26 '12 at 21:29
  • Actually its just 1 query. The other 3 come from queries higher up in the controller block. Highlighting in case it causes confusion. – pejmanjohn Jul 26 '12 at 21:41