MySQL Forums
Forum List  »  Performance

Re: How to somehow use index for group by expression
Posted by: Rick James
Date: September 08, 2012 11:04PM

select  description as name,
        sum(`count`),
        max(date(transdate)),
        ceil((UNIX_TIMESTAMP(`timestamp`) - 1306382221) / (86400 * 100)) as grouper
    from  reporting
    where  UNIX_TIMESTAMP(`timestamp`) between 1323595130 and 9999999999
      and  transtype in (0 , 1, 2, 10, 11, 12, 13)
    group by  name , grouper
    order by  name , `timestamp`
The optimal index is the "compound" INDEX(transtype, `timestamp`)
You will still have "using temp, filesort", but it will not be as bad.
You don't need indexes with expressions (in this case).

Make the ORDER BY the same as the GROUP BY.

What's wrong with your "grouper"? Maybe 1306382221 isn't quite the right value? If not, then use UNIX_TIMESTAMP('2012-01-01'), or whatever.

> where UNIX_TIMESTAMP(`timestamp`) between 1323595130 and 9999999999
Why not:
where UNIX_TIMESTAMP(`timestamp`) >= 1323595130

Instead of
max(date(transdate)),
You could calculate the end (or start) of the "grouper" range:
ceil((UNIX_TIMESTAMP(`timestamp`) - 1306382221) / (86400 * 100)) * (86400 * 100)
At which point, you don't need both the max and the grouper; keep the grouper.

Or, are you looking for the 'last' timestamp in each group?

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: How to somehow use index for group by expression
986
September 08, 2012 11:04PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.