{"id":121,"date":"2009-02-23T12:12:20","date_gmt":"2009-02-23T12:12:20","guid":{"rendered":"http:\/\/tw.newtonstudio.com\/?p=121"},"modified":"2009-02-23T12:12:20","modified_gmt":"2009-02-23T12:12:20","slug":"solution-to-replace-mysql-order-by-rand","status":"publish","type":"post","link":"https:\/\/tw.newtonstudio.com\/?p=121","title":{"rendered":"Solution to replace MYSQL &#8220;ORDER BY RAND()&#8221;"},"content":{"rendered":"<p>Some of you might be using the following directive to do Random Picking from your MYSQL database, which is mysql_query(&#8220;SELECT * FROM table ORDER BY RAND() LIMIT 10&#8243;);<\/p>\n<p>However, It is not a good way if your website is in a high-flow-situation, Using such directive might causing a big loading in your hosting machine, here&#8217;s the alternative way to replace such directive, (special thanks to Nicky Soo for providing this method),<\/p>\n<p>The code should be look like this:<\/p>\n<p>mysql_query(&#8221; SELECT *<br \/>\nFROM news AS r1 JOIN<br \/>\n(SELECT (RAND() *<br \/>\n(SELECT MAX(newsid) FROM news)) AS newsid) As r2<br \/>\nWHERE `display` =1<br \/>\nAND r1.newsid &gt;= r2.newsid<br \/>\nAND r1.counter &gt;50<br \/>\nLIMIT 0 , 10 \u00a0&#8220;)<\/p>\n<p>Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some of you might be using the following directive to d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-mysql-"],"_links":{"self":[{"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":1,"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tw.newtonstudio.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}