MySQL database has 10 tens of thousands of data, using rand () to extract random 10 records, resulting in high server CPU consumption until the crash ~ Baidu looked at some information, combined with some of their experience, the following solutions:
$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 very simple, is to produce a set of random IDs, and then retrieve this group of IDs corresponding records, after such optimization, the page opening speed significantly faster, the CPU occupancy rate is also small to almost 0, oh ~ the way to use this idea of SQL Server site also optimized a bit!
Speed comparison:
Using rand () to take 10 random records is more than 400 milliseconds, and using the above method as long as 0.6 milliseconds, the difference is too big!!!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.