---
title: "Discussion on \"Why You Should Avoid Offset in Your DB Queries\" | Hashnode"
description: "Discussion on \"Why You Should Avoid Offset in Your DB Queries\". Learn about the impact of offset or skip in your database queries and how you can make easy performance gains by avoiding it."
url: "https://hashnode.com/posts/why-you-should-avoid-offset-in-your-db-queries/62700449339f01018101fad0"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Why You Should Avoid Offset in Your DB Queries](https://rrawat.hashnode.dev/why-you-should-avoid-offset-in-your-db-queries?utm_source=hashnode&utm_medium=feed)

Learn about the impact of offset or skip in your database queries and how you can make easy performance gains by avoiding it.

May 2, 2022 · 2 min read · rrawat.hashnode.dev  
Read the article

-   [#2articles1week](https://hashnode.com/tag/2articles1week)
-   [#hashnode](https://hashnode.com/tag/hashnode)
-   [#databases](https://hashnode.com/tag/databases)
-   [#webperf](https://hashnode.com/tag/webperf)

## Written by

[Rishabh Rawat](https://hashnode.com/@rrawat)

@rrawat

  
[View profile](https://hashnode.com/@rrawat)

Software Engineer at smallcase

Software Engineer at smallcase 🧑🏻‍💻 | GSoC at PublicLab 🌴 | Writing atomics on web dev ✍🏼

## Responses 1

Markdown supported · ⌘↵ to post

[Jeff Smith](https://hashnode.com/@thatjeffsmith)

[May 4, 2022](https://hashnode.com/posts/why-you-should-avoid-offset-in-your-db-queries/62700449339f01018101fad0/comment/62728216272fe72a917ccdd4)

When paginating the results of a query in Oracle REST APIs...we don't actually do that.

We take the query...and rewrite it to have the 'page' returned as a singular resultset.

So each time the user/app follows the link for the next or previous page, the query is executed again.

So instead of

```
SELECT * FROM my_table
```

We run

```
SELECT *
    FROM (
         SELECT Q_.*,
                ROW_NUMBER() OVER(
                        ORDER BY 1
                ) RN___
            FROM(
                SELECT *
                    FROM my_table
        ) Q_
)
 WHERE RN___ BETWEEN :1 AND :2
```

The API dev can of course override this and build their own logic, or even disable paging...
