-2
파일이 1 000 000 인 CSV가 있고이 행을 PostgreSQL DB 테이블로 가져와야하므로 가능한 빨리 처리해야합니다.postgre로 빠른 가져 오기 CSV 파일
나는 발전기를 사용하려하고 fsync를 = 떨어져 있지만, 어쨌든 나는 시간을 단축 코드 또는 DB를 최적화하는 방법,
지금 시간이 17 초입니다 가져 동일한 결과를했다?
내 코드 :
<?php
$time_start = microtime(true);
ini_set('max_execution_time', 300);
require_once 'db-config.php';
function generateString($length = 15)
{
$chars = 'qwertyuioplkjhgfdsazxcvbnm';
$numChars = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++)
{
$string .= substr($chars, rand(1, $numChars) - 1, 1);
}
return $string;
}
$fileName = fopen("users.csv", 'a');
for ($i=0; $i < 1000000 ; $i++)
{
$firstName = generateString(15);
$lastName = generateString(15);
$age = rand(10, 90);
fputcsv($fileName, [$firstName, $lastName, (string)$age]);
}
fclose($fileName);
$sql = "COPY users FROM '/home/artur/Documents/Projects/test.com/users.csv' CSV;";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Done! ({$time} seconds)";
그리고 무엇이 문제입니까? – Epodax
이제 가져 오기 시간은 17 초입니다. 어떻게 코드를 최적화하여 DB를 줄일 수 있습니까? –
17,000 x 1 000 000 행. 정말 나쁜가요? – jarlh