Php randomly records mysqlrand () causes a CPU usage. Baidu consulted some materials and combined with some of its own experience, adopted the following solution: Copy the code as follows: $ idlist; for ($ i1; $ i20; $ I ++) {if ($ i1) {$ idlistmt_rand (3. Baidu consulted some materials and combined with some of its own experiences, adopted the following solutions:
The code is as follows:
$ Idlist = '';
For ($ I = 1; $ I <= 20; $ I ++ ){
If ($ I = 1 ){
$ Idlist = mt_rand (3,25216 );
}
Else {
$ Idlist = $ idlist. ','. mt_rand (3,25216 );
}
}
$ Query = "select * from table where id in ($ idlist) LIMIT 0, 10 ";
The principle is actually very simple. it is to generate a group of random IDs and then retrieve the records corresponding to this group of IDS. after such optimization, the page opening speed is much faster, the CPU usage is almost zero ~ By the way, I optimized the SQL Server site!
Speed comparison:
It takes more than 400 milliseconds to use rand () to retrieve 10 random records, and the above method only takes about 0.6 milliseconds. The difference is too big !!!
Pipeline code: $ idlist = ''; for ($ I = 1; $ I = 20; $ I ++) {if ($ I = 1) {$ idlist = mt_rand (3 ,...