Re: Slow ActiveRecord - MySQL

Heist
Fri, 10 Nov 2006 15:31:19 -0000

Around 6. select_all gave results similar to SQLyog (around 15
seconds).

It''s not possible to use YEAR(date_time) as an index, is it? If I alter
my table to have different columns for the year, month and day, will
there be a good performance gain?

And while this is all good advice, I still don''t understand why Rails
would have such problems with the query.

Jeremy Kemper wrote:
> On 11/9/06, HeistTheAlmighty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
<HeistTheAlmighty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> >
> > I use find_by_sql to execute a query which takes around 120 seconds to
> > execute in Ruby. When I execute the same request using SQLyog (GUI),
> > the requests takes around 15 seconds to execute.
> >
> > Here''s roughly what the query looks like:
> >
> > SELECT DAY(date_time) "day", COUNT( * ) "count"
> > FROM table
> > WHERE (id = some_id)
> > AND(YEAR(date_time) = 2006)
> > AND(MONTH(date_time) = 8)
> > AND(some_string IN (''some short string'') )
> > GROUP BY DAY( date_time );
> >
> > I''m querying about a million records.
>
>
> How many results?  Compare find_by_sql with connection.select_all timing.
>
> An index such as (id, YEAR(date_time), MONTH(date_time)) would really help,
> in any case.
>
> jeremy
>
> ------=_Part_56743_7586538.1163110279712
> Content-Type: text/html; charset=ISO-8859-1
> X-Google-AttachSize: 1140
>
> On 11/9/06, <a
href="mailto:HeistTheAlmighty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org">HeistTheAlmighty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
&lt;<a
href="mailto:HeistTheAlmighty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org">HeistTheAlmighty-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> <blockquote class="gmail_quote"
style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt
0.8ex; padding-left: 1ex;">I use find_by_sql to execute a query which
takes around 120 seconds to execute in Ruby. When I execute the same
request using SQLyog (GUI),
> the requests takes around 15 seconds to
execute. Here''s roughly what the query looks
like: SELECT DAY(date_time) "day", COUNT( *
) "count" FROM table WHERE (id =
some_id) AND(YEAR(date_time) = 2006)
> AND(MONTH(date_time) = 8) AND(some_string IN
(''some short string'') ) GROUP BY DAY( date_time
); I''m querying about a million
records. How many results?
Compare find_by_sql with connection.select_all
> timing. An index such as (id, YEAR(date_time),
MONTH(date_time)) would really help, in any
case. jeremy
>
> ------=_Part_56743_7586538.1163110279712--