The principle of limit in mysql

The principle of limit offset and count in mysql is to take out offset+count records first, then discard the previous offset records, and then read the latter count records, mainly because of the offset problem. So the larger the offset, the worse the performance.

Optimization:

select film_id,description from sakila.film order by title limit 10000,5;

1. Give an approximate range, add conditions to determine the range, and avoid offset

select film_id,description from sakila.film where film_id > 10000 order by title limit 5;

*This will also be done when paging a large table. It does not provide the total number of pages and the page skip function, only the next page button, which improves the efficiency of paging

2. When the table is very large, try to use index coverage scan to narrow down the scope of returning to the table, for example:

select film.film_id,film.description from sakila.film
inner join(
	select film_id from sakila.film  order by title limit 50,5
) as lim using(film_id);

The problem of mixing order by and limit

If there are many rows in the result set returned in the order by statement, the return result of the non-sorted column is uncertain, that is, random, so if the limit is used, the order of the result set returned each time is not fixed. If
necessary In order, you need to put all the fields that need to be queued into order by for sorting

Guess you like

Origin blog.csdn.net/m0_53121042/article/details/117458675