MySQL randomly recommends not paying attention to users

Source: Internet
Author: User
Randomly recommended 10 users can use ORDER by rand (), but a large amount of data will affect efficiency
$sql='select uid from member order by rand() limit 10';
Or a random uid, and then look from the 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 the $rand may not be in the table member, so it is less than 10, Weibo is this? Is there any other plan?

Reply content:

Randomly recommended 10 users can use ORDER by rand (), but a large amount of data will affect efficiency
$sql='select uid from member order by rand() limit 10';
Or a random uid, and then look from the 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 the $rand may not be in the table member, so it is less than 10, Weibo is this? Is there any other plan?

You can Gencun a random number to a word and index the field, and then generate a random number on the query to get a number of records greater than this random number.

Take Maxid and then randomly take 10 ID, query, judge whether attention, if missing, then randomly fetch 10, query, judge whether attention, and finally pieced together.

    1. Reads the total amount of the table in select COUNT (*) from TBL;
    2. The program gets the random 10 numbers that are not duplicated, the number range is 0~count (*)-1
    3. Execute the query in turn select * from TBL limit num,1;
    4. Remember to cache the data you've queried.
  • 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.