﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25470	Serious performance impact (500x slower!) when querying distinct model dates objects	Qian Xu	nobody	"I'm using django 1.8.4 writing a webapp. The backend uses MySQL 5.6 (MyISAM). Recently the number of table records reaches 1 million, it will take 1-1.5 seconds to query all distinct record dates. But using MySQL client, it takes less than 0.001 second.

'''Django Code'''

{{{
class Model1(models.Model):
    date = models.DateField(db_index=True)

# benchmark code
db_dates = Model1.objects.dates(""date"", kind=""day"")
}}}

I dumped the django queries. 

{{{
{u'time': u'1.989', u'sql': u""SELECT DISTINCT CAST(DATE_FORMAT(`app1_model1`.`date`, '%Y-%m-%d 00:00:00') AS DATETIME) AS `datefield` FROM `app1_model1` WHERE `app1_model1`.`date` IS NOT NULL ORDER BY `datefield` ASC""}
}}}

As you can see, the query did possibly an unnecessary type cast. The performance impact is scaled to the amount of records. The table field is exactly `DATE` type. I don't know, if there is some reason for the type casting. 

Currently, I use another query to get these dates, which takes 0.04 second.
{{{
db_dates = Model1.objects.values_list('date', flat=True).distinct()
}}}

I posted a question at [http://stackoverflow.com/questions/32795047/have-a-perforamance-issue-of-query-date-object-using-django-queryset StackOverflow].
"	Cleanup/optimization	new	Database layer (models, ORM)	1.8	Normal				Unreviewed	0	0	0	0	0	0
