简体   繁体   English

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

[英]How to speed up thousands of php loop using php jquery

Recently I have develop an apps for processing student result.最近我开发了一个应用程序来处理学生成绩。 The algorithm is below:算法如下:

Algorithm:算法:

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

this takes huge time.这需要大量时间。

now I like to speed up with jQuery.现在我喜欢用 jQuery 加速。

For example,例如,

load all student id and subject id into jQuery array.将所有学生 id 和主题 id 加载到 jQuery 数组中。 then send a request to PHP to process result using jQuery loop or any other way.然后向 PHP 发送请求以使用 jQuery 循环或任何其他方式处理结果。

What you are saying does not make much sense.你说的没有多大意义。 However, from your algorithm description above it looks like you are selecting students, subjects, marks in loops and hitting your DB for each student's subject's marks.但是,从您上面的算法描述来看,您似乎正在选择学生、科目、循环中的分数,并为每个学生科目的分数打入您的数据库。 Instead, use table joins ( left join , inner join , etc.).相反,使用表连接( 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

With this query you are replacing your 1000 queries with one in database calculation query.使用此查询,您将用数据库计算查询中的一个查询替换 1000 个查询。 MySql will run the query in more optimised manner and require less resources. MySql 将以更优化的方式运行查询并且需要更少的资源。

It's pretty hard to say what is your real problem, but it sounds like first thing you should do is to avoid those loops.很难说你真正的问题是什么,但听起来你应该做的第一件事就是避免这些循环。 Maybe you could move part of this logic to the database using some stored procedures?也许您可以使用一些存储过程将此逻辑的一部分移动到数据库中?

Having loads of database shots may affect performance significantly.拥有大量数据库镜头可能会显着影响性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM