Home > Database > Mysql Tutorial > body text

Mysql paging problem under millions of data

藏色散人
Release: 2019-04-17 17:23:34
forward
3734 people have browsed it

During the development process, we often use paging. The core technology is to use limit to read data. During the test process of using limit for paging, the following data was obtained:

select * from news order by id desc limit 0,10
耗时0.003秒
select * from news order by id desc limit 10000,10
耗时0.058秒
select * from news order by id desc limit 100000,10 
耗时0.575秒
select * from news order by id desc limit 1000000,10
耗时7.28秒
Copy after login

We were surprised to find that the larger the paging starting point of MySQL is when the data volume is large, the slower the query speed. The query speed for 1 million items has already been It takes 7 seconds. This is a value we cannot accept!

Improvement plan 1

select * from news 
where id >  (select id from news order by id desc  limit 1000000, 1)
order by id desc 
limit 0,10
Copy after login

The query time is 0.365 seconds, and the efficiency improvement is very obvious! ! What is the principle? ? ?

We used conditions to filter the id. In the subquery (select id from news order by id desc limit 1000000, 1), we only queried the id field. Compared with select * or select multiple fields Save a lot of query overhead!

Improvement Plan 2

Suitable for systems with continuous IDs, extremely fast!

select * from news 
where id  between 1000000 and 1000010 
order by id desc
Copy after login

Not suitable for queries with conditions and discontinuous IDs. high speed!

The above is the detailed content of Mysql paging problem under millions of data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:hcoder.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template