在我的一个模型中,我有以下字段:
creation_time = models.DateTimeField(auto_now_add=True)
creation_date = models.DateField(db_index=True, auto_now_add=True)
如果我使用创建时间(DateTimeField字段)执行以下过滤器:
User.objects.filter(id__in=user_ids, creation_time__range=[start_date, end_date]).count()
正如预期的那样,运行需要几毫秒。但是如果我在创建日期(带有db索引的DateField字段)上执行相同的过滤器:
User.objects.filter(id__in=user_ids, creation_date__range=[start_date, end_date]).count()
几分钟完成...最终我得到的结果几乎与上面相同,但很长一段时间后。
我虽然两个字段都以相同的方式存储在数据库中,但唯一的区别是django如何将它们序列化/反序列化为Python对象(以及ofc日期字段获取默认的午夜时间)。
那么为什么使用日期字段要慢得多?是因为db_index(如果是这样,为什么)?
编辑:他们的SQL查询看起来与btw基本相同:
>>> print User.objects.filter(id__in=user_ids, creation_time__range=[start_date, end_date]).query
SELECT "users_user"."id", "users_user"."uuid", "users_user"."creation_time", "users_user"."ip", "users_user"."user_agent", "users_user"."hash", "users_user"."creation_date", "users_user"."is_bot" FROM "users_user" WHERE ("users_user"."id" IN (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) AND "users_user"."creation_time" BETWEEN 2017-07-18 00:00:00 AND 2017-07-19 00:00:00)
>>> print User.objects.filter(id__in=user_ids, creation_date__range=[start_date, end_date]).query
SELECT "users_user"."id", "users_user"."uuid", "users_user"."creation_time", "users_user"."ip", "users_user"."user_agent", "users_user"."hash", "users_user"."creation_date", "users_user"."is_bot" FROM "users_user" WHERE ("users_user"."id" IN (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) AND "users_user"."creation_date" BETWEEN 2017-07-18 AND 2017-07-19)
谢谢,