How can i optimize MySQL's ORDER BY RAND() function?

前端 未结 8 2298
天命终不由人
天命终不由人 2020-11-22 00:58

I\'d like to optimize my queries so I look into mysql-slow.log.

Most of my slow queries contains ORDER BY RAND(). I cannot find a real solu

8条回答
  •  遥遥无期
    2020-11-22 01:17

    I am optimizing a lot of existing queries in my project. Quassnoi's solution has helped me speed up the queries a lot! However, I find it hard to incorporate the said solution in all queries, especially for complicated queries involving many subqueries on multiple large tables.

    So I am using a less optimized solution. Fundamentally it works the same way as Quassnoi's solution.

    SELECT  accomodation.ac_id,
            accomodation.ac_status,
            accomodation.ac_name,
            accomodation.ac_status,
            accomodation.ac_images
    FROM    accomodation, accomodation_category
    WHERE   accomodation.ac_status != 'draft'
            AND accomodation.ac_category = accomodation_category.acat_id
            AND accomodation_category.acat_slug != 'vendeglatohely'
            AND ac_images != 'b:0;'
            AND rand() <= $size * $factor / [accomodation_table_row_count]
    LIMIT $size
    

    $size * $factor / [accomodation_table_row_count] works out the probability of picking a random row. The rand() will generate a random number. The row will be selected if rand() is smaller or equals to the probability. This effectively performs a random selection to limit the table size. Since there is a chance it will return less than the defined limit count, we need to increase probability to ensure we are selecting enough rows. Hence we multiply $size by a $factor (I usually set $factor = 2, works in most cases). Finally we do the limit $size

    The problem now is working out the accomodation_table_row_count. If we know the table size, we COULD hard code the table size. This would run the fastest, but obviously this is not ideal. If you are using Myisam, getting table count is very efficient. Since I am using innodb, I am just doing a simple count+selection. In your case, it would look like this:

    SELECT  accomodation.ac_id,
            accomodation.ac_status,
            accomodation.ac_name,
            accomodation.ac_status,
            accomodation.ac_images
    FROM    accomodation, accomodation_category
    WHERE   accomodation.ac_status != 'draft'
            AND accomodation.ac_category = accomodation_category.acat_id
            AND accomodation_category.acat_slug != 'vendeglatohely'
            AND ac_images != 'b:0;'
            AND rand() <= $size * $factor / (select (SELECT count(*) FROM `accomodation`) * (SELECT count(*) FROM `accomodation_category`))
    LIMIT $size
    

    The tricky part is working out the right probability. As you can see the following code actually only calculates the rough temp table size (In fact, too rough!): (select (SELECT count(*) FROM accomodation) * (SELECT count(*) FROM accomodation_category)) But you can refine this logic to give a closer table size approximation. Note that it is better to OVER-select than to under-select rows. i.e. if the probability is set too low, you risk not selecting enough rows.

    This solution runs slower than Quassnoi's solution since we need to recalculate the table size. However, I find this coding a lot more manageable. This is a trade off between accuracy + performance vs coding complexity. Having said that, on large tables this is still by far faster than Order by Rand().

    Note: If the query logic permits, perform the random selection as early as possible before any join operations.

提交回复
热议问题