• easy_goer (10/1/2013)


    Hello. I ran the following script that too 3 hours 25 minutes to insert about 40,000 rows. Is there any logic that I can put into this to speed it up? Thank you!

    insert into T3

    select T2.COL1, T2.COL2, T1.COL1, T1.COL2, T1.COL3

    from T1

    join T2 on T1.COL4 = T2.COL3

    where T2.COL4 != 'data1'

    and T1.COL1 is not NULL

    and T1.COL5 is not NULL

    and T1.COL6 = 'data2'

    and CAST (T2.COL1 as VARCHAR)+'-'+CAST(T2.COL2 as VARCHAR)+'-'+CAST(T1.COL1 as VARCHAR)+'-'+CAST(T1.COL2 as VARCHAR) not in

    (select CAST(COL1 as VARCHAR)+'-'+CAST(COL2 as VARCHAR)+'-'+CAST(COL3 as VARCHAR)+'-'+CAST(COL4 as VARCHAR) from T3)

    We can't really help performance issues on theoretical tables. I can say that your performance issues are all based in your where clause. You have lots of nonSARGable predicates here.

    != will force a full scan.

    Also you cast all these columns to varchar. You didn't specify a length so it will use the default length. Do you know the default length for varchar? Me neither, that is why you should always specify the length.

    If you want some real assistance with the performance issues you should take a look at this article.

    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/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/