MySQL randomly extracts a certain number of records

MySQL randomly extracts a certain number of records

In the past, I used to directly order by rand() to handle this kind of usage scenario, but the efficiency was really not satisfactory. So I encountered this scenario again recently and looked for a better solution online.

1.order by rand()

Writing method:

SELECT
  id
FROM
  `table`
ORDER BY
  rand()

The disadvantage of this writing method is that the rand function is executed multiple times in the order by, affecting efficiency.

2. max(id) * rand() using join

Writing method:

SELECT
  *
FROM
  `table` AS t1
JOIN (
  SELECT
    ROUND(
      RAND() * (
        (SELECT MAX(id) FROM `table`) - (SELECT MIN(id) FROM `table`)
      ) + (SELECT MIN(id) FROM `table`)
    ) AS id
) AS t2
WHERE
  t1.id >= t2.id
ORDER BY
  t1.id
LIMIT 1;

The big guys on the Internet all recommend the second way of writing, so I'd like to record it down. I feel that the maximum id and the minimum id can be calculated in the program.

The problem here is that if you take multiple records, they must be continuous, so if you don't want to take continuous data, you have to loop. However, this statement is extremely efficient, so loop query can be done.

The above are all the relevant knowledge points. Friends in need can learn from them. Thank you for your support of 123WORDPRESS.COM.

<<:  Element sample code to implement dynamic table

>>:  How to install babel using npm in vscode

Recommend

Use of select, distinct, and limit in MySQL

Table of contents 1. Introduction 2. select 2.1 Q...

Solution to mysql login warning problem

1. Introduction When we log in to MySQL, we often...

Ideas and methods for incremental backup of MySQL database

To perform incremental backup of the MySQL databa...

Implementation methods of common CSS3 animations

1. What is CSS Animations is a proposed module fo...

Example of how to change the domestic source in Ubuntu 18.04

Ubuntu's own source is from China, so the dow...

WeChat Mini Program implements the likes service

This article shares the specific code for the WeC...

Vue shopping cart case study

Table of contents 1. Shopping cart example 2. Cod...

uni-app WeChat applet authorization login implementation steps

Table of contents 1. Application and configuratio...

MySQL Basic Tutorial Part 1 MySQL5.7.18 Installation and Connection Tutorial

Starting from this article, a new series of artic...

How to set up a deployment project under Linux system

1. Modify the firewall settings and open the corr...

Description of the hr tag in various browsers

Generally, we rarely meet HR, but once we do, it c...

W3C Tutorial (5): W3C XML Activities

XML is designed to describe, store, transmit and ...

Fixed a bug caused by scrollbar occupying space

background This bug was caused by滾動條占據空間. I check...

Solution to MySQL remote connection failure

I have encountered the problem that MySQL can con...

Web page html special symbols html special characters comparison table

Special symbols Named Entities Decimal encoding S...