2010-03-29 91 views
1

我目前正在研究廣泛依賴於EAV模型的項目。兩個實體作爲它們的屬性都由一個模型單獨表示,有時擴展了其他模型(或者至少是基本模型)。使用PHP處理大型(對象)數據集

迄今爲止,這種方法運行良好,因爲應用程序的大部分區域只依賴過濾的實體集合,而不是整個數據集。

但是,現在我需要解析整個數據集(IE:所有實體及其所有屬性),以便根據屬性提供排序/過濾算法。

該應用程序目前由大約2200個實體組成,每個實體約有100個屬性。每個實體都由一個模型(例如Client_Model_Entity)表示,並且具有名爲$_attributes的受保護屬性,該屬性是一個Attribute對象的數組。

每個實體對象大約500KB,這會導致服務器上的令人難以置信的負載。對於2000個實體,這意味着單個任務需要1GB的RAM(以及大量的CPU時間)才能工作,這是不可接受的。

是否有任何模式或常用方法來迭代這些大型數據集?尋呼並不是一個真正的選擇,因爲爲了提供排序算法,所有東西都必須考慮在內。

編輯:一個代碼示例,希望能夠讓事情更清晰:

// code from the resource model 
for ($i=0,$n=count($rowset);$i<$n;++$i) 
{ 
    $clientEntity = new Client_Model_Entity($rowset[$i]); 
    // getattributes gets all possible attributes from the db and creates models for them 
    // this is actually the big resource hog, as one client can have 100 attributes 
    $clientEntity->getAttributes(); 
    $this->_rows[$i] = $clientEntity; 
    // memory usage has now increased by 500KB 
    echo $i . ' : ' . memory_get_usage() . '<br />'; 
} 

回答