2015-05-24 36 views
2

我有10萬條記錄 我到處打電話200次從數據庫或公共變量大數據選擇

功能getNameProcess

PHP代碼與選擇從數據庫

function getNameProcess($id) 
{  
    $time1=microtime(true); 
    $baseClass = new MsDatabase(); 

    $query = "select CON_VALUE,CON_ID,CON_CATEGORY from content where CON_ID=$id and CON_VALUE<>'' and CON_CATEGORY='PRO_TITLE'"; 
    $res= $baseClass->query($query,WF_WORKFLOW_DB_NAME); 

    $time2=microtime(true); 
    $timeTotal=($time2-$time1); 
    echo $timeTotal; 
    return $res[0]["CON_VALUE"];   
} 

PHP代碼與選擇從市民的餐桌內容可變

$contentTable=array(); 
function getNameProcess($id) 
{ 
    $time1=microtime(true); 
    $baseClass = new MsDatabase(); 
    if(empty($GLOBALS['contentTable'])) 
    { 
     $query = "select CON_VALUE,CON_ID,CON_CATEGORY from content ";  
     $GLOBALS['contentTable']= $baseClass->query($query,WF_WORKFLOW_DB_NAME_MARKAZE);   
    }  
    foreach($GLOBALS['contentTable'] as $R) 
    { 
     if($R['CON_ID']==$id && $R['CON_VALUE']!='' && $R['CON_CATEGORY']=='PRO_TITLE') 
     { 
      $time2=microtime(true); 
      $timeTotal=($time2-$time1); 
      echo $timeTotal; 

     return $R["CON_VALUE"]; 
     } 
    } 
    return 0; 
} 

當使用數據庫獲取進程名$totalTime爲1.2二,在使用時酒館lic變量totalTime是3.5秒?

爲什麼我用公用變量$totalTime比使用數據庫時大?

如何減少$totalTime

感謝

+1

的第一個查詢只在表中選擇一個行,第二個選擇所有行。將所有行從數據庫複製到PHP需要更長的時間。 – Barmar

回答

1

爲了加快速度:

  1. 創建和指數CON_CATEGORY + CON_ID(或CON_ID + CON_CATEGORY取決於其他查詢)

  2. 變化您的查找代碼:

新代碼:

$contentTable=array(); 
function getNameProcess($id) 
{ 
    $time1=microtime(true); 
    if(empty($GLOBALS['contentTable'])) 
    { 
     $baseClass = new MsDatabase(); 
     $query = "select CON_VALUE,CON_ID from content WHERE CON_CATEGORY = 'PRO_TITLE'"; 
     $result= $baseClass->query($query,WF_WORKFLOW_DB_NAME_MARKAZE); 

     $GLOBALS['contentTable'] = array(); 
     foreach($result as $R) { 
      if ($R['CON_VALUE'] != '') $GLOBALS['contentTable'][$R['CON_ID']] = $R['CON_VALUE']; 
     } 
    } 

    $retval = 0; 
    if (isset($GLOBALS['contentTable'][$id])) { 
     $retval = $GLOBALS['contentTable'][$id]; 
    } 

    $time2=microtime(true); 
    $timeTotal=($time2-$time1); 
    echo $timeTotal; 

    return $retval; 
} 
  • 的第三種方式以優化被組合2種方法。首先有一些統計數據很重要:同一個id需要多少次?例如,如果要求一個ID平均20倍,你只需要10個查詢,而不是200
  • 像:

    $contentTable=array(); 
    function getNameProcess($id) 
    { 
        $time1=microtime(true); 
    
        if (isset($GLOBALS['contentTable'][$id])) { 
         $retval = $GLOBALS['contentTable'][$id]; 
        } 
        else { 
         $baseClass = new MsDatabase(); 
         $query = "select CON_VALUE,CON_ID,CON_CATEGORY from content where CON_ID=$id and CON_CATEGORY='PRO_TITLE'"; 
         $res= $baseClass->query($query,WF_WORKFLOW_DB_NAME); 
    
         $retval = ($res[0]['CON_VALUE'] == '' ? 0 : $res[0]['CON_VALUE']); 
         $GLOBALS['contentTable'][$res[0]['CON_ID']] = $retval; 
        } 
    
        $time2=microtime(true); 
        $timeTotal=($time2-$time1); 
        echo $timeTotal; 
    
        return $retval; 
    } 
    
    +0

    謝謝,這種方式很好,現在運行代碼800ms(wwwooowww)。 – ashkufaraz

    3

    這裏有兩個問題:

    1. 第一個版本只選定行從數據庫到PHP,所有行的第二個轉移轉移。複製所有數據需要時間。

    2. 第一個版本可以利用索引來加速查找選定的行。第二個版本讀取所有行。

    0

    此外,第二個版本需要迭代(foreach)所有元素。這需要更多時間。