Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为虚拟数据创建批处理MySQL插入脚本_Php_Mysql_Batch Processing - Fatal编程技术网

Php 为虚拟数据创建批处理MySQL插入脚本

Php 为虚拟数据创建批处理MySQL插入脚本,php,mysql,batch-processing,Php,Mysql,Batch Processing,例如,我需要在一些1M-50M的大型数据集上测试一些查询,而我在编写一个能够快速完成这些任务的过程时遇到了困难 我有下面的代码,它是从我在另一个SO帖子上找到的版本中稍微修改的: set_time_limit(0); $db_host = 'localhost'; $db_name = 'test'; $db_user = ''; $db_pass = ''; $entries = 1000000; $entry_words_min = 250; $entry_words_max = 10

例如,我需要在一些1M-50M的大型数据集上测试一些查询,而我在编写一个能够快速完成这些任务的过程时遇到了困难

我有下面的代码,它是从我在另一个SO帖子上找到的版本中稍微修改的:

set_time_limit(0);

$db_host = 'localhost';
$db_name = 'test';

$db_user = '';
$db_pass = '';

$entries = 1000000;
$entry_words_min = 250;
$entry_words_max = 1000;

/*
  End configuration
*/

function get_rand_word( $len_min, $len_max ) {
    for ( $i = 0; $i < ( rand( 0, $len_max - $len_min ) + $len_min ); $i++ ) {
        $word .= chr(rand(65, 90));
    }
    return $word;
}
function get_title() {
    for ( $i = 0; $i < ( rand( 4, 10 ) ); $i++ ) {
        $title .= get_rand_word( 2, 9 ) . ' ';
    }
    return $title;
}
function get_fulltext() {
    for ( $i = 0; $i < ( rand( 250, 500 ) ); $i++ ) {
        $fulltext .= get_rand_word( 2, 9 ) . ' ';
    }
    return $fulltext;
}

$dsn = 'mysql:dbname=' . $db_name . ';host=' . $db_host;

try {
    $dbh = new PDO($dsn, $db_user, $db_pass);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    die();
}

$sth = $dbh->prepare('INSERT INTO `sphinx` (`some_id`,`title`,`content`) VALUES (:some_id, :title, :content)');

$counter = 0;

for ( $i = 0; $i < $entries; $i++ ) {
    $sth->execute(array(
        ':some_id' => mt_rand(1,65000),
        ':title' => get_title(),
        ':content' => get_fulltext()
    ));
    $counter++;
}

echo $counter . ' rows inserted';

然而,这是相当缓慢的插入;仅仅插入500k行就花了几个小时。我知道我可以编写一个批插入查询,但我认为为一百万个插入编写一个查询不是一个好主意;我想它可以一次分解成10公里左右,但我想看看是否有更好/更干净的方法可用。

您可以使用。你可以做一个循环,根据需要伪造尽可能多的记录。所有数据都是随机数据。

首先,我建议在第n次插入后测量时间,比如说每10000个循环索引,以查看任何异常

$start = microtime(true);
for ($i = 0; $i < $entries; $i++) {
    $sth->execute(array(
        ':some_id' => mt_rand(1,65000),
        ':title' => get_title(),
        ':content' => get_fulltext()
    ));
    if ($i % 10000 === 9999) {
        $end = microtime(true);
        echo $i . ' ' . ($end - $start) . '<br>';
    }
    $counter++;
}
数据库类型、索引甚至硬盘驱动器也可能有问题。大多数已知类型比较:

创建CSV文件并使用加载数据INFILE@HoboSapiens我想问题在于用正确的数据填充CSV文件。我想我可以用PHP来做,但是Excel可能会更简单。用PHP编写CSV文件比在数据库中逐行插入要快一个数量级。LOAD DATA INFILE将提供类似的改进,特别是如果您先删除任何索引,然后重新创建它们。这是两个操作,但应该比你目前的方法快得多。谢谢你的链接,这似乎是一个很好的资源来填充正确的数据,但它比我现在做的更快吗?@Brett实际上,你想要插入大量的记录。。。这可能不会比您正在做的更快。我认为,如果您不关心数据复制,但关心数据量,那么可以插入5-10k条记录,然后插入sphinx中的“SELECT*”from“sphinx”`并运行几次。您可能需要选择要插入和排除id的值,假设这是一种可能的方法;尽管我可能倾向于使用csv方法,因为我认为这将是目前为止最快的。加载数据填充比使用任何类型的insert语句快20倍。但是你的文件会很大,我不知道制作和保存需要多长时间。然后,你可以制作一个更小的文件并多次加载。哈哈,我想我们会看到:D