Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

optimizing the query in db2 v9.1 luw

Posted on 2011-03-17
5
Medium Priority
?
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 1000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

609 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