如何使用php-jquery加速数千个php循环


How to speed up thousands of php loop using php jquery

最近我开发了一个用于处理学生成绩的应用程序。算法如下:

算法:

get list of all students from mysql
  if found
    get list of subject from mysql
      get sum of marks from mysql for each student and subject
      calculate pass,fail,grade point etc.
      insert total marks into another table
    //end subject list
  error msg if no student
// end

这需要大量的时间。

现在我想加快使用jQuery的速度。

例如,

将所有学生id和主题id加载到jQuery数组中。然后使用jQuery循环或任何其他方式向PHP发送请求以处理结果。

你说的没有多大意义。然而,从上面的算法描述来看,你似乎在选择学生、科目、循环中的分数,并为每个学生的科目分数点击数据库。相反,使用表联接(left joininner join等)

SELECT s.*, sb.*, SUM(m.value)
FROM students AS s
  LEFT JOIN subjects as sb ON s.id = sb.student_id
  INNER JOIN marks AS m ON sb.id = m.subject_id
GROUP BY s.id, sb.id

使用此查询,您将用数据库中的一个计算查询替换1000个查询。MySql将以更优化的方式运行查询,并且需要更少的资源。

很难说您真正的问题是什么,但听起来您应该做的第一件事就是避免这些循环。也许您可以使用一些存储过程将部分逻辑移到数据库中?

拥有大量数据库快照可能会显著影响性能。