Python orm日期时间问题
我有一个Peewee数据库模型产品,在一个非常大的数据库中有一个datetime字段。 当我想选择插入时间大于现在()的文章时,查询将永远挂起Python orm日期时间问题,python,mysql-python,peewee,Python,Mysql Python,Peewee,我有一个Peewee数据库模型产品,在一个非常大的数据库中有一个datetime字段。 当我想选择插入时间大于现在()的文章时,查询将永远挂起 class Products(BaseModel): id = IntegerField() name= CharField() inserted = DateTimeField() class Meta: db_table = 'products' Products.select().where(Products.i
class Products(BaseModel):
id = IntegerField()
name= CharField()
inserted = DateTimeField()
class Meta:
db_table = 'products'
Products.select().where(Products.inserted >datetime(2013, 04, 03, 14, 52, 50).strftime("%Y-%m-%d %H:%M:%S"))
我不确定这是因为数据库大小(大于10GB)还是因为查询样式
我应该像datetime(2013、04、03、14、52、50)或“2013-04-03 14:52:50”那样使用datetime吗?Peewee将接受
datetime
对象作为输入,因此您无需进行任何转换。我猜问题是你缺少一个索引
尝试:
不需要将
datetime
对象格式化为字符串。SQLAlchemy知道如何处理datetime
对象本身。如果查询需要很长时间,那么这通常是数据库结构的问题。是否有一个包含插入的列的索引?您不需要格式化datetime——peewee将处理python datetime对象。我猜您可能需要添加一个索引。这正是事实,Peewee没有将该列视为索引,因此需要花费大量时间来收集数据。谢谢
class Products(BaseModel):
id = IntegerField(primary_key=True) # <-- You should specify a Primary Key
name= CharField()
inserted = DateTimeField(index=True) # <-- this should be an index
Products.select().where(Products.inserted >datetime(2013, 04, 03, 14, 52, 50))
# assuming your db is named "db"
db.create_index(Products, [Products.inserted])