Query taking over 50 mins; Stuff with XML Path

  • Hi I am using Stuff with XML Path for concatenating the data in multiple columns like city, town etc by Zip code. ie., unique column is Zip and all the cities will be concatenated to one cell, similarly town and so on. The query runs over 1 million data, table has 180 columns.

    I tried -

    Adding indexes to the table, it took 50 minutes to run the query.

    Added a temporary table to get only required 20 columns and added indexes to the temporary table and it helped reduce about 3 minutes.

    Checked the execution plan the issue was with the table scan. There are no filters required to generate this report.

    Can you please suggest what else could be done to improve.

    Thanks

    Esha

  • Eshika (10/9/2013)


    Hi I am using Stuff with XML Path for concatenating the data in multiple columns like city, town etc by Zip code. ie., unique column is Zip and all the cities will be concatenated to one cell, similarly town and so on. The query runs over 1 million data, table has 180 columns.

    I tried -

    Adding indexes to the table, it took 50 minutes to run the query.

    Added a temporary table to get only required 20 columns and added indexes to the temporary table and it helped reduce about 3 minutes.

    Checked the execution plan the issue was with the table scan. There are no filters required to generate this report.

    Can you please suggest what else could be done to improve.

    Thanks

    Esha

    We need a LOT more details to offer much help. We would need to see the table with index definitions. The query you are running and the actual execution plan at a bare minimum.

    How to post Performance Problems[/url]

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Eshika (10/9/2013)


    Hi I am using Stuff with XML Path for concatenating the data in multiple columns like city, town etc by Zip code. ie., unique column is Zip and all the cities will be concatenated to one cell, similarly town and so on. The query runs over 1 million data, table has 180 columns.

    I tried -

    Adding indexes to the table, it took 50 minutes to run the query.

    Added a temporary table to get only required 20 columns and added indexes to the temporary table and it helped reduce about 3 minutes.

    Checked the execution plan the issue was with the table scan. There are no filters required to generate this report.

    Can you please suggest what else could be done to improve.

    Thanks

    Esha

    Please see the second link in my signature line below under "Helpful Links".

    --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)

  • Bellow is the code:

    -- getting required columns to a temporary table

    SELECT LTRIM(RTRIM(Zip)) Zip, Code ,LTRIM(RTRIM(City)) CLLI, LTRIM(RTRIM(City)) CLLI,LTRIM(RTRIM(State)) State,Processed ,LTRIM(RTRIM(A))[A],LTRIM(RTRIM())

    ,LTRIM(RTRIM([C]))[C],LTRIM(RTRIM([D]))[D],LTRIM(RTRIM([E]))[E],LTRIM(RTRIM([F]))[F],LTRIM(RTRIM([G]))[G],LTRIM(RTRIM(H))H ,LTRIM(RTRIM(I)) I

    INTO #IMS_StgTemp FROM IMS_StgUpload WHERE (Processed IS NULL OR Processed ='D')

    CREATE NONCLUSTERED INDEX [IDXRT] ON #IMS_ StgTemp

    (

    [Zip] ASC,

    City ASC,

    [A] ASC,

    ASC,

    [C] ASC,

    [D] ASC,

    [E] ASC,

    [F] ASC,

    [G] ASC,

    [H] ASC,

    ASC,

    [State] ASC,

    [Processed] ASC,

    [Code] ASC

    ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    GO

    INSERT INTO #REPORT_ZIP (Zip, Code,State, Totals)

    SELECT LTRIM(RTRIM(Zip)), ISNULL(Code,'') LEAD_OWNER, ISNULL( LTRIM(RTRIM(State)),'') STATE, count(*)

    FROM #IMS_ StgTemp

    WHERE (Processed IS NULL OR Processed ='D')

    GROUP BY LTRIM(RTRIM(Zip)) , ISNULL(Code,''), ISNULL(LTRIM(RTRIM(State)),'')

    ORDER BY LTRIM(RTRIM(Zip)) , ISNULL(Code,''), ISNULL(LTRIM(RTRIM(State)),'')

    UPDATE t

    SET t.CLITY= C.CityList

    , t.Town= C.townList

    ,t.SharedBy =C.SharedOwnerList

    , t.I = C.I

    FROM #B2BICLREPORT_ZIP t INNER JOIN

    (SELECT p.zip, ISNULL(p.OfficeCode,'')OfficeCode

    ,stuff((SELECT DISTINCT ', ' + LTRIM(RTRIM(CLLI)) FROM #IMS_StgB2BLeadsUpload LC

    where (Processed IS NULL OR Processed ='DINP') AND LTRIM(RTRIM(LC. Zip)) = p.Zip

    and ISNULL(LC. Code,'') = ISNULL(p.Code,'') for xml path('')),1,2,'') as ClliList

    ,stuff((SELECT DISTINCT ', ' + LTRIM(RTRIM(City)) FROM #IMS_StgB2BLeadsUpload LC

    where (Processed IS NULL OR Processed ='DINP') and LTRIM(RTRIM(LC.Zip)) = p.Zip

    and ISNULL(LC. Code,'') = ISNULL(p.Code,'') for xml path('')),1,2,'') as CityiList

    ,stuff((SELECT DISTINCT ', ' + LTRIM(RTRIM(Code)) FROM #IMS_StgB2BLeadsUpload LC

    where (Processed IS NULL OR Processed ='DINP') and LTRIM(RTRIM(LC. Zip)) = p.Zip

    and ISNULL(LC. Code,'') != ISNULL(p. Code,'') for xml path('')),1,2,'') as SharedOwnerList

    , (select count(*) FROM #IMS_StgB2BLeadsUpload LC

    where (Processed IS NULL OR Processed ='DINP') and LTRIM(RTRIM(LC.SrvcZip)) = p.Zip

    and ISNULL(LC. Code,'') != ISNULL(p. Code,'')) I

    FROM #B2BICLREPORT_ZIP p

    GROUP BY p.Zip, ISNULL(p.Code,''),CLLI, City, Code) C

    ON C. Zip = t.Zip and C. Code = ISNULL(t.Code,'')

  • Again, without table definitions, indexes and an actual execution plan we are shooting in the dark. Both Jeff and I have suggested you read the article about how to post performance problems. Without that information we can't provide informed assistance.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Eshika (10/9/2013)


    Bellow is the code:

    I think maybe not. The "C" derived table doesn't have the "townlist" column that have you in the update list. Also, why are you calculating a "ClliList" when it's not being used anywhere and that same thing goes for the two temp tables you create... they're also not being used anywhere.

    Please help us help you. Read the article that you've been directed to.

    --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 6 posts - 1 through 5 (of 5 total)

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