菜单

How does PHP implement random access to several records in the database?

2017年11月22日 - PHPer

In this paper, an example is given to illustrate how PHP implements random access to 20 records in a database of millions of data. To share with you for your reference, as follows:

Well, why do you write this?

When you go to a company interview, let write an algorithm out, then Mongolia, I use the algorithm in the development process? Not big data development, analysis.

Occasionally think of a pit father data, such as: PHP take millions of data in random 20 records, the algorithm used at that time.

1. first count the number of records in the statistical database (this is a data cache, such as 1 hours to re count),

2. according to the total number of bars, random 1 times, 1 times take out 20 records (of course, this is equivalent to paging, the request is not high, this is the fastest, I use this);

Another method, random 20 times, repeated 20 times.

For example:

$sum=800000; / / total number

Take / / cycle data

For ($i=1; $i< =20; $i++) {

$offset=mt_rand (1, $sum);

Repeat contrast / / control, realize your own

/ / database data, realize your own

$sql= * from table limit $offset “select, 1”;

Echo$sql;

}

Code is as follows

发表评论

电子邮件地址不会被公开。