php匯入400萬資料到mysql

來源:互聯網
上載者:User

今天要做個緊急需求,演算法算出了400萬的資料(只包含使用者nick),需要匯入定製化的資料庫

mysql上32個資料庫(icluster_1到icluster_32,根據nick的md5值求餘然後分庫的)

表結構:

CREATE TABLE `preferences` (  `nick` char(32) CHARACTER SET gbk COLLATE gbk_bin NOT NULL,  `enable` tinyint(1) DEFAULT '1',  `preference` varchar(1024) DEFAULT '',  PRIMARY KEY (`nick`)) ENGINE=MyISAM DEFAULT CHARSET=gbk

由於分庫的業務。貌似無法用mysql 的load命令或者mysqldump去匯入,只能寫php指令碼去迴圈insert啦

<?phpheader("content-type:text/html; charset=gbk");set_time_limit(0);ini_set('memory_limit', '1122M');$con = mysql_connect('ip', 'username', 'password');if (!$con)    exit("fail");mysql_query("set names 'gbk'");$arr = array();$file = fopen("/home/admin/personal_combo_nick_gbk", "r");$i = 0;while (!feof($file)) {    $line = trim(fgets($file));    if ($line) {        $i++;        $nk_md5 = md5($line);        $signature = hexdec(substr($nk_md5, -2));        $dbname = (1 + ($signature % 32));        mysql_select_db("icluster_" . $dbname, $con);        $result = mysql_query("INSERT INTO preferences(nick,enable,preference) VALUES('{$line}',1,'your data;switch:1') ON DUPLICATE KEY UPDATE enable=1,preference=concat(preference,';switch:1')");    }    if (in_array($i, array(1, 1000, 10000, 50000, 100000, 1000000, 3000000))) {        shell_exec("/home/admin/mail/bin/email -V -smtp-server server_ip -smtp-port 25  -html -from-addr your_mail -from-name taozi -no-encoding -subject \"已經處理{$i}條\" your_mail <today.html");    }}fclose($file);mysql_close($con);?>

時間還是比較長的,用了40分鐘:(

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.