Why does MYSQL higher LIMIT offset slow the query down?

后端 未结 6 1154
忘掉有多难
忘掉有多难 2020-11-22 07:20

Scenario in short: A table with more than 16 million records [2GB in size]. The higher LIMIT offset with SELECT, the slower the query becomes, when using ORDER BY *prima

6条回答
  •  遇见更好的自我
    2020-11-22 08:00

    I had the exact same problem myself. Given the fact that you want to collect a large amount of this data and not a specific set of 30 you'll be probably running a loop and incrementing the offset by 30.

    So what you can do instead is:

    1. Hold the last id of a set of data(30) (e.g. lastId = 530)
    2. Add the condition WHERE id > lastId limit 0,30

    So you can always have a ZERO offset. You will be amazed by the performance improvement.

提交回复
热议问题