When I was in the MySQL optimization, casually built a temporary table, generate 200W data, but do not understand, generated records are duplicated, but also more, puzzled
Thank you, gentlemen.
$conn=new mysqli("localhost","root","","test");$conn->query("set names utf8");function str_rand($num){ $str="abcdefghijkmnpqrstuvwxyz0123456789"; $return_str=""; for($i=0;$i<$num;$i++){ $return_str.=substr($str,rand(0,33),1); } return $return_str;}$sql="insert into news (id,title,author,keywords,descrition) values (?,?,?,?,?)";$mysqli_stmt=$conn->prepare($sql);for($i=1;$i<=2000000;$i++){ $title=str_rand(rand(3,30)); $author=str_rand(rand(3,20)); $keywords=str_rand(rand(10,100)); $descrition=str_rand(rand(30,255)); $mysqli_stmt->bind_param("issss",$i,$title,$author,$keywords,$descrition); $res=$mysqli_stmt->execute();}$mysqli_stmt->close();$conn->close();
Note: The execution time is too long, certainly more than the maximum execution time of PHP, modified the configuration file, inserted data, 100 records later, such as query title= ' xxx ' field, can query out more than 1.4W
Reply content:
When I was in the MySQL optimization, casually built a temporary table, generate 200W data, but do not understand, generated records are duplicated, but also more, puzzled
Thank you, gentlemen.
$conn=new mysqli("localhost","root","","test");$conn->query("set names utf8");function str_rand($num){ $str="abcdefghijkmnpqrstuvwxyz0123456789"; $return_str=""; for($i=0;$i<$num;$i++){ $return_str.=substr($str,rand(0,33),1); } return $return_str;}$sql="insert into news (id,title,author,keywords,descrition) values (?,?,?,?,?)";$mysqli_stmt=$conn->prepare($sql);for($i=1;$i<=2000000;$i++){ $title=str_rand(rand(3,30)); $author=str_rand(rand(3,20)); $keywords=str_rand(rand(10,100)); $descrition=str_rand(rand(30,255)); $mysqli_stmt->bind_param("issss",$i,$title,$author,$keywords,$descrition); $res=$mysqli_stmt->execute();}$mysqli_stmt->close();$conn->close();
Note: The execution time is too long, certainly more than the maximum execution time of PHP, modified the configuration file, inserted data, 100 records later, such as query title= ' xxx ' field, can query out more than 1.4W
PHP
rand()
is a pseudo-random algorithm, the data generated by the law.
Give you a real randomly generated data distribution map and a PHP rand()
generated data distribution map, the results at a glance.
True random: results are evenly distributed
PHP rand()
: There are obvious stripes (regularity)
And with Mt_rand () there is no such problem.
PHP7 's Random_int () will be better than Mt_rand ().
If you need to produce a random sequence without duplicates, it is generally a sequential sequence that is stored in a list. Then through pseudo-random algorithm to obtain index, take out the value, or write a shuffle method to disrupt the continuous sequence, by pseudo-random algorithm control shuffle, and then sequential read.
Of course, the most important thing about pseudo-random algorithms is the selection of seeds. The most common source of seed is, of course, the current timestamp. There can also be other ways that these online lots of articles about random number seeds can be seen.
Refer to the PHP manual for an introduction to the Mt_rand () function:
Many of the old libc random number generators have some uncertainties and unknown characteristics and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is informally used to replace it. The function uses the known characteristics of the»mersenne Twister as a random number generator, which can produce random values four times times faster than Rand () provided by LIBC.
You need to replace rand () with Mt_rand (), and you can use a GUID to generate unique data.