SQL --- paging query

Paging is actually from that part of the result set to intercept what is currently required to show.

 

Why the need for paging query? When using a SELECT query result set if a large amount of data, such as there are tens of thousands of records, all one-time query results will become very slow, so need to use paging query.

 

Pagination queries in several ways:

1, defining + offset (LIMIT ... OFFSET ...)

First determination result pagesize page number to be displayed, then the set pagesize LIMIT, then this page of the index pageIndex, OFFSET value calculated pagesize * (pageindex-1).

SELECT * FROM user
ORDER BY id
LIMIT 10 OFFSET 20;

Cons: When the offset is greater than LIMIT, low query efficiency.

 

2, defines a screening +

For example, we want to take the 10 rows from the start line 10000, we can put greater than or equal to 000 rows of data and sorting out the check, and then remove the first 10 rows.

SELECT * FROM user
WHERE id >=10000
ORDER BY id
LIMIT 10;

This query can greatly optimize the search speed can be substantially completed within a few tens of milliseconds.

Cons: Only use id to know exactly the situation.

 

Guess you like

Origin www.cnblogs.com/HuZihu/p/12467147.html