Unix Technical Forum

APPLICATION_ID() causes optimizer fits

This is a discussion on APPLICATION_ID() causes optimizer fits within the DB2 forums, part of the Database Server Software category; --> The query: DELETE FROM CONNECTION_USERS WHERE CONNECTION_USERS.APPLICATION_ID = APPLICATION_ID() causes a table scan to always happen in the unique ...


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 11:24 AM
Ian Boyd
 
Posts: n/a
Default APPLICATION_ID() causes optimizer fits

The query:
DELETE FROM CONNECTION_USERS
WHERE CONNECTION_USERS.APPLICATION_ID = APPLICATION_ID()

causes a table scan to always happen in the unique indexed primary keyed
field "Application_ID"

But changing the query to:
DELETE FROM CONNECTION_USERS
WHERE CONNECTION_USERS.APPLICATION_ID = (
SELECT APPLICATION_ID() FROM sysibm.sysdummy1)

causes the proper index scan.


Query A takes 2.7 seconds.
Query B takes 10ms.

i'm sure there's a good reason why the optimizer is thrown into fits; but i
don't really care. Just be aware and fix it.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 11:24 AM
Udo
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

Is the function "APPLICATION_ID()" declared as "deterministic" ?
If not, DB2 needs to check *every* line of the "CONNECTION_USERS" Table
- and won't use the index.

Regards,
Udo

--
Speedgain for DB2 - The DB2 Monitor
http://www.itgain.de/en/index.html

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 11:24 AM
Knut Stolze
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

Udo wrote:

> Is the function "APPLICATION_ID()" declared as "deterministic" ?
> If not, DB2 needs to check *every* line of the "CONNECTION_USERS" Table
> - and won't use the index.


No, it is not.

$ db2 "select deterministic from syscat.routines where routinename
= 'APPLICATION_ID'"

DETERMINISTIC
-------------
N


The reason is that the function is not deterministic - it does not return
the same result on each call. A different SQL session gets a different
application id.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 11:24 AM
Serge Rielau
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

Knut Stolze wrote:
> The reason is that the function is not deterministic - it does not return
> the same result on each call. A different SQL session gets a different
> application id.

It boils down to the fact that application_id() is deterministic within
a query, and even a session, but not constant as required to be used in
e.g. a check constraint.
Unfortunately there is only one level of granularity: DETERMINISTIC.
Adding finer control would allow for better optimizations, but also a
more difficult to use product.
The long term solution will be to provide a new class of "variables"
akin of special registers and make application id such a variable.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 11:24 AM
Ian Boyd
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

> The long term solution will be to provide a new class of "variables" akin
> of special registers and make application id such a variable.


That will be a welcome improvement.

(and you can't believe how many iterations of that line i went through so it
didn't sound insulting.

"At least you're open to improvment."
"i'm grateful future developers will have that feature."
"It's a nice idea, but too late for me."

i was really trying to give a positive response; i had to ask a colleague
for help.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 11:25 AM
Serge Rielau
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

Ian Boyd wrote:
> i was really trying to give a positive response; i had to ask a colleague
> for help.

I know how hard that is for you. :-)

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-27-2008, 11:25 AM
Amanda
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

> I know how hard that is for you. :-)

Oh no; i've acquired a reputation!



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-27-2008, 11:25 AM
Ian Boyd
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

Wow, that's scary. i have no idea how my name got changed to "Amanda"


"Amanda" <newsjet2net@zunblvlda1.dyndns.org> wrote in message
news:enms3n0aj2@enews3.newsguy.com...
>> I know how hard that is for you. :-)

>
> Oh no; i've acquired a reputation!
>
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-27-2008, 11:28 AM
Ian Boyd
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

> It boils down to the fact that application_id() is deterministic within a
> query, and even a session, but not constant as required to be used in e.g.
> a check constraint.
> Unfortunately there is only one level of granularity: DETERMINISTIC.
> Adding finer control would allow for better optimizations, but also a more
> difficult to use product.
> The long term solution will be to provide a new class of "variables" akin
> of special registers and make application id such a variable.



Might want to tell someone to update this technical article
http://www-128.ibm.com/developerwork...302stolze.html

Near the bottom in the audit log section, there is a trigger named auditT1.
Take a look at the SELECT query into the logins table filtered on
application_id()

> SELECT user_id FROM login L WHERE L.appl_id=application_id()



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-27-2008, 11:28 AM
Knut Stolze
 
Posts: n/a
Default Re: APPLICATION_ID() causes optimizer fits

Ian Boyd wrote:

>> It boils down to the fact that application_id() is deterministic within a
>> query, and even a session, but not constant as required to be used in
>> e.g. a check constraint.
>> Unfortunately there is only one level of granularity: DETERMINISTIC.
>> Adding finer control would allow for better optimizations, but also a
>> more difficult to use product.
>> The long term solution will be to provide a new class of "variables" akin
>> of special registers and make application id such a variable.

>
>
> Might want to tell someone to update this technical article
>

http://www-128.ibm.com/developerwork...302stolze.html
>
> Near the bottom in the audit log section, there is a trigger named
> auditT1. Take a look at the SELECT query into the logins table filtered on
> application_id()
>
>> SELECT user_id FROM login L WHERE L.appl_id=application_id()


Actually, the function in the article is defined as DETERMINISTIC. The
article existed before DB2 incorporated its own APPLICATION_ID() function.
Therefore, the trigger works fine and would use an index if present (and
deemed as a good thing by the optimizer).

What we should update, however, is the description on the determinism of
that function...

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:34 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com