3千万行以上あるmysqlからselectしたいのですが、下記のメッセージが出て読み込めません。
Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 211812368 bytes) in
環境はMAMPローカル環境でGoogle Chromeを使って表示しています。
「ini_set('memory_limit', '2G');」はやってみました。
やりたいことは300個程のデータを3千万行のデータから合うデータを探しすことなので最初のselect時に全部のデータを読み込まず、条件に合うデータだけを読み込めば良いのかと思い、色々やりましたがうまく出来ませんでした。
このエラーを回避するにはどうすればいいでしょうか。
php
1<?php 2require_once '../vendor/autoload.php'; 3use Cake\Chronos\Chronos; 4ini_set('memory_limit', '2G'); 5 6$UserName = 'root'; 7$PassWord = 'root'; 8 9$Database = new PDO('mysql:host=localhost;dbname=Encryption_Currency;charset=UTF8;', $UserName, $PassWord); 10$Database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 11 12try { 13$sql = "SELECT * FROM `bitflyerJPY`"; 14$statement = $Database->query($sql); 15// $statement->execute(); 16$records = $statement->fetchAll(PDO::FETCH_ASSOC); 17$statement = null; 18$Database = null; 19} catch (PDOException $error){ 20echo $error->getMessage(); 21exit; 22}; 23 24$bittrexFileName = "fullOrders_Ecxel.csv"; 25$bittrex = []; 26$bittrexTime = []; 27$bittrexUnixTime = []; 28 29function READ_CSV($file) { 30 if (pathinfo($file, PATHINFO_EXTENSION) === "csv") { 31 try { 32 $FileObject = new SplFileObject($file); 33 } catch (Exception $e) { 34 echo $e->getMessage(); 35 } 36 $FileObject->setFlags( 37 SplFileObject::READ_CSV 38 ); 39 foreach ($FileObject as $line) { 40 $array[] = $line; 41 } 42 return $array; 43 44 } else { 45 echo "CSVファイルではありません。"; 46 } 47} 48 49function CONVERT_INT_BY_MAP ($stringarray) { 50 $Int = intval($stringarray); 51 return $Int; 52} 53 54function CONVERT_UNIX ($timearray){ 55 $Time = new Chronos($timearray); 56 $UnixTime = $Time->timestamp; 57 return $UnixTime; 58} 59 60$bittrex = READ_CSV($bittrexFileName); 61 62for ($k = 0; $k < count($bittrex[0]); $k++){ 63 $key = $bittrex[0][$k]; 64 for ($i = 1; $i < count($bittrex)-1; $i++){ 65 $bittrexWithkey[$i][$key] = $bittrex[$i][$k]; 66 } 67} 68 69$bittrexTime = array_column($bittrexWithkey, 'Closed'); 70 71$bittrexUnixTime = array_map("CONVERT_UNIX", $bittrexTime); 72 73 74$BTCPrice =[]; 75for ($i = 0; $i < count($bittrexUnixTime); $i++){ 76 $Ikaarray = []; 77 for ($j = 0; $j < count($records); $j++) { 78 if ($records[$j]["Time"] <= $bittrexUnixTime[$i]) { 79 array_push($Ikaarray, $records[$j]["Time"]); 80 } 81 } 82 $BTCPrice[$i]["Time"] = $bittrexUnixTime[$i]; 83 $BTCPrice[$i]["Price"] = $records[count($Ikaarray)-1]["Price"]; 84} 85 86 87 88$filepath = "bittrexBTCJPY.csv"; 89 90try { 91 $FileObject = new SplFileObject($filepath, "w"); 92} catch (Exception $e) { 93 echo $e->getMessage(); 94} 95 96foreach ($BTCPrice as $row) { 97 $FileObject->fputcsv($row); 98} 99
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/23 04:11
2018/09/23 04:12
2018/09/23 04:31 編集
2018/09/23 07:21