This is where I might try to break this into 2 selects. (to the more experienced SQL people, feel free to correct me)

Query 1
===========================================================
SELECT ir.region 
      ,ir.upc
      ,i_b.nat_upc
      ,rpdr.region
      ,rpdr.pos_dept
      ,ps.pos_dept
      ,ps.ps_bu
      ,vsi.upc
      ,vsi.ps_bu

      ,store_name
      ,NVL(i_a.nat_upc,i_b.nat_upc)
      ,NVL(i_a.long_description,i_b.long_description)
      ,NVL(i_a.item_size,i_b.item_size)
      ,NVL(i_a.item_uom,i_b.item_uom)
      ,vsi.vend_item_num
      ,ps.ps_prod_subteam
  FROM vim.item_region@vim       ir
      ,vim.item_master@vim       i_a
      ,vim.item_master@vim       i_b
      ,vim.reg_pos_dept_ref@vim  rpdr
      ,vim.ps_team_ref@vim       ps
      ,vim.vendor_store_item@vim vsi
 WHERE substr(ir.nat_upc, 12, 12)=substr(i_a.nat_upc(+),-12,12)
   AND rpdr.pos_dept     = ps.pos_dept
   AND rpdr.region       = ps.region
   AND vsi.region        = ps.region
   AND vsi.v_auth_status = 'P'
===========================================================
The first 9 columns in the select are new and will be used in the second select.

Query 2
===========================================================
SELECT mvmnt_data.region
      ,wm.upc
      ,sum(wm.mvmnt_units)    AS salesUnits
      ,sum(wm.mvmnt_dollars) AS salesDollars
      ,wm.store
      ,wm.pos_dept
      ,wm.case_size
      ,SUM(ROUND (CASE WHEN wm.case_size IS NULL OR wm.case_cost=0 THEN NULL 
                       ELSE (wm.case_cost/wm.case_size) * wm.mvmnt_units end, +3)) AS cost
      ,s.store_name
  FROM stella.mvmnt_wkly wm
      ,vim.store@vim s
 WHERE mvmnt_data.region=ir.region(+)
   AND mvmnt_data.upc      = ?
   AND mvmnt_data.upc      = ?
   AND mvmnt_data.region   = ?
   AND mvmnt_data.pos_dept = ?
   AND mvmnt_data.store    = ?
   AND mvmnt_data.upc      = ?
   AND mvmnt_data.store    = ?
   AND wm.mvmnt_date between to_date('10/01/2004', 'mm/dd/yyyy') and to_date('10/03/2004', '/mm/dd/yyyy')
   AND wm.store           in ('10036')
   AND wm.store            = s.ps_bu
   AND wm.pos_dept         = '0110'
 GROUP BY wm.case_size
         ,s.region
         ,wm.pos_dept
         ,wm.upc
         ,wm.store 
         ,s.store_name
===========================================================
So what I would do is call the first query then while looping through it you can do the proper substr on the data. And also call query 2.

I would try to optimize query 2 as much as possable by creating indexs if need be and looking at an execution plan. For example this is what I use for MySQL

deleteplan.sql
===========================================================
delete from plan_table
 where statement_id = 'TEST'

/
commit
/
===========================================================

test.sql
===========================================================
@deleteplan

EXPLAIN PLAN set statement_id = 'TEST' for
SQL STATEMENT GOES HERE

/

@getplan
===========================================================

getplan.sql (there is some other information you can get from the plan_table, but this is all I need most of the time)
===========================================================
select operation, options, object_name, cost, cardinality, TO_CHAR(timestamp, 'HH:MM:SS')
  from plan_table
 where statement_id = 'IFXTEST'
start with id = 0
connect by prior id=parent_id and prior statement_id = statement_id

/
===========================================================
So I just put my SQL in test.sql and run it to get my execution plan. From there you can see where your problems are.

S t r e b e n M ö n c h

In reply to Re^3: Perl CGI timeout by StrebenMönch
in thread Perl CGI timeout by jewang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.