Solved

optimizing the query in db2 v9.1 luw

Posted on 2011-03-17
5
457 Views
Last Modified: 2012-05-11
I have a query which will take almost 3 min to run adn provide the result.

I need to optimize this query and kindly suggest any tools with link which will be helpful

This is critically needed..
select 
	LTRIM(RTRIM("REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_NAME")) "C0" , 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_ENG_DATE" "C1" , 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_WIN_STATUS" "C2" , 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SCOPE" "C3" , 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SECTOR" "C4" , 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_INDUSTRY" "C5" , 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_COMPETITORS" "C6" , 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SKEY" "C7" , 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_REVENUE" "C8" , 
	sum("FACT_SPEND_ANALYSIS"."INVOICE_AMOUNT_LOCAL_CURRENCY" * "EXCHANGE_RATE"."EXCHANGE_RATE") over (partition by LTRIM(RTRIM("REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_NAME")), 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SCOPE", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SECTOR", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_INDUSTRY", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_COMPETITORS", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_WIN_STATUS", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_ENG_DATE") "C9" , 
	sum(case  when "ADDRESSABLE_SPEND"."ADDRESSABLE_DESCRIPTION" = 'Addressable' then "FACT_SPEND_ANALYSIS"."INVOICE_AMOUNT_LOCAL_CURRENCY" * "EXCHANGE_RATE"."EXCHANGE_RATE" else 0 end ) over (partition by LTRIM(RTRIM("REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_NAME")), 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SCOPE", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SECTOR", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_INDUSTRY", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_COMPETITORS", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_WIN_STATUS", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_ENG_DATE") "C10" , 
	sum(case  when "ADDRESSABLE_SPEND"."ADDRESSABLE_DESCRIPTION" = 'Unaddressable' then "FACT_SPEND_ANALYSIS"."INVOICE_AMOUNT_LOCAL_CURRENCY" * "EXCHANGE_RATE"."EXCHANGE_RATE" else 0 end ) over (partition by LTRIM(RTRIM("REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_NAME")), 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SCOPE", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SECTOR", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_INDUSTRY", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_COMPETITORS", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_WIN_STATUS", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_ENG_DATE") "C11" , 
	sum(case  when "ADDRESSABLE_SPEND"."ADDRESSABLE_DESCRIPTION"  NOT  in ('Addressable', 'Unaddressable') then "FACT_SPEND_ANALYSIS"."INVOICE_AMOUNT_LOCAL_CURRENCY" * "EXCHANGE_RATE"."EXCHANGE_RATE" else 0 end ) over (partition by LTRIM(RTRIM("REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_NAME")), 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SCOPE", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SECTOR", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_INDUSTRY", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_COMPETITORS", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_WIN_STATUS", 
	"REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_ENG_DATE") "C12" 

from
	(((("COGNOS"."REF_PARENT_COMPANY_SPEND" "REF_PARENT_COMPANY_SPEND" 
	inner join "COGNOS"."DIM_COMPANY_CODE_SPEND" "COMPANY_CODE_SPEND" on "REF_PARENT_COMPANY_SPEND"."PARENT_COMPANY_SKEY" = "COMPANY_CODE_SPEND"."PARENT_COMPANY_SKEY") 
	inner join "COGNOS"."FACT_SPEND_ANALYSIS" "FACT_SPEND_ANALYSIS" on "COMPANY_CODE_SPEND"."COMPANY_CODE_SKEY" = "FACT_SPEND_ANALYSIS"."COMPANY_CODE_SKEY") 
	inner join "COGNOS"."DIM_ADDRESSABLE_SPEND" "ADDRESSABLE_SPEND" on "ADDRESSABLE_SPEND"."ADDRESSABLE_SKEY" = "FACT_SPEND_ANALYSIS"."ADDRESSABLE_SKEY") 
	inner join "COGNOS"."DIM_DATE_SPEND" "INVOICE_DATE_SPEND" on "INVOICE_DATE_SPEND"."DATE_SKEY" = "FACT_SPEND_ANALYSIS"."INVOICE_DATE_SKEY") 
	inner join (
		select "EXCHANGE_RATE"."CURRENCY_SKEY" "CURRENCY_SKEY" , "EXCHANGE_RATE"."EXCHANGE_RATE" "EXCHANGE_RATE" 
		from "COGNOS"."TEMP_EXCHANGERATE" "EXCHANGE_RATE"
		where "EXCHANGE_RATE"."TO_CURRENCY_CODE" = 'USD') "EXCHANGE_RATE" on "EXCHANGE_RATE"."CURRENCY_SKEY" = "FACT_SPEND_ANALYSIS"."LOCAL_CURRENCY_SKEY"

where 
	"INVOICE_DATE_SPEND"."FISCAL_YEAR_NO" = 2010 or 2010 - "INVOICE_DATE_SPEND"."CALENDAR_YEAR_NO" = 2010 FOR FETCH ONLY

Open in new window

0
Comment
Question by:jmartin_v
  • 2
  • 2
5 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 35157646
can you post the access plan?

basically, make sure your statistics are updated and that you have indexes to support all the join operations

for deeper analysis please post the number of rows in each table and available indexes
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 35158501
try this

at the least replace the or 2010 - .... calculation with
 or calendar_date=0   the calulation maybe causing problems with index usage...


select 
	x."C0" , 
	x."PARENT_COMPANY_ENG_DATE" "C1" , 
	x."PARENT_COMPANY_WIN_STATUS" "C2" , 
	x."PARENT_COMPANY_SCOPE" "C3" , 
	x."PARENT_COMPANY_SECTOR" "C4" , 
	x."PARENT_COMPANY_INDUSTRY" "C5" , 
	x."PARENT_COMPANY_COMPETITORS" "C6" , 
	PSm."PARENT_COMPANY_SKEY" "C7" , 
	PSm."PARENT_COMPANY_REVENUE" "C8" , 
     x.c9,x.c10,x.c11,x.c12
From "COGNOS"."REF_PARENT_COMPANY_SPEND" as PSM
inner join
(
Select 
LTRIM(RTRIM(PS."PARENT_COMPANY_NAME")) as co, 
	PS."PARENT_COMPANY_SCOPE", 
	PS."PARENT_COMPANY_SECTOR", 
	PS."PARENT_COMPANY_INDUSTRY", 
	PS."PARENT_COMPANY_COMPETITORS", 
	PS."PARENT_COMPANY_WIN_STATUS", 
	PS."PARENT_COMPANY_ENG_DATE",
	sum("FACT_SPEND_ANALYSIS"."INVOICE_AMOUNT_LOCAL_CURRENCY" * "EXCHANGE_RATE"."EXCHANGE_RATE")  "C9" , 
	sum(case  when "ADDRESSABLE_SPEND"."ADDRESSABLE_DESCRIPTION" = 'Addressable' then "FACT_SPEND_ANALYSIS"."INVOICE_AMOUNT_LOCAL_CURRENCY" * "EXCHANGE_RATE"."EXCHANGE_RATE" else 0 end )  "C10" , 
	sum(case  when "ADDRESSABLE_SPEND"."ADDRESSABLE_DESCRIPTION" = 'Unaddressable' then "FACT_SPEND_ANALYSIS"."INVOICE_AMOUNT_LOCAL_CURRENCY" * "EXCHANGE_RATE"."EXCHANGE_RATE" else 0 end )  "C11" , 
	sum(case  when "ADDRESSABLE_SPEND"."ADDRESSABLE_DESCRIPTION"  NOT  in ('Addressable', 'Unaddressable') then "FACT_SPEND_ANALYSIS"."INVOICE_AMOUNT_LOCAL_CURRENCY" * "EXCHANGE_RATE"."EXCHANGE_RATE" else 0 end ) "C12" 

from
	(((("COGNOS"."REF_PARENT_COMPANY_SPEND" as PS 
	inner join "COGNOS"."DIM_COMPANY_CODE_SPEND" "COMPANY_CODE_SPEND" on PS."PARENT_COMPANY_SKEY" = "COMPANY_CODE_SPEND"."PARENT_COMPANY_SKEY") 
	inner join "COGNOS"."FACT_SPEND_ANALYSIS" "FACT_SPEND_ANALYSIS" on "COMPANY_CODE_SPEND"."COMPANY_CODE_SKEY" = "FACT_SPEND_ANALYSIS"."COMPANY_CODE_SKEY") 
	inner join "COGNOS"."DIM_ADDRESSABLE_SPEND" "ADDRESSABLE_SPEND" on "ADDRESSABLE_SPEND"."ADDRESSABLE_SKEY" = "FACT_SPEND_ANALYSIS"."ADDRESSABLE_SKEY") 
	inner join "COGNOS"."DIM_DATE_SPEND" "INVOICE_DATE_SPEND" on "INVOICE_DATE_SPEND"."DATE_SKEY" = "FACT_SPEND_ANALYSIS"."INVOICE_DATE_SKEY") 
	inner join (
		select "EXCHANGE_RATE"."CURRENCY_SKEY" "CURRENCY_SKEY" , "EXCHANGE_RATE"."EXCHANGE_RATE" "EXCHANGE_RATE" 
		from "COGNOS"."TEMP_EXCHANGERATE" "EXCHANGE_RATE"
		where "EXCHANGE_RATE"."TO_CURRENCY_CODE" = 'USD') "EXCHANGE_RATE" on "EXCHANGE_RATE"."CURRENCY_SKEY" = "FACT_SPEND_ANALYSIS"."LOCAL_CURRENCY_SKEY"

where 
	"INVOICE_DATE_SPEND"."FISCAL_YEAR_NO" = 2010
or  "INVOICE_DATE_SPEND"."CALENDAR_YEAR_NO" = 0
group by 
LTRIM(RTRIM(PS."PARENT_COMPANY_NAME"))  , 
	PS."PARENT_COMPANY_ENG_DATE" , 
	PS."PARENT_COMPANY_WIN_STATUS"  , 
	PS."PARENT_COMPANY_SCOPE"  , 
	PS."PARENT_COMPANY_SECTOR"  , 
	PS."PARENT_COMPANY_INDUSTRY"  , 
	PS."PARENT_COMPANY_COMPETITORS"  , 
	PS."PARENT_COMPANY_SKEY"  , 
	PS."PARENT_COMPANY_REVENUE"  
) as X
on x."C0" =LTRIM(RTRIM(PSm."PARENT_COMPANY_NAME"))  
and	x."PARENT_COMPANY_ENG_DATE" =psm."PARENT_COMPANY_ENG_DATE" 
and	x."PARENT_COMPANY_WIN_STATUS" =psm."PARENT_COMPANY_WIN_STATUS" 
and	x."PARENT_COMPANY_SCOPE" =psm."PARENT_COMPANY_SCOPE"
and	x."PARENT_COMPANY_SECTOR" =psm."PARENT_COMPANY_SECTOR"
and	x."PARENT_COMPANY_INDUSTRY" =psm."PARENT_COMPANY_INDUSTRY"
and	x."PARENT_COMPANY_COMPETITORS" =psm."PARENT_COMPANY_COMPETITORS"
 FOR FETCH ONLY

Open in new window

0
 

Author Comment

by:jmartin_v
ID: 35167821
HI

The index are also there, in the one of the prime table the count of table is 194563...

@lowfatspread...

The above query did not work

to add a point the query is used by cognos team to generate reports and the query is creted by using the toll and is not hand written..tried using the quest sql optimizer which mentioned that the sql does not support..
please help on this
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 35171307
what error did the query give when it failed?

could you try creating the subquery as a view ?

0
 

Author Closing Comment

by:jmartin_v
ID: 35768367
helped partially
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question