query optimization

  • Hi,

    I have to implement a search functionality. In the various filters for the search, Store Number is one such filter.

    The user should be able to enter range values for store numbers.

    Like 1500-1600. So this should filter for all the stores between 1500 and 1600.

    Similarly, all these also should be valid.

    1550,1600

    1550

    1550 - 1580,6000,8000

    etc.

    I have function which identifies the commas or dashes and separates out the store number and returns a string like

    Stores.Storenumber in(1555,1600)

    Store.StoreNumber between 1555 and 1600 etc...

    i generate a sql at run time and append this piece and then execute the sql.

    I have one of the query below.

    declare @strQuery varchar(max)

    declare @strConcat varchar(10)

    declare @strAppend varchar(max)

    set @strAppend=''

    set @StrConcat ='And '

    if @IsAdmin is null -- Not a Admin

    set @StoreId =(select StoreNumber from Stores where Store_Id = @StoreId)

    set @strQuery='

    Select

    (Select StoreNumber from Stores where Store_id=d.DestinationId) as StoreNumber,

    CartonNumber,

    ActualReceiptDate as [Scan Date],

    isnull(Sum(QtyShipped),0) as [Total Units],

    b.BatchNumber

    from

    Carton c

    left outer join

    CartonDetail Cd on Cd.Carton_Id = c.Carton_ID

    inner join Batch b on b.General_Id = c.Carton_Id and b.BatchType=''Warehouse'' and b.TranTable=''Carton''

    inner join Document d on d.Document_ID = c.document_Id

    inner join Stores st on st.Store_ID = d.SourceID and st.StoreType =5

    inner join Stores on Stores.Store_ID = d.DestinationID

    inner join Codelist cl on cl.Codelist_Id = c.CartonStatus_ID

    inner join Codes on Codes.Code_ID = cl.Code_id and Codes.CodeType=''Cartons Status Code''

    where not c.cartonNumber is null '

    if not (@StoreId) is null

    begin

    set @strAppend = @strConcat + '(' + dbo.DecodeStoreNo(@StoreId) + ')'

    End

    if not (@DateFrom) is null and not (@DateTo) is null

    begin

    set @strAppend = @strAppend + @strConcat + '(convert(varchar(50),c.ActualReceiptDate,101) between ''' + @DateFrom + ''' and ''' + @DateTo + ''')'

    End

    if not (@CartonNumber) is null

    Begin

    set @strAppend = @strAppend + @strConcat + '(c.CartonNumber = ''' + cast(@CartonNumber as varchar) + ''')'

    End

    if not (@Status) is null

    Begin

    set @strAppend = @strAppend + @strConcat + '(cl.Codevalue = ''' + @status + ''')'

    End

    set @strAppend = @strAppend + ' group by

    d.DestinationId,CartonNumber , ActualReceiptDate , b.batchnumber

    order by ActualReceiptDate'

    set @strQuery = @strQuery +@strAppend

    execute(@strQuery)

    This query takes time, if there a little over 1000 records.

    I wanted to know, if there is any way to optimize this query? or any other way in which the above can be accomplished.

    I hope i was able to explain my query fairly. Please let me know otherwise.

    Thanks

    Renu.

  • It would be very helpful if you put this in just before the exec and printed out the result for us...

    PRINT @strQuery

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply