欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

将千万账号密码导入数据库脚本(增加高效版本)

程序员文章站 2022-05-24 15:57:49
...
跳至
$start_time=microtime_float();
$db=mysqli_connect('localhost','root','root','test');
$file=fopen('f:/10-million-combos.txt','rb');
mysqli_query($db,"DROP TABLE IF EXISTS `ten_million_account`;");
mysqli_query($db,"CREATE TABLE `test`.`ten_million_account`( `account_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, `account` VARCHAR(100) NOT NULL, `password` VARCHAR(200) NOT NULL, PRIMARY KEY (`account_id`) ) ENGINE=MYISAM CHARSET=utf8 COLLATE=utf8_general_ci; ");
//mysqli_query($db,"TRUNCATE ten_million_account;");
$sql = "INSERT INTO ten_million_account(`account`,`password`) VALUES('%s','%s');";
$count=1;
while($row=fgets($file)){
    echo $row;
    $data=preg_split("@\s.*?@",$row);
    if($data && !empty($data[0]) && !empty($data[1])){
        mysqli_query($db,sprintf($sql,$data[0],$data[1]));
    }
    $count++;
    unset($row);
    unset($data);
}
echo "\n run time:".(microtime_float()-$start_time);


function  microtime_float ()
{
    list( $usec ,  $sec ) =  explode ( " " ,  microtime ());
    return ((float) $usec  + (float) $sec );
}


2. [代码]改进高效版

$start_time=microtime_float();
$db=mysqli_connect('localhost','root','root','test');
$file=fopen('f:/10-million-combos.txt','rb');
mysqli_query($db,"DROP TABLE IF EXISTS `ten_million_account`;");
mysqli_query($db,"CREATE TABLE `test`.`ten_million_account`( `account_id` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, `account` VARCHAR(100) NOT NULL, `password` VARCHAR(200) NOT NULL, PRIMARY KEY (`account_id`) ) ENGINE=MYISAM CHARSET=utf8 COLLATE=utf8_general_ci; ");
//mysqli_query($db,"TRUNCATE ten_million_account;");
$sql = "INSERT INTO `test`.ten_million_account(`account`,`password`) VALUES %s;";
$count=1;
$conj=$values_str='';
while($row=fgets($file)){
    //echo $row;
    $data=preg_split("@\s.*?@",addslashes($row));
    if($data && !empty($data[0]) && !empty($data[1])){
        //mysqli_query($db,sprintf($sql,$data[0],$data[1]));
        $values_str.=$conj."('".$data[0]."','".$data[1]."')";
        $conj=',';
    }
    unset($row);
    unset($data);
    if(($count%10000)==0){
        if($values_str){
            echo $count."\n";
            if(!mysqli_query($db,sprintf($sql,$values_str))){
                echo sprintf($sql,$values_str)."\n";
                exit;
            }
        }else{
            exit;
        }
        $values_str=$conj='';
    }
    $count++;
}
if($values_str){
    mysqli_query($db,sprintf($sql,$values_str));
}
echo "\n run time:".(microtime_float()-$start_time);


function  microtime_float ()
{
    list( $usec ,  $sec ) =  explode ( " " ,  microtime ());
    return ((float) $usec  + (float) $sec );
}