Aggregation annotate question.

7 просмотров
Перейти к первому непрочитанному сообщению

Satan Study Django

не прочитано,
5 мая 2011 г., 13:26:0905.05.2011
– Django users
Hi. I've some question about aggregation annotate functions, like Max.
I don't realy like the queries it makes. Let me explain.

Models:
class Person (models.Model):
name = models.CharField (max_length = 100)
login = models.CharField (max_length = 30)
class Home (models.Model):
person = models.ForeignKey (Person)
state = models.ForeignKey (States)
date = models.DateTimeField ()
host = models.ForeignKey (Hosts)
time_spent = models.PositiveIntegerField (null = True)


Here is the expression with query made by it:

>>> print Home.objects.values('person').annotate(Max('id')).order_by().query
SELECT `main_home`.`person_id`, MAX(`main_home`.`id`) AS `id__max`
FROM `main_home`
GROUP BY `main_home`.`person_id`, `main_home`.`person_id`
ORDER BY NULL

Here i try to explain it:

mysql> explain SELECT `main_home`.`person_id`, MAX(`main_home`.`id`)
AS `id__max`
FROM `main_home`
GROUP BY `main_home`.`person_id`, `main_home`.`person_id`
ORDER BY NULL;
+----+-------------+-----------+-------+---------------
+--------------------+---------+------+------
+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len |
ref | rows | Extra |
+----+-------------+-----------+-------+---------------
+--------------------+---------+------+------
+------------------------------+
| 1 | SIMPLE | main_home | index | NULL | main_home_21b911c5 | 4 |
NULL | 2188 | Using index; Using temporary |
+----+-------------+-----------+-------+---------------
+--------------------+---------+------+------
+------------------------------+

As you can see, Using temporary is added, it's bacause
`main_home`.`person_id` used in GROUP BY 2 times:
GROUP BY `main_home`.`person_id`, `main_home`.`person_id`
and it makes query to run really long time.

If i'll manually remove one of `main_home`.`person_id` from GROUP BY,
query explain will contain no Using temporary, so execution takes
short time.

Could somebody explain to me - is it a feature? If it is, i really
want to listen reason why. If not - then what? A bug?

Cal Leeming [Simplicity Media Ltd]

не прочитано,
5 мая 2011 г., 13:30:3505.05.2011
– django...@googlegroups.com
Hmm, +1 on this.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Russell Keith-Magee

не прочитано,
5 мая 2011 г., 20:17:0405.05.2011
– django...@googlegroups.com

Looks like a bug to me. If there isn't already a ticket describing
this, please open one so that the problem isn't forgotten.

Yours,
Russ Magee %-)

Ответить всем
Отправить сообщение автору
Переслать
0 новых сообщений