Jump to content

Help - ORDER by RAND() CPU USAGE


defonic

Recommended Posts

Hello All,

 

Having issues with a shared hosting account using to much RAM/CPU on my site

 

Currently using:

SELECT * FROM table ORDER by RAND() LIMIT 3

 

Problem is I have over 200,000 rows. Is there a way to...

(pseudo)

select * from table where id between max(id) and max(id) -50 ?

 

so it only returns the last 50 rows then do a order by rand() limit 3? Does this type of query exsist? and if so will it speed up the query by only selecting rand from last 50 rows?

 

any help appreciated!

Link to comment
Share on other sites

Thanks, I searched the site but time was not on my side!

 

I figured it out and might as well post the solution for future members.

 

SELECT * FROM table
WHERE id BETWEEN ( SELECT (MAX(id)-50) FROM table) AND ( SELECT (MAX(id)) FROM table)
ORDER BY RAND()
LIMIT 3

 

This will get the last 50 records (assuming there is auto increment on id) then randomly choose 3

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.