Why does offset make your query slow?

Why does offset make your query slow?

When working with Pagination, whenever i receive an API request with a URL like: /employee?page=10&count=10, my sql query in the backend will handle this: "SELECT * FROM employee limit 10 offset 100". If the offset is small, my query will run very fast, but if the offset goes bigger and bigger my query will run very slowly.

Considering an example, i have a table employees with 2 columns: id and name. This table has 1 million records and has index in the id column.

Let's check the query plan when i have a limit and offset

The cost is very small and if i try to get offset 999900, let's see what happens

As you can see, i provide a limit 10 and an offset 999900, but the query plan shows me that it actually needs to get 999910 rows, and the cost is very high. So what does this mean? This means that even though you have a limit of 10, the database needs to fetch all 999910 rows and only get rows from 999900 to 999910 to show the result.

How to fix

The solution is that we can drop the offset and use ID instead. The ID column is the primary key and also has an index on it, so the cost will reduce and the database will only get 10 rows. Let's check it.

Voila, the cost is smaller than above, and the actual rows the database needs to fetch are only 10.

Use case

This approach is very helpful when your pagination has to type scroll to load more, lazy loading, or just the next or previous page because the front-end knows the biggest ID each time the next page is clicked. But you can't apply it to the pagination where the user can go to any page they want because the front-end can't track the ID.

Manh Vu Dinh

?Database Administrator at Wecommit Vi?t Nam

7 个月

Thanks for sharing

回复
Steve Loc

??Java Developer | Database | Fullstack

8 个月

The solution will be useful when the ID increases sequentially by 1 right? If a row is deleted, it will not be accurate any more

回复
Báu Tr?n

??Project Team Leader @ CNV Loyalty | Database Administrator | Problem Solving | Software Engineer

8 个月

Thanks bro

要查看或添加评论,请登录

Bùi Minh Hoàng的更多文章

  • Rank in MySQL

    Rank in MySQL

    Introduction to MySQL RANK() function The RANK() fucntion assigns a rank to each row within the partition of a result…

  • Fuzzy Search in Django with PostgreSQL

    Fuzzy Search in Django with PostgreSQL

    In real life, sometimes we want to search football but have the wrong typo, like footbal or fotball, but Google or…

    2 条评论
  • Setup Redis Replication

    Setup Redis Replication

    In this article, i will setup Redis replication with 1 master and 3 replicas. First, create the project folder: RedisM…

    6 条评论
  • Composite index and include columns in PostgreSQL

    Composite index and include columns in PostgreSQL

    In PostgresSQL, both composite indexes and indexes with included columns are used to enhance query performance, but…

    7 条评论
  • Yields and List in Python

    Yields and List in Python

    In this article, i will compare two common ways to return a sequence of values from a function is using yield to create…

    1 条评论
  • Django ORM: prefetch_related

    Django ORM: prefetch_related

    In previous article, i mentioned select_related to minimize time hit database in Django, so in this article, i addition…

    1 条评论
  • Django ORM select_related.

    Django ORM select_related.

    In Django, select_related are designed to stop the deluge of database queries that are caused by accessing related…

  • Pessimitic locking and Optimistic locking in Django: Managing concurrent transactions

    Pessimitic locking and Optimistic locking in Django: Managing concurrent transactions

    In previous article, i mentioned using select_for_update() to solve concurrent transactions. select_for_update() can be…

    6 条评论
  • Using select_for_update() to solve concurrent transactions

    Using select_for_update() to solve concurrent transactions

    The Bank Account Transfer Problem A classic example of concurrent transaction issues is the bank account transfer…

    5 条评论
  • Transaction in Django ORM

    Transaction in Django ORM

    Django’s default behavior is to run in autocommit mode. Each query is immediately committed to the database, unless a…

    1 条评论

社区洞察

其他会员也浏览了