Mysql random recommendation of users not followed

Source: Internet
Author: User
We recommend that 10 users use orderbyrand () at random, but a large amount of data affects efficiency. $ SQL & #039; selectuidfrommemberorderbyrand () limit10 & #039; or random uid, then find from uid, eg: {code ...} the problem is that the uid in $ rand may not be in the member table ,... we recommend that 10 users use order by rand () at random, but a large amount of data affects efficiency.
$sql='select uid from member order by rand() limit 10';
Or random uid, and then find from uid, eg:

for($i=0;$i<10;$i++){$rand[]=mt_rand(1,100);}$sql="select uid from member where uid in (%s) and uid not in (select focus_uid from member_focus where uid ={$uid} ) limit 10";$sql = sprintf($sql,implode(',',$rand));

The problem is that the uid in $ rand may not be in the member table, so there will be less than 10. Is this true for Weibo? Are there other solutions?

Reply content:

We recommend that 10 users use order by rand () at random, but a large amount of data affects efficiency.
$sql='select uid from member order by rand() limit 10';
Or random uid, and then find from uid, eg:

for($i=0;$i<10;$i++){$rand[]=mt_rand(1,100);}$sql="select uid from member where uid in (%s) and uid not in (select focus_uid from member_focus where uid ={$uid} ) limit 10";$sql = sprintf($sql,implode(',',$rand));

The problem is that the uid in $ rand may not be in the member table, so there will be less than 10. Is this true for Weibo? Are there other solutions?

You can store a random number for a field and index the field. Then, a random number is generated during the query and several records greater than the random number can be obtained.

First, take the maxid and then randomly fetch 10 IDs to query and determine whether to pay attention to them. If the value is missing, then randomly fetch 10 IDs. Then, query, judge whether to pay attention to them, and finally splice them together.

  1. Total number of read tables select count (*) from tbl;
  2. Use a program to obtain random numbers ranging from 0 ~ Count (*)-1
  3. Query select * from tbl limit num, 1 in sequence;
  4. Remember to cache the queried data.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.