Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2005
    Posts
    4

    Unanswered: Help needed to improve the performance of the query

    Hello,
    I have the following setup and I would appreciate any help in improving
    the performance of the query.


    BigTable:
    Column1 (indexed)
    Column2 (indexed)
    Column3 (no index)
    Column4 (no index)


    select
    [time] =
    CASE
    when BT.Column3 = 'value1' then DateAdd(...)
    when BT.Column3 in ('value2', 'value3') then DateAdd(...)
    END,
    Duration =
    CASE
    when BT.Column3 = 'value1' then DateDiff(...)
    when BT.Column3 in ('value2', 'value3') then DateDiff(ss,
    BT.OrigTime, (select TOP 1 X.OrigTime from BigTable X where X.Column1 >
    BT.Column1 and X.Column3 <> 'value4' order by X.Column1 ))
    END,


    FROM
    BigTable BT where BT.Column3 = 'value1' OR (BT.Column3 in ('value2',
    'value3') and BT.Column4 <> (select X.Column4 from BigTable X where
    X.Column1 = BT.Column1 and X.Column3 = 'Value1'))


    Apart from the above mentioned, there are a few more columns which are
    just a part of select statement and are not in any condition statments.


    The BigTable has around 1 Mil records and the response time is very
    poor, it takes around 3 mins to retrieve the records (which would be
    around 500K)


    With the Statistics ON,
    I get the following:


    Table 'BigTable'. Scan count 2, logical reads 44184, physical reads 0,
    read-ahead reads 0.
    Table 'WorkTable'. Scan count 541221, logical reads 4873218, physical
    reads 0, read-ahead reads 0.


    Is there any way to increase the performance, so that I can get the
    result under 1 minute?
    Any help would be appreciated.


    P.S: I tried indexing the Column3, but no improvement.

    -SR

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    whoops this might not owork. let me test.
    Last edited by Thrasymachus; 02-28-05 at 15:18.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    OK. Instead of cyling through your entire big table in you your where clause for every record you should do something like this. Mine is a little more dumbed down but you should get the concept

    CREATE TABLE test2
    (
    [ID] int IDENTITY,
    col1 int,
    col2 int,
    col3 int
    )

    INSERT INTO test2(col1,col2,col3)
    VALUES (1,2,3)

    INSERT INTO test2(col1,col2,col3)
    VALUES (3,2,1)

    INSERT INTO test2(col1,col2,col3)
    VALUES (2,3,3)

    SELECT A.col1,A.col2,A.col3
    FROM test2 A
    LEFT OUTER JOIN test2 B
    ON A.[ID] = B.[ID]
    WHERE A.col2 <> B.col3

    I hate subqueries in the where clause.
    Last edited by Thrasymachus; 02-28-05 at 15:38.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    Is Column1 the primary key of the big table?

  5. #5
    Join Date
    Feb 2005
    Posts
    4
    Thanks a lot for your reply.
    The problem is I cannot change the database (I cannot add new tables)
    I have to define a view to use the existing tables.

    I have created a view using the select statement mentioned in the first post, and doing a select on view.

    Quote Originally Posted by Thrasymachus
    OK. Instead of cyling through your entire big table in you your where clause for every record you should do something like this. Mine is a little more dumbed down but you should get the concept

    CREATE TABLE test2
    (
    [ID] int IDENTITY,
    col1 int,
    col2 int,
    col3 int
    )

    INSERT INTO test2(col1,col2,col3)
    VALUES (1,2,3)

    INSERT INTO test2(col1,col2,col3)
    VALUES (3,2,1)

    INSERT INTO test2(col1,col2,col3)
    VALUES (2,3,3)

    SELECT A.col1,A.col2,A.col3
    FROM test2 A
    LEFT OUTER JOIN test2 B
    ON A.[ID] = B.[ID]
    WHERE A.col2 <> B.col3

    I hate subqueries in the where clause.

  6. #6
    Join Date
    Feb 2005
    Posts
    4
    Nope,
    Column2 is the primary key.

    Quote Originally Posted by MCrowley
    Is Column1 the primary key of the big table?

  7. #7
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    I did'nt tell you to create any new tables. This was a demonstration of recursive querying on the same table without doing a subquery against a million records in the where clause (which I think is the source of your performance issue). I did'nt have time to rewrite your whole thing. Sorry.

    If you take a look at the example again I am joining the table to itself by using aliases.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  8. #8
    Join Date
    Feb 2005
    Posts
    4
    Quote Originally Posted by Thrasymachus
    I did'nt tell you to create any new tables. This was a demonstration of recursive querying on the same table without doing a subquery against a million records in the where clause (which I think is the source of your performance issue). I did'nt have time to rewrite your whole thing. Sorry.

    If you take a look at the example again I am joining the table to itself by using aliases.
    My bad. Didn't read it completely.
    I put the left outer join as you mentioned, but haven't seen any improvement.

  9. #9
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    What indices do you have on this table? I would create seperate indices for columns 1,3, and for because they are all being searched. Perhaps a clustered index on the column with the highest number of distinct values and nonclustered on the other 2.

    Have you looked at the execution? Where are your table scans occurring? I do not see why you have column2 indexed. I do not see where it is joined on or searched.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •