I need to to an update with a a join. Is there a better way than this? Token.connection.update "UPDATE tokens INNER JOIN items_tokens ON tokens.id items_tokens.token_id SET field=field-1 WHERE items_tokens.item_id #{item.id}" There are possibly hundreds of tokens for an item (actually words and word pairs in RSS feed descriptions), so instantiating a couple of hundred AR for each click is too CPU intensive. TIA, Jeffrey --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jeffrey L. Taylor wrote:> I need to to an update with a a join. Is there a better way than this? > > Token.connection.update "UPDATE tokens INNER JOIN items_tokens ON > tokens.id > items_tokens.token_id SET field=field-1 WHERE items_tokens.item_id > #{item.id}" > > There are possibly hundreds of tokens for an item (actually words and > word > pairs in RSS feed descriptions), so instantiating a couple of hundred AR > for > each click is too CPU intensive. > > TIA, > JeffreyThere''s ar extensions but it appears to not even work with rails 2. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oops, change ''correct code'' to ''current code''. AFAICT, both are correct. Quoting Jeffrey L. Taylor <ror-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org>:> > > Thank you. Haven''t double checked that the semantics are correct, but the > performance is unacceptable. I''ll stick with the correct code, ugly as it > is. Interesting that there is such a huge difference. > > Jeffrey > > mysql> UPDATE tokens SET clicks = clicks+1 WHERE (id in (select token_id from items_tokens where item_id = 143456)); > Query OK, 5 rows affected (7.58 sec) > Rows matched: 5 Changed: 5 Warnings: 0 > > mysql> UPDATE tokens INNER JOIN items_tokens ON tokens.id = items_tokens.token_id SET clicks=clicks+1 WHERE items_tokens.item_id = 143456; > Query OK, 5 rows affected (0.10 sec) > Rows matched: 5 Changed: 5 Warnings: 0 > > > Quoting Pardee, Roy <pardee.r-go57ItdSaco@public.gmane.org>: > > > > Could you do something like: > > > > Token.update_all(''field = field-1'', ''id in (select token_id from > > items_tokens where item_id = #{item.id})'')--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---