? < PHP // Get the English strings random function getNickname ( $ length ) { $ STRs = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm" ; $ name = substr ( str_shuffle ( $ STRs ), the mt_rand (0, strlen ( $ STRs ) -11), $ length ); return $ name ; } // Set the timeout the set_time_limit (3600 ); the ini_set ( 'the memory_limit', '1024M' ); // link database $ PDO = new new the PDO ( 'MySQL: Host = 127.0.0.1; dbname = 1703a', 'the root', ' the root ' ); //sql前半部分 $sql = 'insert into user (username,nickname,fname,create_at) values '; //循环拼接 for($i=1;$i<=100000;$i++){ $nickname = getNickname(6); $fname = getNickname(10); $sql .= "('".$i."[email protected]','".$nickname."','".$fname."','".time()."'),"; } // remove excess comma $ last_sql = substr ( $ SQL , 0, -1 ); // echo $ last_sql; // final SQL $ PDO -> Exec ( $ last_sql );
-------------------------------
Run the code Note that, except where the operation code is marked red also note the following
10W storage of data given as follows:
The reason is: max_allowed_packet setting mysql is too small to record write failed
The solution is:
1, view the current maximum number of M support
show VARIABLES like '%max_allowed_packet%';
He expressed support for the 1M, so need to be modified
2, Review: set global max_allowed_packet = 20 * 1024 * 1024 is changed here 20M
So 100W of data storage should be how to deal with it?
Just set set global max_allowed_packet = 100 * 1024 * 1024 to
After setting To turn off Navicat re-enter the execution to see results
----------------------------------------
Slow queries on state show VARIABLES LIKE 'slow_query_log%';
Open Set slow query SET GLOBAL slow_query_log = 'ON';
Set slow query more than 1 second recording SET GLOBAL long_query_time = 1
After setting To turn off Navicat re-enter the execution to see results
----------------------------------------
For the optimization of one million data table consider indexing, if the query is still a little long time, consider the storage engine into InnoDb