---
title: "DB2 Fetch ideas on how to optimize result"
description: "DB2 fetch top rules you need to know while using it in your SQL queries."
url: https://srinimf.com/2012/05/02/db2-fetch-rules/
date: 2012-05-02
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2012/05/optimized-db2-fetch.jpg?fit=1200%2C600&ssl=1
categories: ["DB2"]
tags: ["DB2 Fetch first n rows"]
type: post
lang: en
---

# DB2 Fetch ideas on how to optimize result

After I returned from my work, I got an [idea to write](https://srinimf.com/2012/02/08/sql-errors-300-301-302-303-304/) a post on Fetch. I am writing it by listening to a popular [mind music](http://www.amazon.com/Music-Quiet-Mind-Eckhart-Tolle/dp/B001EOQUSM/ref=sr_1_5?s=music&ie=UTF8&qid=1335970200&sr=1-5).

**[FETCH FIRST](https://srinimf.com/2018/02/25/db2-performance-tips/) n  ROW/ROWS ONLY**

Default is ‘1’. It improves performance, if we use this statement in [SQL query](https://srinimf.com/2022/08/13/2-amazing-ways-to-limit-rows-in-oracle-sql-query/).

Related[DB2 SQL: How to check Host-variables (1 of 2)](https://srinimf.com/2011/12/15/db2-where-in/)

## **Rules [to use](https://srinimf.com/2017/12/13/db2-set-operators-to-never-miss-these/) Fetch First**

- FETCH FIRST we should not use with FOR UPDATE
- If FETCH FIRST and ORDER BY clauses are used, first Order By will be applied on the entire table, then it gives the Fetch First *n* rows.
- FETCH FIRST m ROWS ONLY and OPTIMIZE FOR n ROWS , if both mentioned,  m>n, then sql optimizes for n rows.

Related[Cursors Static Vs Scrollable – And its Top Uses in DB2](https://srinimf.com/2010/08/19/db2-scrollabe-cursor/)

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2012%2F05%2F02%2Fdb2-fetch-rules%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2012%2F05%2Foptimized-db2-fetch.jpg%3Ffit%3D1200%252C600%26ssl%3D1&description=DB2%20Fetch%20ideas%20on%20how%20to%20optimize%20result)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2012/05/02/db2-fetch-rules/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2012/05/02/db2-fetch-rules/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2012/05/02/db2-fetch-rules/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2012/05/02/db2-fetch-rules/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20DB2%20Fetch%20ideas%20on%20how%20to%20optimize%20result&body=https%3A%2F%2Fsrinimf.com%2F2012%2F05%2F02%2Fdb2-fetch-rules%2F&share=email)
-

### Like this:

Like Loading…
