循环为32列生成随机单词/datetime,将100万条记录插入mysql的速度非常慢

x33g5p2x  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(248)

我有一些php代码,我已经修改,它基本上有3个函数一个生成随机字母,其他2个生成随机日期或时间。然后我使用一个“for”循环来调用每个函数,构建一个要插入mysql“main”表的值数组。然而,这个过程似乎非常缓慢。另一方面,我在这个“main”表上有一个触发器,它将所有列连接到一个列中,并将其插入到另一个表中。任何帮助如何加快这一进程将不胜感激。
我想在每一列中填充100万条具有某种程度上唯一性的数据的记录,以测试一些搜索查询性能。代码如下:

function genWord($numberOfWords) {
        $words = [];
        for ($s = 0; $s<$numberOfWords; $s++){
            //for ($i=0; $i<$numberOfWords; $i++){
            $worLen = rand(3,10);
            $word = "";
            for($j=0; $j<$worLen; $j++){
                // Ascii table uppercase letters
                $chVal1 = rand(65,90);
                // Ascii table lowercase letters
                $chVal2 = rand(97,122);

                if ($j == 0) {
                    $picker = rand(0,1);
                    if($picker ===0 ) $ch = chr($chVal1);
                    else $ch = chr($chVal2);
                }else $ch = chr($chVal2);
                $word = $word.$ch;
            }
            $words[] = $word;
            //}
            $sentence = implode(" ", $words);
        }
        return $sentence;
    }

    function genDate() {
        //Generate Random Date format yyyy/mm/dd
        $randomDate = '';
        $year = rand(1950,2018);
        $month = rand(1, 12);
        $thirtyOneDayMonths = array ('1', '3', '5', '7', '8', '10', '12');
        $thirtyDayMonths = array ('4', '6', '9', '11');

        // 31 day months
        if (in_array($month, $thirtyOneDayMonths)) $day = rand(1,31);
        // 30 day months
        else if (in_array($month, $thirtyDayMonths)) $day = rand(1,30);
        // month equal to Feb
        else $day = rand(1,28);

        $month = str_pad($month, 2, '0', STR_PAD_LEFT);
        $day = str_pad($day, 2, '0', STR_PAD_LEFT);
        $randomDate = $year."-".$month."-".$day;
        return $randomDate;
    }

    function genTime() {
        // Generate Random Time hh/mm/ss
        $randomTime = '';
        $hour = rand(1,23);
        $min = rand(1,59);
        $sec = rand(1,59);
        $hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
        $min = str_pad($min, 2, '0', STR_PAD_LEFT);
        $sec = str_pad($sec, 2, '0', STR_PAD_LEFT);
        $randomTime = $hour.":".$min.":".$sec;
        return $randomTime;
    }

for($n=0; $n < 10000; $n++) { 
    $sql = "INSERT INTO table1( 
    col1, 
    col2, 
    col3, 
    col4, 
    col5, 
    col6, 
    col7, 
    col8, 
    col9, 
    col10, 
    col11, 
    col12, 
    col13, 
    col14, 
    col15, 
    col16, 
    col17, 
    col18, 
    col19, 
    col20, 
    col21, 
    col22, 
    col23, 
    col24, 
    col25, 
    col26, 
    col27, 
    col28, 
    col29, 
    col30, 
    col31, 
    col32)
    VALUES
    (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

    // Generate values for table1
    $insertValue = array();
    for($c = 0; $c < 32; $c++) {
        if ($c <=1) {
            $fileDate = genDate()." ".genTime();
            $insertValue[] = $fileDate;
        }
        else if ($c == 8) $insertValue[] = rand(1, 14);
        else if ($c == 10) $insertValue[] = 0;
        else if ($c == 14) $insertValue[] = rand(1, 20);
        else if ($c == 19) {
            $fileDate = genDate()." ".genTime();
            $insertValue[] = $fileDate;
        }
        else if ($c == 21) {
            $fileDate = genDate()." ".genTime();
            $insertValue[] = $fileDate;
        }
        else if ($c == 23) {
            $fileDate = genDate()." ".genTime();
            $insertValue[] = $fileDate;
        }
        else $insertValue[]= genWord(1);
    }

    //print_r($insertValue);

    if ($pdo) {
        // Table1
        $stmt=$pdo->prepare($sql);
        $stmt->execute($insertValue);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题