How to quickly query 10 million records in Mysql

How to quickly query 10 million records in Mysql

Normal paging query

When we encounter big data queries in our daily work, our first reaction is to use paging queries.

MySQL supports the limit statement to select a specified number of data, while Oracle can use rownum to select

The mysql paging query statement is as follows:

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset
  • The first parameter is used to specify the offset of the first returned record row.
  • The second parameter specifies the maximum number of rows to return.
    • When the offset is the same, the larger the amount of data, the longer it takes
    • When the amount of data is the same, the larger the offset, the longer it takes.

How to optimize

From the above summary, we can clearly see that when the offset is large and the amount of data is large, the query time is still quite long, so we will start to optimize these two types.

Large offset

Using subquery

We can first locate the id of the offset position and then query the data

select * from test limit 1000000,10
select id from test limit 1000000,1
select * from test where id>=(select id from test limit 1000000,1)limit 10

Through execution, we can find that the first one takes the longest time, the third one is slightly better than the first one, and the subquery is faster when using the index.

But it only applies to cases where the id is incremented

Use id limitation

This method has higher requirements. The ID must be continuously increasing, and the range of the ID must be calculated, and then between is used. The SQL is as follows:

select * from test where id between 1000000 and 1000100 limit 100;
select * from test where id>=1000000 limit 100

Results are fast

Here, limit is used to limit the number of entries, and no offset is used.

Optimizing the problem of large data volume

  • The amount of data returned will also directly affect the speed
  • Reducing unnecessary columns will significantly improve query efficiency

This is the end of this article about how to quickly query 10 million records in MySQL. For more information about MySQL quick query, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of MySQL database tens of millions of data query and storage
  • Optimizing query speed of MySQL with tens of millions of data using indexes
  • Summary of SQL query optimization knowledge points for MySQL tens of millions of big data
  • Detailed explanation of 30 SQL query optimization techniques for MySQL tens of millions of large data
  • Optimizing the performance of paging query for MySQL with tens of millions of data
  • Detailed explanation of the batch query design pattern for MySQL sharding to achieve distributed storage of millions of records

<<:  Detailed explanation of how Vue returns values ​​to dynamically generate forms and submit data

>>:  Summary of the use of CSS scope (style splitting)

Recommend

Detailed explanation of concat related functions in MySQL

1. concat() function Function: Concatenate multip...

Minimalistic website design examples

Web Application Class 1. DownForEveryoneOrJustMe ...

Bootstrap 3.0 study notes grid system principle

Through the brief introduction in the previous tw...

MySQL 8.0.25 installation and configuration tutorial under Linux

The latest tutorial for installing MySQL 8.0.25 o...

Detailed explanation of formatting numbers in MySQL

Recently, due to work needs, I need to format num...

Four ways to combine CSS and HTML

(1) Each HTML tag has an attribute style, which c...

Object-Oriented Programming with XHTML and CSS

<br />If only XHTML and CSS were object-orie...

How to correctly modify the ROOT password in MySql8.0 and above versions

Deployment environment: Installation version red ...

Tutorial on installing MySQL with Docker and implementing remote connection

Pull the image docker pull mysql View the complet...

Media query combined with rem layout in CSS3 to adapt to mobile screens

CSS3 syntax: (1rem = 100px for a 750px design) @m...

Detailed explanation of Docker Swarm concepts and usage

Docker Swarm is a container cluster management se...

Detailed tutorial on installing nvidia driver + CUDA + cuDNN in Ubuntu 16.04

Preparation 1. Check whether the GPU supports CUD...

Solution for FileZilla 425 Unable to connect to FTP (Alibaba Cloud Server)

Alibaba Cloud Server cannot connect to FTP FileZi...

How to introduce pictures more elegantly in Vue pages

Table of contents Error demonstration By computed...

Implementation of CSS border length control function

In the past, when I needed the border length to b...