큰 다운로드 사이트가 있습니다. 각 파일을 다운로드하는 고유 한 사람 수와 총 사람 수를 추적하기 위해 각 다운로드를 업데이트하는 코드가 있습니다.Mysql 카운터 코드
그러나 코드는 정상적으로 작동하지만 트래픽이 많을 때 코드가 사이트 속도를 늦추고 mysql 서버가 많은 리소스를 사용할 수 있도록 허용합니다. 이 코드가 최적화 될 수 있습니까?
나는
<?php
#Gathers the client info
$agent = $_SERVER['HTTP_USER_AGENT'];
$brows = explode(" ",$agent);
$ubr = "$brows[0]";
$exptime = time() + 200;
$var = time();
$uip = user_ip();
$del = mysql_query("DELETE FROM log_hits WHERE exptime < '".$var."'");
$hits = mysql_fetch_array(mysql_query("SELECT * FROM user_downloads WHERE id='".$file_id."'"));
#Process unique download count
$u_check = DB::FetchArray(DB::Query("SELECT COUNT(*) FROM log_hits where browser='".$ubr."' and uip='".$uip."' and file_id='".$file_id."'"));
if($file_check[0]=="0")
{
$res3 = DB::Query("INSERT INTO log_hits SET browser='".$ubr."', uip='".$uip."', exptime='".$exptime."', file_id='".$file_id."'");
$unique = $hits[day_unique] + 1;
}else
{
$unique = $hits[day_unique];
}
#update regular hits to the file,
$week = $hits[weekly] + 1;
$hour = $hits[this_hour] + 1;
$todayx = $hits[today_hits] + 1;
$total = $hits[total] + 1;
$month = $hits[month] + 1;
$res3 = DB::Query("UPDATE `user_downloads` SET `day_unique`='{$unique}', `weekly`='{$week}', `this_hour`='{$hour}', `month`='{$month}', `total`='{$total}' , `today_hits`='{$todayx}' WHERE `id`='".$file_id."'") or die(mysql_error());
?>
'$ _SERVER [ 'HTTP_USER_AGENT']'의 데이터를 이스케이프하고 배열 인덱스에 따옴표를 사용하십시오 :'$ this [ 'day_unique']'! – ComFreek
오, 알았어. 더 나은 성능을 위해 다른 작업을 수행 할 수 있습니까? –
$ mysql_real_escape_string ($ _ SERVER [ 'HTTP_USER_AGENT']); ' – ComFreek