php - What is the most efficient way to exclude records with a particular ID in a MySQL query for random records? -


i have api endpoint returns x amount of random posts table called "posts". endpoint used mobile app. retreives random posts doing select count(*) on posts table , returning amount of posts in table. enters loop in which, @ start of each loop, random number 0 count(*) generated. random post obtained using handy offset. loop goes until x amount of random posts obtained.

pseudocode:

function getrandposts :  $numofposts = queryexecute("select count(*) posts");  (iterations < numofrandompostsneeded) {     $randomnumber = rand(0, $numofposts)      $randompost = queryexecute("select * posts limit 1, offset $randomnumber")   } 

now, each call getrandposts function want them retrieve unique post wasn't retrieved before. current getrandposts call , pasts calls.

i've thought of several ways of going doing this:

  1. i store id's of seen posts in array in app , send array getrandposts function. getrandposts function uses "not in" clause.

    function getrandposts(array $idsofpostsalreadyseen) : ... ...  $randompost = queryexecute("     select * posts     id not in $idsofpostsalreadyseen     limit 1, offset $randomnumber")  $idsofpostsalreadyseen.addtoarray($randompost.id) ... 
  2. after each random post retrieved, save post id table called "seenposts" there column called idofapp used distinguish users.

    function getrandposts(string appinstallid) : ... ...   $randompost = queryexecute("     select posts.* posts     join seenposts.postid seenposts     on posts.id = seenposts.postid     seenposts.postid = null , seenposts.appinstallid = $appinstallid     limit 1, offset $randomnumber")  queryexecute("insert seenposts set postid = $randompost.id, appinstallid = $appinstallid") ... 

i'd ask idea of, idea number one, creating temporary table array , using join temp table vs using array.

consider idea that, @ most, 600 posts have excluded given user (so array of 600 ids in case of first idea).


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -