Table of Contents
" >2. [代码]改进高效版    
Home php教程 PHP源码 将千万账号密码导入数据库脚本(增加高效版本)

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

May 26, 2016 am 08:19 AM

跳至

$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 );
}
Copy after login


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 );
}
Copy after login

           

       

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)