Pergunta

I am using SQL Anywhere as my database.

I have two tables SALES DETAIL (POSDETAIL) and Inventory Adjusted table (AdjustInventory) and creating and query to show sales and wastage/adjusted query.

Output should be like this

Product   SalesQty  Value    WastageQty    Value
------------------------------------------------

Sales Qty will come from POSDETAIL table and WastageQty will come from AdjustInventory

POSDETAIL has total 435625 records and AdjustInventory has 183528 total records.

I have designed below query it is giving me perfect out as needed, but the issue is query is very very slow it takes almost 10 to 15 mnts to display any particular data with in any date range.

This query is needed optimization and tuning here is the query.

select    
    p.OrderDate, p.ProductId, p.SalesQty,
    f.WastedQty / p.SalesQty as WastedQty,
    p.NetCost, p.EachCost
from
    (select 
         POSDETAIL.PRODNUM as ProductId,
         DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd') as OrderDate, 
         POSDETAIL.NETCOSTEACH as NetCost,POSDETAIL.COSTEACH as EachCost,
         SUM(POSDETAIL.QUAN) as SalesQty
     from 
         DBA.POSDETAIL
     group by 
         POSDETAIL.PRODNUM, POSDETAIL.NETCOSTEACH, POSDETAIL.COSTEACH, 
         DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
) as p (ProductId, OrderDate, NetCost, EachCost, SalesQty) 
left outer join
    (select 
         DBA.AdjustInventory.INVENNUM as ProductId,
         DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd') as OrderDate,
         SUM(AdjustInventory.ADJUSTUNITS) as WastedQty
     from 
         DBA.POSDETAIL
     join 
         DBA.AdjustInventory on DBA.POSDETAIL.PRODNUM = DBA.AdjustInventory.INVENNUM
                             and DBA.AdjustInventory.AdjustType = 9 
                             and DATEFORMAT(DBA.AdjustInventory.AdjustTime,'yyyy/mm/dd') = DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
     group by 
         DBA.AdjustInventory.INVENNUM, DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
    ) as f(ProductId, OrderDate, WastedQty) on p.ProductId = f.ProductId and p.OrderDate = f.OrderDate;

Please help

Foi útil?

Solução

OK here is your problem potentially. Never group by in a nested select. 1. Pull the select out make it a temp table.
2. Index the temp table 3. Join the temp table to the outer query.

You should be able to do so twice here and it should really help.

Outras dicas

not 100% sure of syntax for SQL

select 
         POSDETAIL.PRODNUM as ProductId,
         DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd') as OrderDate, 
         POSDETAIL.NETCOSTEACH as NetCost,POSDETAIL.COSTEACH as EachCost,
         SUM(POSDETAIL.QUAN) as SalesQty
     from 
         DBA.POSDETAIL
     into #P
     group by 
         POSDETAIL.PRODNUM, POSDETAIL.NETCOSTEACH, POSDETAIL.COSTEACH, 
         DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')

select 
         DBA.AdjustInventory.INVENNUM as ProductId,
         DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd') as OrderDate,
         SUM(AdjustInventory.ADJUSTUNITS) as WastedQty
     from 
         DBA.POSDETAIL
     Into #F
     join 
         DBA.AdjustInventory on DBA.POSDETAIL.PRODNUM = DBA.AdjustInventory.INVENNUM
                             and DBA.AdjustInventory.AdjustType = 9 
                             and DATEFORMAT(DBA.AdjustInventory.AdjustTime,'yyyy/mm/dd') = DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')
     group by 
         DBA.AdjustInventory.INVENNUM, DATEFORMAT(POSDETAIL.TIMEORD,'yyyy/mm/dd')




select    
    p.OrderDate, p.ProductId, p.SalesQty,
    f.WastedQty / p.SalesQty as WastedQty,
    p.NetCost, p.EachCost
from #p (ProductId, OrderDate, NetCost, EachCost, SalesQty)
Left outer Join #f
     on p.ProductId = f.ProductId and p.OrderDate = f.OrderDate;

Its been about 16 years since I used SQL Anywhere so sorry that is psudeo code

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top