将mysql_query与mysql_fetch_row一起使用时的性能问题

时间:2014-09-10 08:59:00

标签: php mysql

我对mySQL和php不太熟悉。帮助朋友解决他们的表现问题。

他们有一个名为report.php的文件,它查询数据库,然后输出查询中的所有行和列。

当多个用户执行报告时,此执行似乎会锁定数据库并导致CPU使用率达到峰值100%。我有什么明显的遗漏吗?

report.php的结构如下

include('../settings2.inc');

$type=$_GET['report'];


if(isset($_GET['report']))
{
   switch($type){
    case uuppgroup:     $export=mysql_query("select * from uuppgroupdetails order by id");
                        break;

    case Beneficiary:   $export=mysql_query("select * from uupp_ben_details order by ben_id");
                            break;

    (many other switch cases over here
..
..

}


$fields = mysql_num_fields ( $export );  

for ( $i = 0; $i < $fields; $i++ ) 
{     
  $header .= mysql_field_name( $export , $i ) . "\t"; 
}  

while( $row = mysql_fetch_row( $export ) ) 
{     
  $line = '';     
  foreach( $row as $value )     
  {                                                     
    if ( ( !isset( $value ) ) || ( $value == "" ) )         
    {             
       $value = "\t";         
    }         
    else         
    {             
      $value = str_replace( '"' , '""' , $value );             
      $value = '"' . $value . '"' . "\t";         
    }         
    $line .= $value;     
  }     
  $data .= trim( $line ) . "\n"; 
} 

$data = str_replace( "\r" , "" , $data );   }

$vExcelFileName=$type.".xls";
#$vExcelFileName="export". ".doc";
header("Content-type: application/x-ms-download"); //#-- build header to download the excel file
header("Content-Disposition: attachment; filename=$vExcelFileName");
header('Cache-Control: public');
print "$header\n$data"; 
/*
}

1 个答案:

答案 0 :(得分:-2)

确保包括:

mysql_close($dbcon);

如果这些文件是定期生成的,要停止不断获取数据库,如果每30分钟/小时运行一次CRON作业来生成这些文件,然后当有人请求它们时,他们就会这样做可在您选择的文件夹中找到,例如&#34; /downloads/reports/Beneficiary.pdf"

由于您的代码看起来很好(从我可以看到的部分),我最好的猜测是您的其他一些查询可能会破坏它。我唯一可以猜到的是,当多个用户同时从数据库请求数据时,它会在尝试处理时挂起(因此100%的CPU使用率)

相关问题