简体   繁体   English

我如何将大量内容从多阵列数组存储到mysql数据库中?

[英]How can I store a huge amount of content from multidiemsional array into mysql database?

This is my array: 这是我的数组:

array(4) {
  [0]=>
  array(500000) {
    ["1234"]=>
    array(3) {
      ["fileName"]=>
      string(10) "monkey.jpg"
      ["path"]=>
      string(20) "animales/monkey.jpg"
      ["dateTime"]=>
       string(19) "2016-10-12 19:46:25"
    }
    ["3456"]=>
    array(3) {
      ["fileName"]=>
      string(9) "horse.jpg"
      ["path"]=>
      string(19) "animales/horse.jpg"
      ["dateTime"]=>
       string(19) "2016-10-12 19:46:25"
    }
    .... and many more...
  }
 ... and many more...
}

I want to store the content into my database: 我想将内容存储到我的数据库中:

$sql = "INSERT INTO files (id,fileName,path,dateTime) values(?,?,?,?) ";

foreach($array as $key => $value){
    if(is_array($value)){
        foreach($value as $key => $v){
            foreach($v as $k => $item){
                if(is_array($v)){
                    $s = str_replace("\\","",$v['dateTime']);
                    $d = strtotime($s);
                    $dateTime = date('Y.m.d H:i:s', $d);  
                    $q->execute(array($key,$v['fileName'],$v['path'],$dateTime));
                }
            }
        }
    }
}

My problem is, that I have over 500.000 entries. 我的问题是,我有超过500.000条目。 So my system crashes. 所以我的系统崩溃了。 I think it is because there are so many loops inside the loop. 我认为这是因为循环内有太多循环。 Is there a way to read the content with only one loop or some other way faster? 有没有一种方法可以仅通过一个循环或以其他方式更快地读取内容?

Note: the $array is a spliced array created like this ($array[] = array_splice($orinal_array, 0,count($original_array)); I actually did that to make the system faster 注意: $array是这样创建的拼接数组($ array [] = array_splice($ orinal_array,0,count($ original_array));实际上,这样做是为了使系统更快

Please have a look at this answer: 请看一下这个答案:

MYSQL import data from csv using LOAD DATA INFILE MYSQL使用LOAD DATA INFILE从csv导入数据

You should convert your data to csv and rely on LOAD DATA INFILE 您应该将数据转换为csv并依靠LOAD DATA INFILE

Not that you should upload to your mysql server the csv file to rely on this Mysql functionality 不是您应该将csv文件上传到mysql服务器以依赖于此Mysql功能

Using serialize or json_encode is probably a way to go. 使用序列化json_encode可能是一种方法。 This way, you won't have to traverse all elements and handle slashes, because all becomes a string that can be later read using json_decode or deserialize PHP functions. 这样,您就不必遍历所有元素并处理斜线,因为所有元素都变成了一个字符串,以后可以使用json_decode读取或反序列化PHP函数。

Side note: please use meaningful variable names so that people helping you won't have to figure out what you meant. 旁注:请使用有意义的变量名,以便帮助您的人们不必弄清楚您的意思。 ie: 即:

foreach($v as $k => $item){

is a bit worse than 比...差一点

foreach($fileCollection as $fileIterator => $fileDetails){

If you REALLY need to traverse all the data and store each file property in a separate column, all you need is 2x foreach (one for collection, and one for each file). 如果您确实需要遍历所有数据并将每个文件属性存储在单独的列中,则您需要的只是2x foreach(一个用于收集,一个用于每个文件)。

foreach($globalCollection as $fiveHundredThousandRows){ 
    foreach ($fiveHundredThousandRows as $fileIterator => $fileData){
        $timestamp = strtotime($fileData['dateTime']);
        $q->execute(array($fileIterator,$fileData['fileName'],$fileData['path'],date( 'Y.m.d H:i:s', $timestamp)));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM