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

How to operate Docker and images

Find mirror We can search for images from the Doc...

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced componen...

Problems encountered in using MySQL

Here are some problems encountered in the use of ...

How to solve "Unable to start mysql service error 1069"

Today, when I was on the road, a colleague sent m...

Summary of new usage of vi (vim) under Linux

I have used the vi editor for several years, but ...

WeChat Mini Program QR Code Generation Tool weapp-qrcode Detailed Explanation

WeChat Mini Program - QR Code Generator Download:...

MySQL table return causes index invalidation case explanation

Introduction When the MySQL InnoDB engine queries...

Implementing long shadow of text in less in CSS3

This article mainly introduces how to implement l...

Implementation of TypeScript in React project

Table of contents 1. Introduction 2. Usage Statel...

JavaScript object-oriented implementation of magnifying glass case

This article shares the specific code of JavaScri...

Detailed explanation of MySQL InnoDB secondary index sorting example

Sorting Problem I recently read "45 Lectures...

Docker stop stops/remove deletes all containers

This article mainly introduces Docker stop/remove...

Example of implementing dashed border with html2canvas

html2canvas is a library that generates canvas fr...

Detailed explanation of non-parent-child component value transfer in Vue3

Table of contents App.vue sub1.vue sub2.vue Summa...