MYSQL: Sequential Number Table

前端 未结 8 1819
清歌不尽
清歌不尽 2020-12-15 14:33

I am trying to get a sequential number table from 1 to 20 million. (or 0 to 20 million)

I am rather awestruck at how difficult it\'s been to get a MySQL-compatible s

8条回答
  •  北海茫月
    2020-12-15 15:32

    In response to Devon Bernard's answer, I decided to approach it using PDO Mysql PHP and use the concept of just a few queries. At first I tried to do it with just 1 big query but PHP ran out of memory with default settings, so I decided to tweak to run every 100,000th. Even if you allocate enough memory to hold, there is no significant improvement.

    $i = 1;
    $inserts = array();
    while($i <= 20000000) {
        $inserts[] = "($i)";
    
        if($i % 100000 == 0) {
            $dbh->beginTransaction();
            $query = "INSERT INTO numbers(i) VALUES " . implode(',', $inserts) . ";";
                $sth = $dbh->prepare($query);
                $sth->execute();
            $dbh->commit();
            $inserts = array();
        }
        $i +=1;
    }
    

提交回复
热议问题