MySQL Forums
Forum List  »  Optimizer & Parser

Re: Optimize sub query with large group by
Posted by: Øystein Grøvlen
Date: March 15, 2011 04:32AM

Alex K Wrote:
-------------------------------------------------------
> I don't use object_id anywhere else because I
> don't care which object it was. I just want to
> make sure that only the latest person to access
> each object gets credit.

I do not think you can achieve that with a GROUP BY. For columns that
are not part of the GROUP BY expression, no guarantees are given as to
which of the rows in the group the values are selected from.
Ref. http://dev.mysql.com/doc/refman/5.5/en/group-by-hidden-columns.html:

When using this feature, all rows in each group should have the
same values for the columns that are ommitted from the GROUP BY
part. The server is free to return any value from the group, so
the results are indeterminate unless all values are the same.

> The innermost query sorts all the of the events by
> date (most recent first). The query outside that
> uses the group by trick to select only the first
> event for each object. The outermost query then
> counts up how many per person.

That the group by query returns the first row from the result set of
the subquery, is an implementation detail that may change in future
versions without further notice.

Regards,

Øystein Grøvlen,
Senior Principal Software Engineer,
MySQL Group, Oracle,
Trondheim, Norway

Options: ReplyQuote


Subject
Views
Written By
Posted
3926
March 10, 2011 07:44PM
Re: Optimize sub query with large group by
1836
March 15, 2011 04:32AM


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.