|
View previous topic :: View next topic
|
| Author |
Message |
lalitha.rayala5
New User
Joined: 25 Aug 2010 Posts: 10 Location: Bangalore, India
|
|
|
|
Hi,
I have a query as below
declare csr1 cursor for
select ibusrea , ipcod from hbw.berjkv
where ctabbw = 'abcd'
order by ibusrea desc , ipcod desc optimize for 1 row.
how does "optimize for 1 row" work here? As per my code after fetch only first 2 rows are being used. |
|
| Back to top |
|
 |
Anuj Dhawan
Superior Member

Joined: 22 Apr 2006 Posts: 6248 Location: Mumbai, India
|
|
|
|
OPTIMIZE FOR 1 ROW tells DB2 to select an access path that returns the first qualifying row quickly. This means that whenever possible, DB2 avoids any access path that involves a sort.
In "OPTIMIZE FOR n ROWS", if you specify a value for n that is anything but 1, DB2 chooses an access path based on cost, and you won't necessarily avoid sorts. |
|
| Back to top |
|
 |
Anuj Dhawan
Superior Member

Joined: 22 Apr 2006 Posts: 6248 Location: Mumbai, India
|
|
| Back to top |
|
 |
lalitha.rayala5
New User
Joined: 25 Aug 2010 Posts: 10 Location: Bangalore, India
|
|
|
|
"OPTIMIZE FOR 1 ROW tells DB2 to select an access path that returns the first qualifying row quickly".
In the above statement which will be the first qualifying row? In the query above, generally DB2 will get all the qualifying rows first and then it will do the sort for ibusrea and ipcod. After sorting i will display all the sorted qualifying rows list. Is it the first row from this list? or the first row from the qualifying rows before sort? |
|
| Back to top |
|
 |
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
if possible db2 will avoid the sort :
if an index exists on (ibusrea desc , ipcod desc,... ) or on (ctabbw, ibusrea desc , ipcod desc,... ) , it will get the rows via that index , eliminating the need for sort.
If no suitable index is found, OF1R does not have any effect. |
|
| Back to top |
|
 |
lalitha.rayala5
New User
Joined: 25 Aug 2010 Posts: 10 Location: Bangalore, India
|
|
|
|
| ok thanx.. |
|
| Back to top |
|
 |
|
|