This article mainly introduces the method of PHP to randomly obtain 20 records from millions of data in the database, involving simple database query and random number operation related skills in PHP. Friends who need it can refer to it
1. First count the number of records in the database (this is used as a data cache, such as re-counting once every hour),
2. Based on the total number of records, randomly take out 20 records at once (of course This is equivalent to paging. If the requirements are not high, this is the fastest. This is what I use);
There is another method, randomize 20 times and repeat 20 times.
For example:
$sum=800000;//得到总条数 //循环取数据 for($i=1;$i<=20;$i++){ $offset=mt_rand(1,$sum); //控制重复对比的,请自行实现 //数据库取数据的,请自行实现 $sql="select * from table limit $offset,1"; echo $sql; }
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
Detailed explanation of the installation and use of extensions in the Yii framework of PHP_php tips
PHPHow to record the time spent on a page_php tips
PHP How to record the time spent on the page_php tips
The above is the detailed content of PHP method to randomly obtain 20 records from millions of data in the database. For more information, please follow other related articles on the PHP Chinese website!