Optimize DISTINCT / LIMIT MySQL Query for Pagination

I have the following SQL:

SELECT group_id FROM products WHERE category = 12345 GROUP BY group_id LIMIT 0, 10

or:

SELECT DISTINCT group_id FROM products WHERE category = 12345 LIMIT 0, 10

LIMIT is used for pagination. I want to know how I can avoid MySQL to search all rows until it finds 10 different group_id that I want. For example, if I did:

SELECT DISTINCT group_id FROM products WHERE category = 12345 LIMIT 200, 10

It scans the first 200 lines with category = 12345, how will it start collecting 10 different group_objects?

UPDATE: What if I created an index (group_id, category)?

+3
source share
1 answer

200 , 10. DISTINCT LIMIT. , , limit. , , , .

(BTW, , LIMIT 10 OFFSET 200 LIMIT 200, 10.)

+1

Source: https://habr.com/ru/post/1792112/


All Articles