2010-10-26 66 views
0

我管理的是一個相當大的流量網站。在每個頁面上,我們通過運行以下代碼來跟蹤訪客在線。然後我們有一個cron刪除超過X分鐘的記錄。在線訪問大型流量網站登錄訪問者

if(!isset($_SESSION[users_online_id])) { 
if(!(date('H')=='02' && date("i") >= 37 && date("i") <= 45)) { 
# NEW VISITOR - DOESN'T HAVE A USER_ONLINE_ID 
    $update_query = "INSERT INTO online SET 
    ip_address = '".$visitors_ip_address."', 
    datetime = now(), 
    user_id = ".$visitor_id.", 
    page = '".escape($_SERVER[REQUEST_URI])."', 
    area = '".$this_area."', 
    type = ".$visitor_type; 
    mysql_query($update_query); 

    # SET THEIR SESSION VAR FOR NEXT PAGE CHECKING 
    $_SESSION[users_online_id] = mysql_insert_id(); 
} 
    } 
    else{ 
# USER HAS A SESSION 

# CHECK THAT THE RECORD STILL EXIST 
$check_record = "SELECT COUNT(1) FROM online WHERE id = ".$_SESSION[users_online_id]; 
$check_record = mysql_query($check_record); 

if(mysql_result($check_record,0) > 0){ 
    # IF RECORD STILL EXISIT (WAS NOT DELETED) UPDATE IT 
    $update_query = "UPDATE online SET 
    datetime = now(), 
    user_id = ".$visitor_id.", 
    page = '".escape($_SERVER[REQUEST_URI])."', 
    type = '".$visitor_type."', 
    area = '".$this_area."', 
    ip_address= '".$visitors_ip_address."' 
    WHERE id = '".$_SESSION[users_online_id]."'"; 
    mysql_query($update_query); 
} 
else if(!(date('H')=='02' && date("i") >= 37 && date("i") <= 45)){ 
    # IF RECORD WAS DELETED (TO LONG ON 1 PAGE), INSERT A NEW RECORD 
    $update_query = "INSERT INTO online SET 
    ip_address = '".$visitors_ip_address."', 
    datetime = now(), 
    user_id = ".$visitor_id.", 
    page = '".escape($_SERVER[REQUEST_URI])."', 
    area = '".$this_area."', 
    type = ".$visitor_type; 
    mysql_query($update_query); 
    $_SESSION[users_online_id] = mysql_insert_id(); 
} 
     } 

做一些服務表現的監測,我注意到,與此表進行交互查詢開始花費更長的時間來執行。關於如何改進的任何想法?

回答

1

您可以消除SELECT查詢並使用UPDATE查詢和mysql_affected_rows()函數。

0

小了點,但是......

$ _ SERVER [ 'REQUEST_URI']是7倍高於$ _SERVER更快[REQUEST_URI]

同時確保ID的設定作爲主鍵。

+0

同樣$ _SESSION ['users_online_id'] – 2010-10-26 15:14:13