求教导怎么优化算法
只是第1,2次选择。还要再做2次。后面基本一样重复第二次志愿的。100个学生运行数据大概花了6-7秒。求指导怎么可以优化下~~谢谢
if($_POST['cal'])
{
//--------------------------------------------第一志愿-------------------------------------------
$sql = "select * from hos;";
$res = mysql_query($sql)or die(mysql_error());
$info = mysql_fetch_array($res)or die(mysql_error());
//echo $info['total'];//得到总医院数
do{//循环1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
if($info['same']==0)//如果不分男女时
{
$hos_id = $info['id']; //医院ID号
//echo $hos_id.",";
$hos_total = $info['total']; //医院总需求人数
//echo $hos_total.".";
$sql_total = "select count(*) as total from stu where first='".$hos_id."' and get=0 ;"; //添该志愿的学生数
$res_total = mysql_query($sql_total);
$info_total = mysql_fetch_array($res_total); //得到填报该志愿的学生数
if($hos_total > $info_total['total'])
{$sql1 = "select id from stu where first='".$hos_id."' and get=0 ORDER BY score DESC ;";}
else {$sql1 = "select id from stu where first='".$hos_id."' and get=0 ORDER BY score DESC limit $hos_total;";}
$res1 = mysql_query($sql1);
$info1 = mysql_fetch_array($res1);
do//循环2
{
$stu_id = $info1['id'];
$sql2 = "UPDATE stu SET result='".$hos_id."',get=1 where id='".$stu_id."' ;";
$res2 = mysql_query($sql2);
}while($info1 = mysql_fetch_array($res1)); //循环2结束
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
else//如果区分男女-----$info['same']==1;
{
$hos_id = $info['id']; //医院ID号
//男生SQL
$hos_total_men = $info['men']; //医院总需求男生数
$sql_total_men = "select count(*) as total_men from stu where first='".$hos_id."' and sex='f' and get=0 ;"; //添该志愿的男学生数
$res_total_men = mysql_query($sql_total_men);
$info_total_men = mysql_fetch_array($res_total_men); //得到填报该志愿的男学生数
if($hos_total_men > $info_total_men['total_men'])
{$sql1_men = "select id from stu where first='".$hos_id."' and sex='m' and get=0 ORDER BY score DESC ;";}
else {$sql1_men = "select id from stu where first='".$hos_id."'and sex='m' and get=0 ORDER BY score DESC limit $hos_total_men;";}
$res1_men = mysql_query($sql1_men);
$info1_men = mysql_fetch_array($res1_men);
do//循环2
{
//男生更新
$stu_id_men = $info1_men['id'];
$sql2_men = "UPDATE stu SET result='".$hos_id."',get=1 where id='".$stu_id_men."' ;";
$res2_men = mysql_query($sql2_men);
}while($info1_men = mysql_fetch_array($res1_men));
//女生SQL
$hos_total_wom = $info['wom']; //医院总需求女生数
$sql_total_wom = "select count(*) as total_wom from stu where first='".$hos_id."' and sex='f' and get=0 ;"; //添该志愿的男学生数
$res_total_wom = mysql_query($sql_total_wom);
$info_total_wom = mysql_fetch_array($res_total_wom); //得到填报该志愿的女学生数
if($hos_total_wom > $info_total_wom['total_wom'])
{$sql1_wom = "select id from stu where first='".$hos_id."' and sex='f' and get=0 ORDER BY score DESC ;";}
else {$sql1_wom = "select id from stu where first='".$hos_id."' and sex='f' and get=0 ORDER BY score DESC limit $hos_total_wom;";}
$res1_wom = mysql_query($sql1_wom);
$info1_wom = mysql_fetch_array($res1_wom);
do//循环2
{
//女生更新
$stu_id_men = $info1_wom['id'];
$sql2_wom = "UPDATE stu SET result='".$hos_id."',get=1 where id='".$stu_id_wom."';";
$res2_wom = mysql_query($sql2_wom);
}while($info1_wom = mysql_fetch_array($res1_wom));
//循环2结束
}
}while($info = mysql_fetch_array($res)); //循环1结束
//--------------------------------------------第一志愿结束-------------------------------------------
//----------------------------------------------第二志愿---------------------------------------------
$s_sql = "select * from hos;";
$s_res = mysql_query($s_sql)or die(mysql_error());
$s_info = mysql_fetch_array($s_res)or die(mysql_error());
do{//循环1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//分界线
if($s_info['same']==0)//如果不分男女时
{
$s_hos_id = $s_info['id']; //医院ID号
$s_hos_total = $s_info['total']; //医院总需求人数
$s_sql_total = "select count(*) as total from stu where second='".$s_hos_id."' and get=0 ;"; //添该志愿的学生数
$s_res_total = mysql_query($s_sql_total);
$s_info_total = mysql_fetch_array($s_res_total); //得到第二志愿填报该志愿的学生数
$s_get_sql = "select count(*) as total from stu where result='".$s_hos_id."' ;"; //得到第一志愿已为HOS_ID的学生
$s_get_res = mysql_query($s_get_sql);
$s_get_info = mysql_fetch_array($s_get_res);
if($s_get_info['total'] == $s_hos_total){}
else
{
$xuqiu2 = $s_hos_toal-$s_get_info['total']; //还需要多少学生数
if( $xuqiu2 > $s_info_total['total']){$s_sql1 = "select id from stu where second='".$s_hos_id."' and get=0 ORDER BY score DESC ;";}
else {$s_sql1 = "select id from stu where second='".$s_hos_id."' and get=0 ORDER BY score DESC limit $xuqiu2;";}
$s_res1 = mysql_query($s_sql1);
$s_info1 = mysql_fetch_array($s_res1);
do//循环2
{
$s_stu_id = $s_info1['id'];
$s_sql2 = "UPDATE stu SET result='".$s_hos_id."',get=1 where id='".$s_stu_id."' ;";
$s_res2 = mysql_query($s_sql2);
}while($s_info1 = mysql_fetch_array($s_res1));
}
}//循环2结束
else//如果区分男女-----$info['same']==1;
{
$s_hos_id = $s_info['id']; //医院ID号
//男生SQL
$s_hos_total_men = $s_info['men']; //医院总需求男生数
$s_sql_total_men = "select count(*) as total_men from stu where second='".$s_hos_id."' and sex='m' and get=0 ;"; //添该志愿的男学生数
$s_res_total_men = mysql_query($s_sql_total_men);
$s_info_total_men = mysql_fetch_array($s_res_total_men); //得到填报该志愿的男学生数
$s_get_sql_men = "select count(*) as total from stu where result='".$s_hos_id."' and sex='m' ;"; //得到第一志愿已为HOS_ID的男学生
$s_get_res_men = mysql_query($s_get_sql_men);
$s_get_info_men = mysql_fetch_array($s_get_res_men);
if($s_get_info_men['total'] == $s_hos_total_men){}
else
{
$xuqiu2_men = $s_hos_toal_men-$s_get_info_men['total']; //还需要多少男学生数
if($xuqiu2_men > $s_info_total_men['total_men'])
{$s_sql1_men = "select id from stu where second='".$s_hos_id."' and sex='m' and get=0 ORDER BY score DESC ;";}
else {$s_sql1_men = "select id from stu where second='".$s_hos_id."'and sex='m' and get=0 ORDER BY score DESC limit $xuqiu2_men;";}
$s_res1_men = mysql_query($s_sql1_men);
$s_info1_men = mysql_fetch_array($s_res1_men);
do//循环2
{
//男生更新
$s_stu_id_men = $s_info1_men['id'];
$s_sql2_men = "UPDATE stu SET result='".$s_hos_id."',get=1 where id='".$s_stu_id_men."' ;";
$s_res2_men = mysql_query($s_sql2_men);
}while($s_info1_men = mysql_fetch_array($s_res1_men));
}
//女生SQL
$s_hos_total_wom = $s_info['wom']; //医院总需求女生数
$s_sql_total_wom = "select count(*) as total_wom from stu where second='".$s_hos_id."' and sex='f' and get=0 ;"; //添该志愿的女学生数
$s_res_total_wom = mysql_query($s_sql_total_wom);
$s_info_total_wom = mysql_fetch_array($s_res_total_wom); //得到填报该志愿的女学生数
$s_get_sql_wom = "select count(*) as total from stu where result='".$s_hos_id."' and sex='f' ;"; //得到第一志愿已为HOS_ID的女学生
$s_get_res_wom = mysql_query($s_get_sql_wom);
$s_get_info_wom = mysql_fetch_array($s_get_res_wom);
if($s_get_info_wom['total'] == $s_hos_total_wom){}
else
{
$xuqiu2_wom = $s_hos_toal_wom-$s_get_info_wom['total']; //还需要多少女学生数
if($xuqiu2_wom > $s_info_total_wom['total_wom'])
{$s_sql1_wom = "select id from stu where second='".$s_hos_id."' and sex='f' and get=0 ORDER BY score DESC ;";}
else {$s_sql1_wom = "select id from stu where second='".$s_hos_id."'and sex='f' and get=0 ORDER BY score DESC limit $xuqiu2_wom;";}
$s_res1_wom = mysql_query($s_sql1_wom);
$s_info1_wom = mysql_fetch_array($s_res1_wom);
do//循环2
{
//女生更新
$s_stu_id_wom = $s_info1_wom['id'];
$s_sql2_wom = "UPDATE stu SET result='".$s_hos_id."',get=1 where id='".$s_stu_id_wom."' ;";
$s_res2_wom = mysql_query($s_sql2_wom);
}while($s_info1_wom = mysql_fetch_array($s_res1_wom));
}
}
}while($info = mysql_fetch_array($res)); 优化 sql 算法
[解决办法]
update stu set result='$hos_id',get=1 where in (select id) and get=0
你 吧 $hos_id 改为你要那参数,然后去 SQL 直接执行下
[解决办法]
没有编辑权限,只好补充说明一下:
1.录取操作应该是不能中断的,一旦中途失败后果很严重(要是我们高考的录取系统会出这个问题那就好玩了)。所以需要用INNODB的表来存放学生数据,并且要用事务来保证更新操作的完整性。
2.这里假设楼主是需要按批次来进行更新的,如果没有这个限制,还可以进一步优化。
3.53行没用了,请删除之
4.对楼主提点建议。请将代码以代码格式发布,文本格式真的让人很抓狂。雷同的代码尽量封装成函数,咱们是程序员,不是编辑。函数体的代码尽量控制在100行以内,编写和调试都很简单,百万字的长篇小说都会有段落的。