减少请求并优化动态调用

时间:2014-10-25 00:07:48

标签: php mysql query-optimization

我在one.com上托管,并且最近遇到了由于来自同一IP地址的请求过多导致用户被阻止访问我的网站的问题。我认为原因是我的笨拙编程(在调用数据库时)。我在do-while循环中动态调用了几个连接,如下所示:

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


$unit_nr_var = $unit_nr_var1.'.';
$rs_nr = 1;
$te_unit_id_var = $unit_nr_var.$rs_nr;

mysql_select_db($database_db_stat, $db_stat);
$query_rs_theory = sprintf("SELECT * FROM tbl_exercise_theory WHERE tet_exercise_id = %s", GetSQLValueString($unit_nr_var1, "text"));
$rs_theory = mysql_query($query_rs_theory, $db_stat) or die(mysql_error());
$row_rs_theory = mysql_fetch_assoc($rs_theory);
$totalRows_rs_theory = mysql_num_rows($rs_theory);

for($i = 0; $i <= 9; $i++) {

$_cook_id = $_COOKIE['MM_cookieUserID'];

mysql_select_db($database_db_stat, $db_stat);
$query_rs_unit_[$i] = sprintf("SELECT te.*, tp.tp_answer FROM tbl_exercise te LEFT OUTER JOIN tbl_progression tp ON (tp.tp_user_id = %s AND tp.tp_exe_id = te.te_id) WHERE te_unit_id = %s ORDER BY te_id ASC", GetSQLValueString($_cook_id, "text"),     GetSQLValueString($te_unit_id_var, "text"));
$rs_unit_[$i] = mysql_query($query_rs_unit_[$i], $db_stat) or die(mysql_error());
$row_rs_unit_[$i] = mysql_fetch_assoc($rs_unit_[$i]);
$totalRows_rs_unit_[$i] = mysql_num_rows($rs_unit_[$i]);

mysql_select_db($database_db_stat, $db_stat);
$query_rs_name_[$i] = sprintf("SELECT * FROM tbl_exercise_name WHERE ten_exercise_id = %s", GetSQLValueString($te_unit_id_var, "text"));
$rs_name_[$i] = mysql_query($query_rs_name_[$i], $db_stat) or die(mysql_error());
$row_rs_name_[$i] = mysql_fetch_assoc($rs_name_[$i]);
$totalRows_rs_name_[$i] = mysql_num_rows($rs_name_[$i]);

mysql_select_db($database_db_stat, $db_stat);
$query_Recordset[$i] = sprintf("SELECT * FROM tbl_exercise WHERE te_unit_id = %s ORDER BY     te_id ASC", GetSQLValueString($te_unit_id_var, "text"));
$Recordset[$i] = mysql_query($query_Recordset[$i], $db_stat) or die(mysql_error());
$row_Recordset[$i] = mysql_fetch_assoc($Recordset[$i]);
$totalRows_Recordset[$i] = mysql_num_rows($Recordset[$i]);

    $rs_nr = $rs_nr + 1;
    $te_unit_id_var = $unit_nr_var.$rs_nr;


if (($totalRows_Recordset[$i] <= 0))
{
    $i = 10;
}

}

我知道这可能不是一个有效的方法来获取可变数量的表,这取决于页面变量。那么问题是:我该如何优化这个?

0 个答案:

没有答案