FlaskSQLAlchemy查询非常慢

时间:2018-11-25 12:32:32

标签: python sqlalchemy flask-sqlalchemy

使用FlaskSQLAlchemy查询功能时,我遇到了与性能有关的问题。数据库大小为ca。 400 MB,包含所有元素的查询将花费近100秒。

以下是查询:

start_time = time.time()
results = DataPoint.query.all()
end_time = time.time()
print("All Datapoints: ", results)
print("Number of elements:", len(results))
print("Time elapsed: ", end_time-start_time)

输出:Number of elements: 5871988 Time elapsed: 96.98983788490295

其中DataPoint为:

class DataPoint(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      sensor_type = db.Column(db.String(75), nullable=False)
      sensor_number = db.Column(db.Integer)
      serial_number = db.Column(db.Float, db.ForeignKey('sensor_box.serial_number'), nullable=False)
      sensor_value = db.Column(db.Float, nullable=False)
      date_time = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

这种长时间持续正常吗,还是我做错了什么?

预先感谢

0 个答案:

没有答案