I want to show a random entry from a database. I would like to show X the number of random entries if I choose. So I need to select the top X entries from a randomly selected list of identifiers
(There will be no more than 500 entries to choose from, unless the Earth grows significantly in size. There are currently 66 possibilities.)
This feature works, but how can I make it better?
/***************************************************/ /* RandomSite */ //****************/ // Returns an array of random site IDs or NULL /***************************************************/ function RandomSite($intNumberofSites = 1) { $arrOutput = NULL; //open the database GetDatabaseConnection('dev'); //inefficient //$strSQL = "SELECT id FROM site_info WHERE major <> 0 ORDER BY RAND() LIMIT ".$intNumberofSites.";"; //Not wonderfully random //$strSQL = "SELECT id FROM site_info WHERE major <> 0 AND id >= (SELECT FLOOR( COUNT(*) * RAND()) FROM site_info ) ORDER BY id LIMIT ".$intNumberofSites.";"; //Manual selection from available pool of candidates ?? Can I do this better ?? $strSQL = "SELECT id FROM site_info WHERE major <> 0;"; if (is_numeric($intNumberofSites)) { //excute my query $result = @mysql_query($strSQL); $i=-1; //create an array I can work with ?? Can I do this better ?? while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $arrResult[$i++] = $row[0]; } //mix them up shuffle($arrResult); //take the first X number of results ?? Can I do this better ?? for ($i=0;$i<$intNumberofSites;$i++) { $arrOutput[$i] = $arrResult[$i]; } } return $arrOutput; }
UPDATE QUESTION: I know about ORDER BY RAND (), I just do not want to use it, because there are rumors that it is not the best for scaling and performance. I overly criticize my code. I have work, ORDER BY RAND () works, but can I do better?
. , , , , , , .
!
Rand orderby ? .. ...
- ( , )
Select * from site_info Order by Rand() LIMIT N
N - ...
? , .
rand().
array_rand :
$randKeys = array_rand($arrResult, $intNumberofSites); $arrOutput = array_intersect_key(array_flip($randKeys), $arrResult);
edit: = >
, , ORDER BY RAND() 66 , .
/ ( )?
, , max id , PHP, N 1 id, . , , , . .
, "id-type", , , . , 66 , , 1-66. , , , . , , PHP, " ?".
,
/***************************************************/ /* RandomSite1 */ //****************/ // Returns an array of random rec site IDs or NULL /***************************************************/ function RandomSite1($intNumberofSites = 1) { $arrOutput = NULL; GetDatabaseConnection('dev'); $strSQL = "SELECT id FROM site_info WHERE major <> 0;"; if (is_numeric($intNumberofSites)) { $result = @mysql_query($strSQL); $i=-1; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $arrResult[$i++] = $row[0]; } //mix them up shuffle($arrResult); for ($i=0;$i<$intNumberofSites;$i++) { $arrOutput[$i] = $arrResult[$i]; } } return $arrOutput; }
JPunyon
/***************************************************/ /* RandomSite2 */ //****************/ // Returns an array of random rec site IDs or NULL /***************************************************/ function RandomSite2($intNumberofSites = 1) { $arrOutput = NULL; GetDatabaseConnection('dev'); $strSQL = "SELECT id FROM site_info WHERE major<>0 ORDER BY RAND() LIMIT ".$intNumberofSites.";"; if (is_numeric($intNumberofSites)) { $result = @mysql_query($strSQL); $i=0; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $arrOutput[$i++] = $row[0]; } } return $arrOutput; }
OIS , .
/***************************************************/ /* RandomSite3 */ //****************/ // Returns an array of random rec site IDs or NULL /***************************************************/ function RandomSite3($intNumberofSites = 1) { $arrOutput = NULL; GetDatabaseConnection('dev'); $strSQL = "SELECT id FROM site_info WHERE major<>0;"; if (is_numeric($intNumberofSites)) { $result = @mysql_query($strSQL); $i=-1; while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $arrResult[$i++] = $row[0]; } $randKeys = array_rand($arrResult, $intNumberofSites); $arrOutput = array_intersect_key($randKeys, $arrResult); } return $arrOutput; }
10 000 , 2 . . 3 , .
. 2- , array_rand , . .
mysql_query("SELECT id FROM site_info WHERE major <> 0 ORDER BY RAND() LIMIT $intNumberofSites")
, JPunyon :)
:
SELECT @nv := @min + (RAND() * (@max - @min)) / @lc, ( SELECT id FROM site_info FORCE INDEX (primary) WHERE id > @nv ORDER BY id LIMIT 1 ), @max, @min := @nv, @lc := @lc - 1 FROM ( SELECT @min := MIN(id) FROM site_info ) rmin, ( SELECT @max := MAX(id) FROM site_info ) rmax, ( SELECT @lc := 5 ) l, site_info LIMIT 5
.
, , , , id.
, .
rand() (, MySQL)...
SELECT id, rand() as rand_idx FROM site_info WHERE major <> 0 ORDER BY rand_idx LIMIT x;
JPunyon. ORDER BY RAND() LIMIT $N. , $arrResult, () , MySQL RAND().
ORDER BY RAND() LIMIT $N
$arrResult
function getSites ( $numSites = 5 ) { // Sanitize $numSites if necessary $result = mysql_query("SELECT id FROM site_info WHERE major <> 0 " ."ORDER BY RAND() LIMIT $numSites"); $arrResult = array(); while ( $row = mysql_fetch_array($result,MYSQL_NUM) ) { $arrResult[] = $row; } return $arrResult; }
Source: https://habr.com/ru/post/1703376/More articles:Evenly spaced HTML list markup for columns - htmlWhy can't I access combobox in the callback method in Flash CS4? - flashWhen is it advisable to use AJAX? - ajaxHow do you set the correct width of a dynamic UITextField before assigning text? - flexSSRS WinForm Report Viewer Actions - winformsShould I add AJAX logic to my PHP classes / scripts? - jqueryRun the SSIS package created using VS2008 on SQL Server 2005 - sql-server-2005Why does the full-text string search "FDR" not return results in MySQL? - mysqlThe correct way to clear a constant thread in C # - multithreadingWhy is my asp.net caching throwing an exception? - cachingAll Articles