[Django] #25470: Django cast DATE field to DATETIME unexpectedly

97 views
Skip to first unread message

Django

unread,
Sep 26, 2015, 5:37:23 AM9/26/15
to django-...@googlegroups.com
#25470: Django cast DATE field to DATETIME unexpectedly
----------------------------------------------+--------------------
Reporter: stanleyxu2005 | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
I posted a question at http://stackoverflow.com/questions/32795047/have-a
-perforamance-issue-of-query-date-object-using-django-queryset

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 = set(Model1.objects.dates("date", kind="day"))
}}}

I dumped the django queries.

{{{
[{u'time': u'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
{u'time': u'1.989', u'sql': u"SELECT DISTINCT
CAST(DATE_FORMAT(`model1_table`.`date`, '%Y-%m-%d 00:00:00') AS DATETIME)
AS `datefield` FROM `model1_table` WHERE `model1_table`.`date` IS NOT NULL
ORDER BY `datefield` ASC"}
]
}}}

Actually the second query did a type cast. This is the root cause of the
slow.

I'd like to know, why django cast DATE to DATETIME. Is it a bug?

--
Ticket URL: <https://code.djangoproject.com/ticket/25470>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 26, 2015, 8:52:45 AM9/26/15
to django-...@googlegroups.com
#25470: Django cast DATE field to DATETIME unexpectedly
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by stanleyxu2005):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

> I posted a question at http://stackoverflow.com/questions/32795047/have-a
> -perforamance-issue-of-query-date-object-using-django-queryset
>
> 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 = set(Model1.objects.dates("date", kind="day"))
> }}}
>
> I dumped the django queries.
>
> {{{
> [{u'time': u'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
> {u'time': u'1.989', u'sql': u"SELECT DISTINCT
> CAST(DATE_FORMAT(`model1_table`.`date`, '%Y-%m-%d 00:00:00') AS DATETIME)
> AS `datefield` FROM `model1_table` WHERE `model1_table`.`date` IS NOT
> NULL ORDER BY `datefield` ASC"}
> ]
> }}}
>
> Actually the second query did a type cast. This is the root cause of the
> slow.
>
> I'd like to know, why django cast DATE to DATETIME. Is it a bug?

New description:

I posted a question at http://stackoverflow.com/questions/32795047/have-a
-perforamance-issue-of-query-date-object-using-django-queryset

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 = set(Model1.objects.dates("date", kind="day"))
}}}

I dumped the django queries.

{{{
[{u'time': u'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
{u'time': u'1.989', u'sql': u"SELECT DISTINCT
CAST(DATE_FORMAT(`model1_table`.`date`, '%Y-%m-%d 00:00:00') AS DATETIME)
AS `datefield` FROM `model1_table` WHERE `model1_table`.`date` IS NOT NULL
ORDER BY `datefield` ASC"}
]
}}}

I noticed that the second query did a type cast. The cast is not a
necessary step. It slows down when the amount of records is huge. I pretty
sure this is the root cause of the slow.

The table is created by django. The field is exactly `DATE` field. I'd


like to know, why django cast DATE to DATETIME. Is it a bug?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:1>

Django

unread,
Sep 26, 2015, 8:53:29 AM9/26/15
to django-...@googlegroups.com
#25470: Django cast DATE field to DATETIME unexpectedly
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by stanleyxu2005:

Old description:

> I posted a question at http://stackoverflow.com/questions/32795047/have-a
> -perforamance-issue-of-query-date-object-using-django-queryset
>
> 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 = set(Model1.objects.dates("date", kind="day"))
> }}}
>
> I dumped the django queries.
>
> {{{
> [{u'time': u'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
> {u'time': u'1.989', u'sql': u"SELECT DISTINCT
> CAST(DATE_FORMAT(`model1_table`.`date`, '%Y-%m-%d 00:00:00') AS DATETIME)
> AS `datefield` FROM `model1_table` WHERE `model1_table`.`date` IS NOT
> NULL ORDER BY `datefield` ASC"}
> ]
> }}}
>

> I noticed that the second query did a type cast. The cast is not a
> necessary step. It slows down when the amount of records is huge. I

> pretty sure this is the root cause of the slow.
>
> The table is created by django. The field is exactly `DATE` field. I'd


> like to know, why django cast DATE to DATETIME. Is it a bug?

New description:

I posted a question at http://stackoverflow.com/questions/32795047/have-a
-perforamance-issue-of-query-date-object-using-django-queryset

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 = set(Model1.objects.dates("date", kind="day"))
}}}

I dumped the django queries.

{{{
[{u'time': u'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
{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"}
]
}}}

I noticed that the second query did a type cast. The cast is not a


necessary step. It slows down when the amount of records is huge. I pretty
sure this is the root cause of the slow.

The table is created by django. The field is exactly `DATE` field. I'd


like to know, why django cast DATE to DATETIME. Is it a bug?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:2>

Django

unread,
Sep 26, 2015, 9:46:52 AM9/26/15
to django-...@googlegroups.com
#25470: Django cast DATE field to DATETIME unexpectedly
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by stanleyxu2005:

Old description:

> I posted a question at http://stackoverflow.com/questions/32795047/have-a


> -perforamance-issue-of-query-date-object-using-django-queryset
>
> 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 = set(Model1.objects.dates("date", kind="day"))
> }}}
>
> I dumped the django queries.
>
> {{{
> [{u'time': u'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
> {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"}
> ]
> }}}
>


> I noticed that the second query did a type cast. The cast is not a
> necessary step. It slows down when the amount of records is huge. I
> pretty sure this is the root cause of the slow.
>

> The table is created by django. The field is exactly `DATE` field. I'd


> like to know, why django cast DATE to DATETIME. Is it a bug?

New description:

I posted a question at http://stackoverflow.com/questions/32795047/have-a
-perforamance-issue-of-query-date-object-using-django-queryset

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'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
{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"}
]
}}}

I noticed that the second query did a type cast. The cast is not a


necessary step. It slows down when the amount of records is huge. I pretty
sure this is the root cause of the slow.

The table is created by django. The field is exactly `DATE` field. I'd


like to know, why django cast DATE to DATETIME. Is it a bug?

Currently, I use a workaround:
{{{
db_dates = [dt['date'] for dt in
Model1.objects.order_by('date').values('date').distinct()]
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:3>

Django

unread,
Sep 26, 2015, 9:47:30 AM9/26/15
to django-...@googlegroups.com
#25470: Django cast DATE field to DATETIME unexpectedly
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by stanleyxu2005:

Old description:

> I posted a question at http://stackoverflow.com/questions/32795047/have-a


> -perforamance-issue-of-query-date-object-using-django-queryset
>
> 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'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
> {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"}
> ]
> }}}
>


> I noticed that the second query did a type cast. The cast is not a
> necessary step. It slows down when the amount of records is huge. I
> pretty sure this is the root cause of the slow.
>

> The table is created by django. The field is exactly `DATE` field. I'd


> like to know, why django cast DATE to DATETIME. Is it a bug?
>

> Currently, I use a workaround:
> {{{
> db_dates = [dt['date'] for dt in
> Model1.objects.order_by('date').values('date').distinct()]
> }}}

New description:

I posted a question at http://stackoverflow.com/questions/32795047/have-a
-perforamance-issue-of-query-date-object-using-django-queryset

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'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
{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"}
]
}}}

I noticed that the second query did a type cast. The cast is not a


necessary step. It slows down when the amount of records is huge. I pretty
sure this is the root cause of the slow.

The table is created by django. The field is exactly `DATE` field. I'd


like to know, why django cast DATE to DATETIME. Is it a bug?

Currently, I use a workaround:


{{{
db_dates = [dt['date'] for dt in
Model1.objects.order_by('date').values('date').distinct()]
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:4>

Django

unread,
Sep 26, 2015, 10:34:27 AM9/26/15
to django-...@googlegroups.com
#25470: Django cast DATE field to DATETIME unexpectedly
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by stanleyxu2005:

Old description:

> I posted a question at http://stackoverflow.com/questions/32795047/have-a


> -perforamance-issue-of-query-date-object-using-django-queryset
>
> 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'0.000', u'sql': u'SET SQL_AUTO_IS_NULL = 0'},
> {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"}
> ]
> }}}
>


> I noticed that the second query did a type cast. The cast is not a
> necessary step. It slows down when the amount of records is huge. I
> pretty sure this is the root cause of the slow.
>

> The table is created by django. The field is exactly `DATE` field. I'd


> like to know, why django cast DATE to DATETIME. Is it a bug?
>

> Currently, I use a workaround:
> {{{
> db_dates = [dt['date'] for dt in
> Model1.objects.order_by('date').values('date').distinct()]
> }}}

New description:

I posted a question at http://stackoverflow.com/questions/32795047/have-a
-perforamance-issue-of-query-date-object-using-django-queryset

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"}
}}}

I noticed that the second query did a type cast. The cast is not a


necessary step. It slows down when the amount of records is huge. I pretty
sure this is the root cause of the slow.

The table is created by django. The field is exactly `DATE` field. I'd


like to know, why django cast DATE to DATETIME. Is it a bug?

Currently, I use a workaround:


{{{
db_dates = [dt['date'] for dt in
Model1.objects.order_by('date').values('date').distinct()]
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:5>

Django

unread,
Sep 26, 2015, 10:38:55 AM9/26/15
to django-...@googlegroups.com
#25470: Django cast DATE field to DATETIME unexpectedly
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by stanleyxu2005:

Old description:

> I posted a question at http://stackoverflow.com/questions/32795047/have-a


> -perforamance-issue-of-query-date-object-using-django-queryset
>
> 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"}
> }}}
>


> I noticed that the second query did a type cast. The cast is not a
> necessary step. It slows down when the amount of records is huge. I
> pretty sure this is the root cause of the slow.
>

> The table is created by django. The field is exactly `DATE` field. I'd


> like to know, why django cast DATE to DATETIME. Is it a bug?
>

> Currently, I use a workaround:
> {{{
> db_dates = [dt['date'] for dt in
> Model1.objects.order_by('date').values('date').distinct()]
> }}}

New description:

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 a workaround:
{{{

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].

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:6>

Django

unread,
Sep 27, 2015, 10:58:34 PM9/27/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by stanleyxu2005:

Old description:

> 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 a workaround:
> {{{
> db_dates = Model1.objects.values_list('date', flat=True).distinct()
> }}}
>

> of-query-date-object-using-django-queryset StackOverflow].

New description:

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].

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:7>

Django

unread,
Sep 30, 2015, 2:30:58 PM9/30/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Unreviewed => Accepted


Comment:

If you'd like to investigate and propose a fix, that would be great!

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:8>

Django

unread,
Oct 5, 2015, 2:48:43 PM10/5/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by stanleyxu2005):

Thanks for agreeing this is a serious performance impact. It is still
unclear, why DATE is first converted to DATETIME. The Django team must
know, whether the code is written by design?

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:9>

Django

unread,
Oct 5, 2015, 2:58:13 PM10/5/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

Did you try modifying the code and seeing if any tests fail? That's often
helpful.

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:10>

Django

unread,
Oct 8, 2015, 12:59:43 PM10/8/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* cc: felisiak.mariusz@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:11>

Django

unread,
Oct 11, 2015, 3:42:47 AM10/11/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by stanleyxu2005):

Replying to [comment:10 timgraham]:


> Did you try modifying the code and seeing if any tests fail? That's
often helpful.

My solution just helps me to solve my current problem. Sorry, I don't
realize that when I report issue to django community, I'd provide patch
and do necessary tests. Really sorry about that. Bye

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:12>

Django

unread,
Oct 12, 2015, 4:22:18 PM10/12/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: felixxm
Type: | Status: assigned

Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => assigned
* owner: nobody => felixxm


--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:13>

Django

unread,
Oct 13, 2015, 3:41:51 PM10/13/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: felixxm
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1


Comment:

I made [https://github.com/django/django/pull/5428 PR] to omit unnecessary
type cast.

In my opinion, the second step, is to change default {{{order}}} in
{{{dates}}}, {{{datetimes}}} methods

{{{#!diff
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -726,21 +726,25 @@ class QuerySet(object):
clone._iterable_class = FlatValuesListIterable if flat else
ValuesListIterable
return clone

- def dates(self, field_name, kind, order='ASC'):
+ def dates(self, field_name, kind, order=None):
"""
}}}
or give a way to disable it
{{{#!diff
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -733,14 +733,18 @@ class QuerySet(object):
"""
assert kind in ("year", "month", "day"), \
"'kind' must be one of 'year', 'month' or 'day'."
- assert order in ('ASC', 'DESC'), \
- "'order' must be either 'ASC' or 'DESC'."
- return self.annotate(
+ assert order is None or order in ('ASC', 'DESC'), \
+ "'order' must be either 'ASC', 'DESC' or None."
+ qs = self.annotate(
datefield=Date(field_name, kind),
plain_field=F(field_name)
).values_list(
'datefield', flat=True
- ).distinct().filter(plain_field__isnull=False).order_by(('-' if
order == 'DESC' else '') + 'datefield')
+ ).distinct().filter(plain_field__isnull=False)
+ if order is not None:
+ return qs.order_by(('-' if order == 'DESC' else '') +
'datefield')
+ else:
+ return qs
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:14>

Django

unread,
Oct 15, 2015, 1:57:00 PM10/15/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: felixxm
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by felixxm):

Any suggestions? I can't find any indications of default sort.

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:15>

Django

unread,
Oct 19, 2015, 8:34:29 PM10/19/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: felixxm
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* cc: charettes (added)


Comment:

The default sort order of `ASC` seems
[https://docs.djangoproject.com/en/1.8/ref/models/querysets/#dates to be
documented] and simply making results unordered would be backward
incompatible.

I guess you should open a new ticket for a feature request allowing the
use of `order=None` to get unordered results.

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:16>

Django

unread,
Oct 20, 2015, 8:33:55 PM10/20/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: felixxm
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:17>

Django

unread,
Oct 21, 2015, 2:27:01 PM10/21/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: felixxm
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_tests: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:18>

Django

unread,
Oct 21, 2015, 7:25:24 PM10/21/15
to django-...@googlegroups.com
#25470: Serious performance impact (500x slower!) when querying distinct model
dates objects
-------------------------------------+-------------------------------------
Reporter: stanleyxu2005 | Owner: felixxm
Type: | Status: closed

Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"0f6d51e6a0a22e37e45c4bf452ddb49723e2f956" 0f6d51e6]:
{{{
#!CommitTicketReference repository=""
revision="0f6d51e6a0a22e37e45c4bf452ddb49723e2f956"
Fixed #25470 -- Avoided unnecessary, expensive DATETIME typecast on MySQL.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25470#comment:19>

Reply all
Reply to author
Forward
0 new messages