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

How to configure SSL certificate in nginx to implement https service

In the previous article, after using openssl to g...

How to implement on-demand import and global import in element-plus

Table of contents Import on demand: Global Import...

CSS3 achieves flippable hover effect

CSS3 implements a flippable hover effect. The spe...

Solution for multiple Docker containers not having the same port number

Background In Docker, four containers are created...

Solution to the error when installing Docker on CentOS version

1. Version Information # cat /etc/system-release ...

How to install JDK8 on Windows

1. Download: http://www.oracle.com/technetwork/ja...

Pure CSS to change the color of the picture

The css technique for changing the color of an im...

Implementation of Single Div drawing techniques in CSS

You can often see articles about CSS drawing, suc...

In-depth explanation of slots and filters in Vue

Table of contents Slots What are slots? Slot Cont...

JavaScript ES6 Module Detailed Explanation

Table of contents 0. What is Module 1.Module load...

Nginx rewrite regular matching rewriting method example

Nginx's rewrite function supports regular mat...

How to enable remote access permissions in MYSQL

1. Log in to MySQL database mysql -u root -p View...