{"id":819,"date":"2013-01-15T23:31:13","date_gmt":"2013-01-15T23:31:13","guid":{"rendered":"http:\/\/www.omniweb.com\/wordpress\/?p=819"},"modified":"2013-01-15T23:32:12","modified_gmt":"2013-01-15T23:32:12","slug":"randomize-mysql-results-the-right-way","status":"publish","type":"post","link":"https:\/\/www.omniweb.com\/wordpress\/?p=819","title":{"rendered":"Randomize MySQL results the right way?"},"content":{"rendered":"<p>I wanted to get some randomized results but read many warnings about the dangerous inefficiency of using MySQL&#8217;s &#8220;ORDER BY RAND()&#8221; functionality.  After searching around, I came across <a href=\"http:\/\/awesomism.co.uk\/68423#\">this solution<\/a> which seems very good!  Copied without permission for posterity.<\/p>\n<blockquote><p>Often around the internet I find people asking how to get random rows out of MySQL. Typically I see helpful people giving this:<br \/>\nSELECT * FROM table ORDER BY RAND();<\/p>\n<p>However what a lot of people don&#8217;t realise, while ok for systems will small tables and limited accessed, this as a huge performance hit when used like this.<br \/>\nThe correct way to order your results by a random number is:<br \/>\nSELECT *, RAND() as rand FROM table ORDER BY rand;<\/p>\n<p>Why? Well the answer is actually very simple, ORDER BY RAND() causes the order to be recalculated every time MySQL fetches a new row as for each row RAND() returns a different value. When used in the select the value is only calculated once per row and the results are only ordered once.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to get some randomized results but read many warnings about the dangerous inefficiency of using MySQL&#8217;s &#8220;ORDER BY RAND()&#8221; functionality. After searching around, I came across this solution which seems very good! Copied without permission for posterity. Often &hellip; <a href=\"https:\/\/www.omniweb.com\/wordpress\/?p=819\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/819"}],"collection":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=819"}],"version-history":[{"count":3,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/819\/revisions"}],"predecessor-version":[{"id":822,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/819\/revisions\/822"}],"wp:attachment":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}