我正在尝试从 PHP 访问 MySql 中非常大的表,但执行时间非常大(有时超过 10 分钟),我尝试使用限制和偏移来减少执行时间,但他们没有帮助。我将限制设置为 1,这样一旦找到该行,它就会停止搜索更多行,并且我使用了 Offset 以便下次它搜索行时,从到达的最后一行开始搜索:
$offset1 = 0;
for ($date = mktime($hour_i,$minute_i,0,$month_i,$day_i,$year_i); $date <= mktime($hour_f,$minute_f,0,$month_f,$day_f,$year_f); $date = mktime($hour,$minute+$time_step,0,$month,$day,$year))
{
$hour = date('H',$date);
$minute = date('i',$date);
$second = date('S', $date);
$day = date('d',$date);
$month = date('m',$date);
$year = date('Y',$date);
?>
<?php
$Ensoleillement_Query = mysql_query("SELECT Moyenne, @row := @row + 1 AS ROW FROM mesure_ensoleillement,(SELECT @row := 0) r WHERE IdMesure IN(SELECT IdMesures FROM mesures WHERE IdPasserelle='".$PERSIL."' and ColonneEnregistrement='IdEnsoleillement')
and hour(DateMesure)= ".$hour."
and minute(DateMesure) = ".$minute."
and year(DateMesure)= ".$year."
and month(DateMesure)= ".$month."
and day(DateMesure)= ".$day." LIMIT 1 OFFSET ".$offset1." ");
if (!$Ensoleillement_Query) {
die('Query failure Query: ' . mysql_error());
}
if(mysql_num_rows($Ensoleillement_Query) == 0) die("There is no Data in the Database corresponding to that date or site");
$Ensoleillement[$i] = mysql_result($Ensoleillement_Query,0,0);
$offset1 = mysql_result($Ensoleillement_Query,0,1);
$offset1 = $offset1-1;
}