Question

Make this query run faster

Asked by: leonstryker

We are running this query on a DB2 database and are wondering if there is anything which can be done to make it run faster?

with temp(id,desk,CUR_MTD_TOTAL, WTD, CUR_FY_YTD, PRV_MTD_TOTAL, PRV_BUS_DAY,PRV_DAY_AVG, PRV_WEEK_AVG,PRV_MONTH_AVG, PRV_FY_YTD,CUR_REV_ANN,PRV_FY,FORECAST,TOTAL_YTD_ACCRUED, NEXT_PLAN_PRIM,NEXT_PLAN_SECDY,NEXT_PLAN) as (
    select rootoe.row_id,rootoe.name as desk, sum(prev.CUR_MTD_TOTAL), sum(prev.WTD), sum(prev.CUR_FY_REV), sum(prev.PRV_MTD_TOTAL) , sum(prev.PRV_BUS_DAY) , sum(prev.DAILY_AVG) ,sum(prev.WEEKLY_AVG) ,sum(prev.PRV_MONTH_AVG) ,sum(prev.PRV_YTD_REV) ,sum(prev.PRV_FY_REV),sum(prev.CUR_FY_ANN_REV),0,0,0,0,0
    from cx_matrix_pos tr
    inner join cx_revenue prev on prev.postn_id=tr.child_pos
    inner join s_org_ext oe on oe.row_id=prev.oe_id
    inner join s_postn postn on postn.row_id=tr.child_pos
    inner join s_org_ext rootoe on rootoe.row_id=oe.frght_terms_cd
    where tr.parent_pos='1-156J4' and oe.FRGHT_TERMS_INFO like 'Investment Advisor' and Ucase(rootoe.name) like '%'
    group by rootoe.row_id,rootoe.name
    union all
    select rootoe.row_id,rootoe.name as desk,  0,0,0,0,0,0,0,0,0,0,0,sum(bu.forecast),sum(bu.total_ytd_accrued),0,0,0
    from cx_matrix_pos tr
    inner join cx_forecast_fy bu on bu.position_id=tr.child_pos
    inner join cx_fiscal_year fy on fy.fiscal_year=bu.fiscal_year
    inner join s_postn postn on postn.row_id=tr.child_pos
    inner join s_org_ext oe on oe.row_id=bu.ou_id
    inner join s_org_ext rootoe on rootoe.row_id=oe.frght_terms_cd
    where tr.parent_pos='1-156J4' and oe.FRGHT_TERMS_INFO like 'Investment Advisor' and Ucase(rootoe.name) like '%'
    group by rootoe.row_id,rootoe.name
   union all
     select rootoe.row_id,rootoe.name as desk,  0,0,0,0,0,0,0,0,0,0,0,0,0,sum(bu.ni_forecast),sum(bu.sec_forecast),sum(bu.forecast)
    from cx_matrix_pos tr
    inner join cx_forecast bu on bu.position_id=tr.child_pos
    inner join s_period per on per.row_id=bu.period_id
    inner join s_postn postn on postn.row_id=tr.child_pos
    inner join s_org_ext oe on oe.row_id=bu.ou_id
    inner join s_org_ext rootoe on rootoe.row_id=oe.frght_terms_cd
    where tr.parent_pos='1-156J4' and oe.FRGHT_TERMS_INFO like 'Investment Advisor' and Ucase(rootoe.name) like '%'
    and per.desc_text='Fiscal Year 2005'
    group by rootoe.row_id,rootoe.name
)
 select id,desk,  cast(sum(CUR_MTD_TOTAL) as double) as Cur_Month, cast(sum(FORECAST) as double) as Cur_Plan_FY, cast(sum(TOTAL_YTD_ACCRUED) as double) as Cur_Plan_YTD, cast(sum(WTD) as double) as Cur_Week,cast(sum(CUR_FY_YTD) as double) as Cur_YTD, cast(sum(PRV_MTD_TOTAL) as double) as Prv_Month, cast(sum(PRV_BUS_DAY) as double) as Prv_Day,cast(sum(PRV_DAY_AVG) as double) as Prv_Day_Avg, cast(sum(PRV_WEEK_AVG) as double) as Prv_Week_Avg, cast(sum(PRV_MONTH_AVG) as double) as Prv_Month_Avg,cast(sum(PRV_FY_YTD) as double) as Prv_YTD ,cast(sum(PRV_FY) as double) as Prv_FY, cast(sum(CUR_REV_ANN) as double) as Cur_Rev_Ann,cast(sum(NEXT_PLAN_PRIM) as double) as Next_Plan_Prim, cast(sum(NEXT_PLAN_SECDY) as double) as Next_Plan_Secdy,cast(sum(NEXT_PLAN) as double) as Next_Plan  from temp group by id,desk order by Cur_Ytd desc

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-09-24 at 07:02:01ID21143805
Tags

run

,

faster

,

make

,

query

Topic

Databases Miscellaneous

Participating Experts
4
Points
500
Comments
19

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. MySQL to DB2
    Hello, Does someone know how to convert the following sql query to a db2 query. SELECT LAST_INSERT_ID() FROM Kund2 LIMIT 1 Regards Pierre Andersson
  2. Faster ...
    Hi, I'm trying to iterate through a large table and change the value of a field. Using Edit & Post takes a long time. Is there any significantly faster way to do it? (I thought of something like writing directly to the recordbuffer.) A fast way to populate a ClientDat...
  3. DB2 and oracle
    Hi, Between DB2 and Oracle in a web site ( such as ebay!) , what do you recommend?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: jltoopsPosted on 2004-09-24 at 07:32:06ID: 12143585

Ideas ..
a) looks like this would make a nice View ...
b) Remove/Replace Ucase(rootoe.name) like '%'
with somthing like rootoe.name <> ''
c) Create a SP to do this... and Run it every so often and cache the results in a table


Questions
a) how long does it take
b) how many rows does it return
c) what are the tables involved and
d) example of what it returns.


Jay

ps... THATS one fat harry monster of a query u have there

 

by: leonstrykerPosted on 2004-09-24 at 07:55:25ID: 12143832

This query runs on db2 ver6 stored procedures are not supported.

It currently run in 11 sec.

CX_REVENUE 500000 rows
CX_FORECAST 140000 rows
Cx_forecast_fy 15000 rows

Returns 634 rows

Purpose for Ucase() like '%' -- During runtime this gets replaced with users query string

 

by: leonstrykerPosted on 2004-09-24 at 08:01:01ID: 12143898

How will a view improve this?

 

by: jltoopsPosted on 2004-09-24 at 08:03:39ID: 12143930

you could create the veiw with all the code EXCEPT the like statement which changes.

Views are basically compiled queries, the data is already arranged and organized like it is
needed then you query the view just like any other table. using your Like statement
which is basically the only "DYNAMIC" piece of this query .

According to the manual DB2v6 supports Stored procedures.

Jay

 

by: leonstrykerPosted on 2004-09-24 at 08:16:07ID: 12144058

Stored procedures for db2 6 are written in C++

 

by: jltoopsPosted on 2004-09-24 at 08:18:19ID: 12144089

bummer...
Jay

 

by: leonstrykerPosted on 2004-09-24 at 08:38:38ID: 12144356

Yeah ....

 

by: jltoopsPosted on 2004-09-24 at 08:45:27ID: 12144466

really a view should help

Jay

 

by: leonstrykerPosted on 2004-09-24 at 08:52:00ID: 12144560

We will try it.

 

by: SjoerdVerweijPosted on 2004-09-24 at 10:47:01ID: 12145775

Like Leon said: the like '%' stuff is killing you. It is very likely forcing a table scan. Replace with <> ''.

 

by: leonstrykerPosted on 2004-09-24 at 11:13:20ID: 12146018

SjoerdVerweij,

In this case the like '%' is a ariable to which a value is bound at run time.  It is not a condition for items which are not empty.

 

by: SjoerdVerweijPosted on 2004-09-24 at 11:53:33ID: 12146450

Man oh man I'm fried today. Okay, what kind of terms do you put in there?

 

by: jltoopsPosted on 2004-09-24 at 11:54:58ID: 12146458

LOL have a nice weekend guys..

JAY

 

by: leonstrykerPosted on 2004-09-24 at 12:08:05ID: 12146609

Its a name of a person

 

by: derekpapeschPosted on 2004-09-24 at 22:48:00ID: 12149368

That query is ugly.  

There is nothing wrong with it if you only plan to run it once or twice, infrequently or on very small tables, but if that isn't the case you should look at each join and remove some.

Take a look at the tables you are using, and find which tables are the biggest to work out which joins are going to be the most expensive.  Then check to make sure you have indexes on those columns.

Find out where the query is burning all the time, but using an analysis package (I don't know DB2) or just break it down into small queries to find out which part goes slow.

Also, when you join data, joining 100 rows will usually be much faster than 10,000, so if you filter the data first (perhaps using a subquery) then join, it will be much faster than joining the data, then filtering. (and don't forget indexes).

Lastly, if the speed of this query is important, you might want to de-normalize your data.  DB2 should have a feature like Oracle's snapshot, which will create a view, then keep updating it a set time intervals to keep it current.  You treat the snapshot like a normal table and get very fast access to the data.

Hope this gets you started.

P.S. Unless DB2 has special features built into its view, I can't see how a view will speed up your query.  The execution path may be precompiled, but that cost will be cheap alongside joins on 500,000 rows.

P.P.S. Indexes should be your first port of call.

 

by: granbajoPosted on 2004-09-28 at 05:32:03ID: 12168751


  DB2 has something called "summary tables", I am pretty sure it is available in version 6, although you would have to check.  Check the sql reference for the CREATE TABLE STATEMENT.  They are used to create "persistent views", and they can be calculated every time you add a row to the base tables, then querying them would be much faster.

 

by: leonstrykerPosted on 2004-09-29 at 12:24:48ID: 12183446

"summary tables" are not available in version 6

:(

 

by: derekpapeschPosted on 2004-09-29 at 17:20:16ID: 12185836

Looks like there are a few things missing in v6!

Depending on how frequently your data is updated, how fast you need the query to run, and whether you need the latest data, you could  create a table then schedule a script (to be called from outside the database if v.6 doesn't have any way of doing it!), then keep updating the table every day (/hour/month).  Thus, you would have a lightning fast query, but would only get the data since the latest update.

Of course, this approach will only work if you are happy with serving data which may be several hours (/minutes/days) old.

 

by: leonstrykerPosted on 2004-09-29 at 20:10:46ID: 12186680

derekpapesch,

I was actually considering this approach and am currently attempting to implement it.  I will let you know as soon as possible.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...