PHP imports 4 million of data to MySQL

Source: Internet
Author: User

We need to make an urgent request today,Algorithm4 million of the data is calculated (only Nick is included), and a customized database needs to be imported.

32 databases on MySQL (icluster_1 to icluster_32, calculate the remainder Based on Nick's MD5 value and then shard)

Table Structure:

 
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

Because of the sub-database service. It seems that the MySQL load command or mysqldump cannot be used to import data. You can only write a PHP script to execute the insert loop.

<? 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, 'ur data; Switch: 1 ') on duplicate key update enable = 1, preference = Concat (preference,'; Switch: 1') ");} If (In_array ($ I, array (1, 1000,100 00, 50000,100 000, 1000000,300 0000 ))) {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 \ "processed {$ I} \" your_mail <today.html ");}} fclose ($ file); mysql_close ($ con);?>

The time is still relatively long. It took 40 minutes :(

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.