Avatar of netslt
netslt
 asked on

get 25 random rows from huge mysql table - but fast....

Hi

How do i retrieve 25 random rows from a big mysql table, but fast?

The table has 100 mio rows - and the queries/solutions I tried (found by googling around) took very long or gave errors about the tmp table that mysql tried to create for the query.

Any ideas how to solve this?

(Some background information: I have multiple java clients accessing the table and they process the table data in batches of 25 rows. If I don't have random rows all clients process the same rows. I need multiple clients to speed things up, just one client would have weeks to process all rows.)

Thanks!
MySQL ServerSQL

Avatar of undefined
Last Comment
Cornelia Yoder

8/22/2022 - Mon
JimFive

A better solution would be to have your clients mark the rows they are working and have the clients select the next 25 unmarked rows.

Alternatively, give each client a number from 0 to numclients-1 and only allow them to get rows where the rowid mod numclients = their own number

Finally, to answer the asked question:
write a query that returns the top 25 rows ordered by a column of random numbers.
something like:
SELECT Rand(), * FROM TABLE order by 1 limit 1,25
Cornelia Yoder

Is there a primary key, and especially is it numeric?

If so, use php to generate 25 random numbers (very fast) and then retrieve those 25 rows with a single MySQL query.
netslt

ASKER
Thank you for the 2 comments.

I have to add that after processing each row has a field

processed = 1

as when it is not yet processed then it is

processed = 0

So I the SQL query is always "..... where processed = 0"

Over time many more rows have processed = 1, so it is difficult to access rows with a predefinded random number (yodercm solution) or with the mod numclients (JimFive solution).

So if you have another solution similar to that but that works with the "processed" flag, then please let me know....

JimFive: the query "SELECT Rand(), * FROM rowpool order by 1 limit 1,25;" gives me an error.

JimFive: I already had tried to mark the rows as soon as a client has got them, but its not fast enough, other clients can already get it while client1 gets it & updates the mark (there are a few miliseconds between retrieving and updating)

Thanks
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Cornelia Yoder

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
JimFive

I already had tried to mark the rows as soon as a client has got them, but its not fast enough
Mark the rows first with the client id then retrieve the rows to the client.

e.g.
update table set processing = myclientid

THEN
select from table where processing = myclientid.

JimFive: the query "SELECT Rand(), * FROM rowpool order by 1 limit 1,25;" gives me an error.
Ok, I looked it up, in mysql you use:  SELECT * FROM rowpool order by RAND() limit 25
Cornelia Yoder

RAND still doesn't solve the problem of a row never being selected.

And yes, fill in a client id at the time the row is created.  Let the client select it later for processing.
netslt

ASKER
SELECT * FROM rowpool order by RAND() limit 25 is correct but gives me an error:

Incorrect key file for table '/tmp/#sql_2b_0.MYI'; try to repair it.

I think it tries to generate a temp table but times out because of the amount of data...



Regarding "update table set processing = myclientid" - how do I update just 25 next records that do not have processed = 1 without the other clients interfering?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Tomas Helgi Johannsson

Hi!

Your N clients should process the data from first row to the last, dividing the rows into M chunks of equal size X rows.
For this you could create a sequence function (similar to Oracle's nextval ) that tells client A(i) how many rows has been processed and marks (sets the sequence to be seqno = seqno + Xrows + 1

Then each client can select their chunk of data by executing

select *
from table LIMIT secno , X

so for secno = 1000 and X = 100
you will get 100 rows starting from rowid = 1000 to 1100

Unfortunatly MySQL doesn't have this kind of sequence built in but here is a good solution that you could use as a substitute

http://www.microshell.com/database/mysql/emulating-nextval-function-to-get-sequence-in-mysql/

This will help your N clients to process X rows of your Y million row table fast and accurately and no rows will be processed twice or left out from the process.
Hope this helps.

Regards,
    Tomas Helgi
JimFive

Regarding "update table set processing = myclientid" - how do I update just 25 next records that do not have processed = 1 without the other clients interfering?
UPDATE tablename
SET processing = myclientID
WHERE tablename.processed = 0 and tablename.processing =0
LIMIT 25

Open in new window


Since this is a single statement, it is atomic and will set all 25 at once with no interference from the other clients.
Cornelia Yoder

>>>Regarding "update table set processing = myclientid" - how do I update just 25 next records that do not have processed = 1 without the other clients interfering?

Once you have a client id in each row, assigning that row to a particular client, each client just uses

SELECT ... WHERE processed = 0 AND clientid = (client id processing program) LIMIT 25
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck