Million data table generation method (php+yii) version

Three steps are required to generate millions of data

One, create the corresponding database

ps: Because of the difference in the system, after copying, delete the space, and add it again. Otherwise there will be grammatical errors

CREATE TABLE `NewTable` (
`id`  int(11) NOT NULL AUTO_INCREMENT ,
`name`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`age`  int(11) NOT NULL ,
`phone`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`email`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`comment`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=1
ROW_FORMAT=DYNAMIC
;

2. PHP code under Yii framework

 public function actionMakeSql(){
        $i=1;
        $user_name= [
            '夏候望',
            '劲冥石',
            '济川烨',
            '欧阳劲',
            '林风眠',
            '伍忘因',
            '赵紫颖',
            '月明溪',
            '风川鸣',
            '欣月馨'
        ];

        $email=[
            "@qq.com",
            "@163.com",
            "@gmail.com",
            "@126.com",
            "@yahoo.com",
            "@xinlang.com",
            "@vip.com",
            "@foxmail.com"
        ];

        while ($i<2000) {
            $name_key = array_rand($user_name);
            $email_key = array_rand($email);
            $model = new BillionSql();
            $model->name = $user_name[$name_key];
            $model->phone = (string)rand(13202000000, 13202007109);
            $model->age = rand(20, 60);
            $model->email = $this->getRandomString(rand(5, 10)) . $email[$email_key];
            $model->comment = (string)$i;
            $status = $model->save();
            if (!$status) {
                print_r($model);
                break;
            }
            $i++;
        }
    }

    private function getRandomString($len,$char=null){
        if (is_null($char)){
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        //随机数播种生成器
        //根据参数生成随机数,mt_rand()使用
        //php4.2之后就自动播种,已淘汰
        mt_srand(10000000*(double)microtime());
        for ($i=0,$str=" ",$lc=strlen($chars)-1;$i<$len;$i++){
            $str.=$chars[mt_rand(0,$lc)];
        }
        return $str;
    }

Three, data tools, sql command to copy data to generate tables

Because of performance problems, the amount of data generated by the php code is basically 2000, and it will get stuck.

insert into test(name,age,email,phone,comment) select name,age,email,phone,comment from test;

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/114539311