Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query range of data in DB2 with highest performance?

Usually, I need to retrieve data from a table in some range; for example, a separate page for each search result. In MySQL I use LIMIT keyword but in DB2 I don't know. Now I use this query for retrieve range of data.

SELECT * 
FROM(
   SELECT  
      SMALLINT(RANK() OVER(ORDER BY NAME DESC)) AS RUNNING_NO
      , DATA_KEY_VALUE
      , SHOW_PRIORITY
   FROM 
      EMPLOYEE
   WHERE 
      NAME LIKE 'DEL%'
   ORDER BY
      NAME DESC
   FETCH FIRST 20 ROWS ONLY
) AS TMP
ORDER BY 
  TMP.RUNNING_NO ASC
FETCH FIRST 10 ROWS ONLY

but I know it's bad style. So, how to query for highest performance?

like image 279
Fuangwith S. Avatar asked Nov 05 '08 16:11

Fuangwith S.


People also ask

How do I limit the number of rows in DB2 query?

You can specify the fetch clause in a SELECT statement to limit the number of rows in the result table of a query.


1 Answers

My requirement have been added into DB2 9.7.2 already.

DB2 9.7.2 adds new syntax for limit query result as illustrate below:

SELECT * FROM TABLE LIMIT 5 OFFSET 20

the database will retrieve result from row no. 21 - 25

like image 50
Fuangwith S. Avatar answered Sep 28 '22 08:09

Fuangwith S.