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 > returning a table with a function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-22-09, 05:28
saez saez is offline
Registered User
 
Join Date: Oct 2009
Posts: 23
returning a table with a function

Hello,

for reporting (with crystal) I wanted to create a db2-procedure that returns a special constructed table. In fact I did that with a function that returns a table and a stored proc that is called by crystal with some parameters. This works.
The problem is to fill the table with all needed data. The table should have 4 columns.

First part is just a select:
select column1,column2 from tab1

But I want to return column3 and column4, which are in a different table and which depend on col1/col2-values.
Though there will be a select over the given resultset:
select col3,col4 from tab2 where tab2.col5 = <tab1.col1>

col3 and col4 should now be added somehow to the first resultset, and the "new" resultset should be returned as a table.

Any idea?

DB2v9
Reply With Quote
  #2 (permalink)  
Old 10-22-09, 09:46
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Wouldn't a join do what you want?

Andy
Reply With Quote
  #3 (permalink)  
Old 10-22-09, 10:22
saez saez is offline
Registered User
 
Join Date: Oct 2009
Posts: 23
Thanks for answer, so u understand the problem :-)

No, join is not possible because db2 is not using the index, i don't know why. When using a separate select the index is used. In fact tab2 is a view, consisting of at least 100 daily tables. I already tried to improve the explain plan, no deal.
Reply With Quote
  #4 (permalink)  
Old 10-22-09, 13:30
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Rolling the join on your own is bound to be slower than letting DB2 do it. So just use a join and be done with it.

The question why DB2 is not using your index is hard to answer without knowing what DB2 tries to do instead. Maybe it is used another index that better matches the query?

And do you really join 100 tables in a view? That's like begging for problems and even in warehouses you won't see such complex queries often.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development

Last edited by stolze; 10-22-09 at 13:33.
Reply With Quote
  #5 (permalink)  
Old 10-23-09, 01:51
saez saez is offline
Registered User
 
Join Date: Oct 2009
Posts: 23
As we started the project it was not clear that db2 supports partitioning, to avoid one big table with transaction data we decided for a daily table construction. All the tables do have the same structure. But for some reason we need an ALL-View, e.g. reporting.

My procedure should be faster than db2 join. Why?
The resultset of tab1-select is going to be very small (up to 500 records), the tab2-View has at least 50 million records, but I just want to pick up col3,col4 of 500 records. I thought the db2-join would do that for me, but the explain plans shows the difference.
So my plan was to select the 500 records and then finding the needed information in tab2-view by using the index. A select on tab2-view with an index value is fast enough.
Reply With Quote
  #6 (permalink)  
Old 10-24-09, 12:56
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
When you say an "ALL-View" you mean a "UNION ALL view", for which DB2 LUW has quite a few performance optimizations?

In any case, I still stand to my comment that doing the join yourself is typically slower than letting DB2 handle it. Why you see some difference in the access plan, is not clear to us because we don't know the table structure, indexes, and queries. So all we can do is some guessing - and Andy and myself just think that your approach is bound to be problematic.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 10-27-09, 03:24
saez saez is offline
Registered User
 
Join Date: Oct 2009
Posts: 23
Indeed, we are using union all. I suggest there are no other ways to put all the daily tables together?
For now we can't change to using partitions.
Reply With Quote
  #8 (permalink)  
Old 10-27-09, 13:48
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I don't know what you want to know. Doing a join yourself is possible and just requires two queries: one for the outer part of the (nested loop) join and one query for the inner branch.

Using a single query will also work. If such a query has performance problems, I would rather tackle those problems, i.e. identify and solve them. But for this, you would have to provide some more details.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 10-27-09, 15:58
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs down interVIEW

just a table function....
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