the N+1 db calls is 1 db call for the customers and then another N
calls, one for each of the customers to get their cell or email what
not.  So it would be a customer find with whatever limits/conditions,
maybe even the default hobo_index find.

As to why your query is taking longer to run in db console, it must be
how you're running the query as it should be no longer than an
equivalent rails call and should be slightly quicker as it doesn't
have the overhead of building the sql code to make the call.

On Feb 4, 5:40 pm, Gabriele Tassoni <[email protected]>
wrote:
> On 5 Feb, 01:09, kevinpfromnm <[email protected]> wrote:
>
> > How many customers per page?
>
> 30 customers per page
>
> > Can you get by with the N+1 db
> > calls from not including either?
>
> I can't understand this bit.. sorry.. do you intend performing a
> Customer.find(:all) then trying to filter rails side?
>
> > If not, might be a case for a
> > find_by_sql call.
>
> Hmmm! Don't think so, testing the SQL query directly in mysl console,
> I get 6 seconds, thus is really a query problem, if I can concatenate
> the Scopes, without losing them as scopes, the time would Drop to
> 55ms... the query is:
>
> SELECT `customers`.* FROM `customers` LEFT OUTER JOIN `cellphones` ON
> cellphones.customer_id = customers.id LEFT OUTER JOIN `emails` ON
> emails.customer_id = customers.id WHERE ((cellphones.active = 1 AND
> cellphones.customer_id = customers.id) OR (emails.active = 1 AND
> emails.customer_id = customers.id))
>
> > One thing, you don't need to use hobo_index necessarily.  You can get
> > similar results by doing your own pagination.
>
> hmmm, can I apply the will_paginate to the array that result from
> concatenation? Must investigate...
>
>
>
> > On Feb 4, 5:01 pm, Gabriele Tassoni <[email protected]>
> > wrote:
>
> > > From what I can see, chaining them is not like saying "either ...
> > > or ..." i.e. I have 2600 customers with cellphones, and 4 with emails,
> > > with 3 that have both, What I want is obtain 2601 records so far (it
> > > would be good even to have 2604, I can add a :group condition...), but
> > > what I obtain is 3 records, the customers that have both an email and
> > > a cellphone... so, chaining is like intersection, not concatenation...
>
> > > On 5 Feb, 00:50, kevinpfromnm <[email protected]> wrote:
>
> > > > You can't just chain your named_scopes?
> > > > Customer.with_cellphones.with_emails
>
> > > > On Feb 4, 4:11 pm, Gabriele Tassoni <[email protected]>
> > > > wrote:
>
> > > > > Hi,
> > > > > I try to explain better, I wrote a named scope like that:
>
> > > > > named_scope :with_cellphones_or_emails, {:include =>
> > > > > [:cellphones, :emails], :conditions => ["(cellphones.active = ? AND
> > > > > cellphones.customer_id = customers.id) OR (emails.active = ? AND
> > > > > emails.customer_id = customers.id)", true, true]}
>
> > > > > for the Customer model, calling Customer.with_cellphones_or_emails
> > > > > takes about 6 seconds on my macbook unibody with 2GB Ram, I've made
> > > > > some tests, I wrote two more named_scopes:
>
> > > > > named_scope :with_emails, {:joins => :emails, :conditions =>
> > > > > {"emails.active" => true}}
> > > > > named_scope :with_cellphones, {:joins => :cellphones, :conditions =>
> > > > > {"cellphones.active" => true}}
>
> > > > > running Customer.with_cellphones takes approx. 50ms,
> > > > > Customer.with_emails takes approx 5ms (yeah, my customers have more
> > > > > cellphones than emails! ^_^)... 55ms vs 6s! °_° (these are mysql
> > > > > calculated times)...
>
> > > > > Trying to minimize the time spent on db query, by single query, I
> > > > > thought it could be great if I can concatenate the two results, indeed
> > > > > I can do it:
>
> > > > > @customers = Customer.with_cellphones + Customer.with_emails
>
> > > > > but the resulting object is an Array, the index action of the
> > > > > controller is like this one:
>
> > > > > hobo_index @customers, :order => :name, :group => "customers.id"
>
> > > > > is there a way to obtain an ActiveRecord::NamedScope::Scope object
> > > > > from that concatenation or to let hobo_index accept the Array?
>
> > > > > Thank you for the answer.
>
> > > > > G.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to