while循环内的sql查询使我的应用程序非常慢

时间:2018-07-28 22:40:29

标签: php mysql mysqli

while循环内的Sql查询使我的应用程序非常慢。

<ul>    
<?php 
$sql1 = "SELECT cat_id,cat_name FROM all_services where uni_id='$uni_id'";
$sql_query = mysqli_query($conn,$sql1);
while($display_class = mysqli_fetch_assoc($sql_query))
{
$class_id = $display_class['cat_id'];
?>
<li>
<i class="fa fa-plus"></i>
<label>
<input  id="1" data-id="1" type="checkbox" value="<?php echo "$class_id";   ?>"/><?php echo $display_class['cat_name']; ?>
</label>
<ul>
<?php  $sql2 = mysqli_query($conn,"SELECT roll_no,st_name,st_mobile,st_dob FROM student_info where school_uni_id='$uni_id' and st_class='$class_id' order by roll_no + 0 ASC, st_name ASC"); 
while($display_stu = mysqli_fetch_assoc($sql2))
{
?>
<li>
<label>
<input class="treenode" id="xnode-0-1-1" data-id="custom-0-1-1" type="checkbox" value="<?php echo $display_stu['st_mobile']; ?>,<?php echo $display_stu['st_dob']; ?>" /><?php echo $display_stu['roll_no']; ?>&nbsp;&nbsp;<?php echo $display_stu['st_name']; ?>
</label>
</li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>

两个sql查询都相互链接,我需要第一个查询$class_id中的$sql1才能放入第二个查询,即$sql2

在while循环内部循环时,速度非常慢。

如果查询结果最多为1000行,但超过2000行时,应用程序将变得非常慢,它可以正常工作。如何使我的应用程序快速运行?

2 个答案:

答案 0 :(得分:0)

///您没有了解MySQL数据库大小和服务器配置,但按照以下步骤操作可以加快while循环

//请在uni_id表中添加MySQL索引all_services

//我在$ sql2上做了一些更改

$ sql2 = mysqli_query($ conn,“ SELECT roll_no,st_name,st_mobile,st_dob FROM student_info,其中school_uni_id ='$ uni_id'由roll_no ASC排序”);

//请在school_uni_id表中添加MySQL索引roll_nostudent_info

答案 1 :(得分:-1)

您需要使用查询参数对页面进行分页。
一会儿没用:您可以以相同的方式直接获取结果。