optimizing the query in db2 v9.1 luw

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

jmartin_vAsked:
Who is Participating?
 
momi_sabagCommented:
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
 
LowfatspreadCommented:
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
 
jmartin_vAuthor Commented:
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
 
LowfatspreadCommented:
what error did the query give when it failed?

could you try creating the subquery as a view ?

0
 
jmartin_vAuthor Commented:
helped partially
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.