MySQL select 10 random rows from 600K rows fast

后端 未结 26 2911
粉色の甜心
粉色の甜心 2020-11-21 05:06

How can I best write a query that selects 10 rows randomly from a total of 600k?

26条回答
  •  温柔的废话
    2020-11-21 05:38

    Well if you have no gaps in your keys and they are all numeric you can calculate random numbers and select those lines. but this will probably not be the case.

    So one solution would be the following:

    SELECT * FROM table WHERE key >= FLOOR(RAND()*MAX(id)) LIMIT 1
    

    which will basically ensure that you get a random number in the range of your keys and then you select the next best which is greater. you have to do this 10 times.

    however this is NOT really random because your keys will most likely not be distributed evenly.

    It's really a big problem and not easy to solve fulfilling all the requirements, MySQL's rand() is the best you can get if you really want 10 random rows.

    There is however another solution which is fast but also has a trade off when it comes to randomness, but may suit you better. Read about it here: How can i optimize MySQL's ORDER BY RAND() function?

    Question is how random do you need it to be.

    Can you explain a bit more so I can give you a good solution.

    For example a company I worked with had a solution where they needed absolute randomness extremely fast. They ended up with pre-populating the database with random values that were selected descending and set to different random values afterwards again.

    If you hardly ever update you could also fill an incrementing id so you have no gaps and just can calculate random keys before selecting... It depends on the use case!

提交回复
热议问题