PHP не хватает памяти при выполнении функции sql SELECT для небольшого набора данных - PullRequest
0 голосов
/ 02 мая 2020

Сам код работает просто отлично, но я думаю, что в моем коде может быть большая проблема с управлением памятью, потому что когда я запускал код в таблице с меньшим количеством записей (<100), он выполнялся идеально и возвращал результаты Я ожидал </p>

ВАЖНОЕ РЕДАКТИРОВАНИЕ: как ни странно, код отлично работает для 124 записей, когда я добавляю 125-ю запись, она вставляется в oop и вызывает проблему, я пробовал другую запись он все еще зацикливается

Я выделил 2 ГБ памяти для PHP, но до сих пор не хватает памяти.

Вот функция SQL, вызывающая проблему:

function getFeatureDecisionCountTable($filter_feature,$condition,$labels){
global $connect, $table_name;
global $label_name;
$featureTitles = getFeatureTitles();    // Returns a List of Features to go through them later

//  Removes the $label_name and Filter Features so we don't run through it again
unset($featureTitles[sizeof($featureTitles)-1]);

if(($filter_feature != "COMPLETE") OR ($condition != "COMPLETE")){
if (($key = array_search($filter_feature, $featureTitles)) !== false) {
    unset($featureTitles[$key]);
}
}

foreach($featureTitles as $feature){

    // Check if a filter & condition is set (Will return the complete table if not)
    if(($filter_feature != "COMPLETE") OR ($condition != "COMPLETE")){
    $sql = "SELECT " . $feature . ", COUNT(`" . $feature . "`) as " . $feature . "Count, $label_name FROM (SELECT " . $feature . ",$label_name FROM $table_name WHERE " . $filter_feature . " = '" . $condition . "') as " . $condition . "Table group by $label_name, " . $feature . " ORDER BY " . $feature;
    }
    else{
    $sql = "SELECT " . $feature . ", COUNT(`" . $feature . "`) as " . $feature . "Count, $label_name FROM $table_name group by $label_name, " . $feature . " ORDER BY " . $feature;
    }

$stmt = $connect->query($sql);
while ($row = $stmt->fetch()) {  
    // $table[$feature][$row[$feature]][$feature] = $row[$feature];
    $table[$feature][$row[$feature]][$row[$label_name]] = $row[$feature . "Count"];
}   
$stmt->closeCursor();

}

// Fill in the 0 Values
foreach(array_keys($table) as $i){
    foreach(array_keys($table[$i]) as $j){
        foreach($labels as $k){
        if(!isset($table[$i][$j][$k])) $table[$i][$j][$k] = 0;
        }
    }
}
// print_r($table);
return $table;
}  

А вот основная рекурсивная функция, которая ее использует

function generateTree($entropy, $labels , $filter='COMPLETE', $condition = 'COMPLETE', $dfcount=''){
  $tree = [];

  if($dfcount == ''){
    $dfcount = getFeatureDecisionCountTable($filter,$condition,$labels);
  }
  // Generates Entropy table and 
  $features_entropies = getEntropies($dfcount);
  $feature_gains = getGains($dfcount,$entropy,$features_entropies);   // Get and ordered list of Gains  
  $gain_keys = array_keys($feature_gains);
  $filter = end($gain_keys);  // Get The Feature with the highest Gain

  $dfcount_keys = array_keys($dfcount[$filter]);

foreach($dfcount_keys as $filter_value){
  $total_values = array_sum($dfcount[$filter][$filter_value]);
  $dfcount_filter_val = array_keys($dfcount[$filter][$filter_value]);
  foreach($dfcount_filter_val as $descision){
  if($dfcount[$filter][$filter_value][$descision] == $total_values) {
    // if the selected feature value will always have the same decision, add it as a leaf value to the tree
    $tree[$filter][$filter_value] = $descision;
    break;
  }
   // if the selected feature value is 0 it will be ignored, otherwise regenerate a tree for the selected feature value and add it as a branch to the final tree
  else if($dfcount[$filter][$filter_value][$descision] != "0") {
    $filter_dfcount = getFeatureDecisionCountTable($filter,$filter_value,$labels);   // Regenrate Feature Decision Value for the selected subset
    $filter_feature_entropy =  $features_entropies[$filter][$filter_value];   // Get the entropy for the selected feature value

    $tree[$filter][$filter_value] = generateTree($entropy,$labels,$filter,$filter_value,$filter_dfcount);
    break;
    }
  }
 }

 return $tree;
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...