Versión del método de generación de tablas de datos de millones (php + yii)

Se requieren tres pasos para generar millones de datos

Uno, crea la base de datos correspondiente

ps: Debido a la diferencia en el sistema, después de copiar, elimine el espacio y vuelva a agregarlo. De lo contrario, habrá errores gramaticales.

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. Código PHP bajo el marco Yii

 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;
    }

Tres, herramientas de datos, comando sql para copiar datos para generar tablas

Debido a problemas de rendimiento, la cantidad de datos generados por el código php es básicamente 2000 y se atascará.

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

Supongo que te gusta

Origin blog.csdn.net/weixin_43272542/article/details/114539311
Recomendado
Clasificación