3
我在我的MySQL数据库中有一个大表。我想查看其中的一列,并将其传递给一个函数,以查看它是否存在于另一个表中,如果不在此处创建它。在检索MySQL表时克服PHP内存耗尽或执行时间错误
但是,我总是面对内存耗尽或执行时间错误。
//Get my table
$records = DB::($table)->get();
//Check to see if it's fit my condition
foreach($records as $record){
Check_for_criteria($record['columnB']);
}
然而,当我这样做,我得到一个内存耗尽错误。
所以我有试过声明
//Get min and max id
$min = \DB::table($table)->min('id');
$max = \DB::table($table)->max('id');
//for loop to avoid memory problem
for($i = $min; $i<=$max; $i++){
$record = \DB::table($table)->where('id',$i)->first();
//To convert in array for the purpose of the check_for_criteria function
$record= get_object_vars($record);
Check_for_criteria($record['columnB']);
}
但会这样,我得到了一个最大执行时间错误。
FYI的check_for_criteria功能是一样的东西
check_for_criteria($record){
$user = User::where('record', $record)->first();
if(is_null($user)){
$nuser = new User;
$nuser->number = $record;
$nuser->save();
}
}
我知道我可以ini_set('memory_limit', -1);
但我宁愿找到一个方法来限制在某些方面我的内存使用情况,或至少它散布某种方式。
我应该在流量较低时在后台运行这些操作吗?任何其他建议?
怀疑你的MySQL查询没有得到很好的优化。直接执行查询需要多长时间才能执行。它返回多少数据?索引好吗? – AllInOne
'SELECT * FROM Table'需要0.0006秒 – Wistar
不错!你真的需要*吗?它返回多少数据?也许PHP解析它有困难? – AllInOne