If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Application of predicates or filters before materialising view definition

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-09, 17:41
vprabh vprabh is offline
Registered User
 
Join Date: Sep 2009
Posts: 1
Application of predicates or filters before materialising view definition

Team,

We are using rank over() DB2 funtion in defintion of one the view of my application. We need to apply certain filters like time period etc before rank over() function is evaluated.

However, as rank over() is within view, view definition materialises first and then apply time filter on top of it, resulting in incorrect results.

Would you have any alternative or suggestion to implement my requirement.

View Defintion is :

create view vdw02.v_clist as
(select
a.ord_no
, a.base_id || a.basic_no as c
, rank()over (partition by a.base_id, a.basic_no order by a.ORD_NO,a.BASE_ID,a.BASIC_NO,a.MKT_CHAN_CD asc) as rnk
, rank()over (partition by a.base_id, a.basic_no order by (rank()over (partition by a.base_id, a.basic_no order by a.ORD_NO,a.BASE_ID,a.BASIC_NO,a.MKT_CHAN_CD asc)) desc) as rnk2
from dw02.clist a)
Reply With Quote
  #2 (permalink)  
Old 09-01-09, 23:56
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I'm not sure what you're asking, but views don't "materialize" in DB2; you may want to check the execution plan to see what the query looks like after the rewrite.
Reply With Quote
  #3 (permalink)  
Old 09-02-09, 03:57
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Right, a view doesn't "materialize". The thing is that the view dynamically computes a table. If you query that view and apply additional predicates, the predicates are applied on the result of the view and not on the input for the view, i.e. the tables being joined. That's just a basic design principle for views.

So you have some choices:
  1. define a side-table that is joined-in by the view to filter out rows in the view's WHERE clause; you will have to maintain this table before querying and it will bring along locking issues
  2. remove the RANK() function from the view definition and do it in the SQL statement that queries the view instead.
  3. encapsulate the whole logic into a stored procedure and build the query dynamically and return the query's result as result set of the stored procedure
Maybe there are more options, but those are the ones that come first to my mind.

Btw, here you find the documentation that explains how a subselect is processed: http://publib.boulder.ibm.com/infoce.../r0000875.html Now you just piece this together with the fact thatt the view occurs in the FROM clause and it becomes that you can't inject additional predicates into view's WHERE clause when querying the view - only on top of the view's result table.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development

Last edited by stolze; 09-02-09 at 04:01.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On