我有一个小型 webapp,它使用 Pyhon/Flask 和一个 MySQL 数据库来存储数据。我有一个学生数据库,大约有 3000 行。尝试加载该页面时,加载需要很长时间,有时甚至一分钟左右。它大约 20 秒,这真的很慢,我想知道是什么原因造成的。这是发出任何请求之前服务器的状态,当我尝试加载该站点时会发生这种情况。
正如我所说,这并没有太多的记录,我很困惑为什么这样无效。我正在使用带有Ver 14.14 Distrib 5.5.32, for debian-linux-gnu (x86_64) using readline 6.2
mysql 版本的 Ubuntu 12.04。其他查询运行良好,例如列出名字以某个字母开头的学生大约需要 2-3 秒,这是可以接受的。这显示了表格的一部分,所以我猜有些东西没有优化。
My.cnf 文件位于此处。我尝试了一些东西,在底部添加了一些行,但没有太大的成功。
实际查询由 sqlalchemy 完成,这是用于加载此的特定代码:
score = db.session.query(Scores.id).order_by(Scores.date.desc()).correlate(Students).filter(Students.email == Scores.email).limit(1)
students = db.session.query(Students, score.as_scalar()).filter_by(archive=0).order_by(Students.exam_date)
return render_template("students.html", students=students.all())
这似乎是生成的 sql:
SELECT student.id AS student_id, student.first_name AS student_first_name, student.middle_name AS student_middle_name, student.last_name AS student_last_name, student.email AS student_email, student.password AS student_password, student.address1 AS student_address1, student.address2 AS student_address2, student.city AS student_city, student.state AS student_state, student.zip AS student_zip, student.country AS student_country, student.phone AS student_phone, student.cell_phone AS student_cell_phone, student.active AS student_active, student.archive AS student_archive, student.imported AS student_imported, student.security_pin AS student_security_pin, (SELECT scores.id \nFROM scores \nWHERE student.email = scores.email ORDER BY scores.date DESC \n LIMIT 1) AS anon_1 \nFROM student \nWHERE student.archive = 0"
提前感谢您的时间和帮助!