Search


關於工作室

Newton 牛頓
Web Developer
PHP+MYSQL, Flash, Javascript, .NET Framework, Web Application.

找尋網頁設計專家?

牛頓工作室提供客製化網頁設計服務,想知道更多,歡迎來信洽詢。 E-mail Us!

近期文章

Sponsors

Plurk

Plurk.com

行事曆

主題

彙整

Sponsors

Blogroll

« PHP 日曆 | Home | 網站設計心得 »

Solution to replace MYSQL “ORDER BY RAND()”

By admin | 2月 23, 2009

Some of you might be using the following directive to do Random Picking from your MYSQL database, which is mysql_query(”SELECT * FROM table ORDER BY RAND() LIMIT 10″);

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’s the alternative way to replace such directive, (special thanks to Nicky Soo for providing this method),

The code should be look like this:

mysql_query(” SELECT *
FROM news AS r1 JOIN
(SELECT (RAND() *
(SELECT MAX(newsid) FROM news)) AS newsid) As r2
WHERE `display` =1
AND r1.newsid >= r2.newsid
AND r1.counter >50
LIMIT 0 , 10  ”)

Cheers!

Topics: MYSQL 常見問題 | No Comments »

Comments

You must be logged in to post a comment.