[Solved] Alternative for Rand function in MySQL

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Ordering by Random Function (query line: 12): Ordering by RAND will cause performance degradation as the data grows. An alternative to fetch one random result is to pick a random number in the application and use the following \u003ca target\u003d"_blank" href\u003d"http://www.eversql.com/faster-pagination-in-mysql-why-order-by-with-limit-and-offset-is-slow/"\u003eseek method\u003c/a\u003e
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `jobs_feed` ADD INDEX `jobs_feed_idx_check_pars_similarjob_is_updated` (`check_parsed`,`similarjob_status`,`is_updated`);
The optimized query:
SELECT
        jobs_feed.id,
        jobs_feed.postalCode,
        jobs_feed.location 
    FROM
        jobs_feed 
    WHERE
        jobs_feed.check_parsed = 1 
        AND jobs_feed.similarjob_status = 0 
        AND jobs_feed.is_updated != 0 
    ORDER BY
        RAND() LIMIT 1

Related Articles



* original question posted on StackOverflow here.