简体   繁体   中英

using php insert huge amount of data into mysql

I would like to generate random strings and insert into database, the insertion should be efficient and robust. I have a generating strings function working like this:

public function getRandString(){
    $string= //generats a random string
    return $string;
}

db connection:

$con = mysql_connect($host, $user, $pass)
or die ( " Not able to connect to server ");
mysql_select_db($db_name);

Now I want to create a method that insert the random strings into database 1 millon rows at once with each row contains 2 column of random strings, how should I go about this?

thanks for help in advance.

最好创建随机字符串函数数据库端并运行INSERT ... SELECT查询一次以生成行。

Two approaches:

  1. Use php to create a sql file to import via the command line, phpmyadmin, sequelpro, tomcat, etc

    • open a file
    • append the INSERT INTO whatever VALUES ( 'randomshit', 'randomshit' ); however many times you want
    • close the file, use it to populate the db
  2. Use the connection you mentioned above:

    • create the INSERT INTO whatever VALUES( 'randomshit', 'randomshit' ); in batches of 25000

       $howMany = 40; // do this 40 times (40x25000 ) = 1,000,000 while( --$howmany ) { $random = ""; $randomMany = 25000; while( --$randomMany ) { $random += sprintf( "INSERT INTO whatever VALUES ('%s', '%s'); ", rand_text(), rand_text() ); } // you'll now have a big list (25000 ) of insert queries to insert mysql_query( $random ); } 

We can combine two tricks to generate a table of junk:

Create a table of single digits: https://stackoverflow.com/a/273703/260007

CREATE TABLE num (i int);
INSERT INTO num (i) VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);

Generate random strings:

INSERT INTO junktable
SELECT
  (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6)) AS aa,
  (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 6)) AS bb
FROM       num AS l1
INNER JOIN num AS l2
INNER JOIN num AS l3
INNER JOIN num AS l4
INNER JOIN num AS l5
INNER JOIN num AS l6
LIMIT 100; -- number of wanted rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM