今天要做個緊急需求,演算法算出了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分鐘:(