Why does MYSQL higher LIMIT offset slow the query down?

后端 未结 6 1157
忘掉有多难
忘掉有多难 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 07:57

    For those who are interested in a comparison and figures :)

    Experiment 1: The dataset contains about 100 million rows. Each row contains several BIGINT, TINYINT, as well as two TEXT fields (deliberately) containing about 1k chars.

    • Blue := SELECT * FROM post ORDER BY id LIMIT {offset}, 5
    • Orange := @Quassnoi's method. SELECT t.* FROM (SELECT id FROM post ORDER BY id LIMIT {offset}, 5) AS q JOIN post t ON t.id = q.id
    • Of course, the third method, ... WHERE id>xxx LIMIT 0,5, does not appear here since it should be constant time.

    Experiment 2: Similar thing, except that one row only has 3 BIGINTs.

    • green := the blue before
    • red := the orange before

提交回复
热议问题