The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Table Scan Hell

Last post 08-11-2006 9:20 AM by versatilia. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 08-09-2006 3:08 PM

    • db2
    • Top 200 Contributor
    • Joined on 06-15-2006
    • Posts 219

    Table Scan Hell

    What began as figuring out why some existing reporting procedures were taking longer than I'd like quickly went downhill.

    "Gee, why is this one query accounting for 98.5% of the workload?"
    Lessons learned:
    Making your most selective WHERE conditions part of AND/OR decisions that refer to both sides of your self-join = BAD DEVELOPER, NO BISCUIT. Even more so when every other part of the WHERE clause gives you a selectivity of maybe 80% at best, and your selective column has no index anyway.

    Oh, did I mention there's a report page that calls this procedure about 7 times in a row? At least there aren't any cursors.
  • 08-09-2006 6:03 PM In reply to

    • emurphy
    • Top 75 Contributor
    • Joined on 01-14-2005
    • Granada Hills, CA
    • Posts 512

    Re: Table Scan Hell

    db2:

    Making your most selective WHERE conditions part of AND/OR decisions that refer to both sides of your self-join = BAD DEVELOPER, NO BISCUIT.


    Do you mean something like this?

    select a.x, b.y
    from a, b
    where a.m = b.n or a.p = b.q

    as opposed to

    select a.x, b.y
    from a join b on a.m = b.n
    union
    select a.x, b.y
    from a join b on a.p = b.q

  • 08-09-2006 6:34 PM In reply to

    Re: Table Scan Hell

    Could anyone tell me, why, even though I have indexed each column, and explain select count( distinct visits) from stats says it's going to use it, still select count( distinct visits) from stats takes several seconds (while the result is 442, which in most brain-dead way I could invented leads to 442 btree records being read?). I'm referring to MySQL server. Is it so hard to determine the number of nodes in the btree? Is it due to some parallel issues?

    And the query I was really interested in was Select Sum(visits) From stats, which I was sure should take about 441 additions and multiplications, but I'll forgive the optimizer lack of arithmetic knowledge...but still...what are those btrees for, then - just to find a specific row ? What a waste of potential potential

  • 08-09-2006 6:38 PM In reply to

    • db2
    • Top 200 Contributor
    • Joined on 06-15-2006
    • Posts 219

    Re: Table Scan Hell

    Close. It's sort of like this:

    SELECT subtotal
    FROM orders AS orders1 LEFT JOIN orders AS orders2
    ON orders1.orignum = orders2.ordnum
    WHERE
    --a few business logic conditions on fields in orders1 with lousy selectivity--
    AND ((orders1.orderdate BETWEEN @startDate AND @endDate) OR (orders1.origtype = 0 AND orders1.docdate BETWEEN @startDate AND @endDate)
    OR orders2.orderdate BETWEEN @startDate AND @endDate)

    That's kind of anonymized and simplified, but you get the idea. Alone, either of these two conditions is perfectly selective, and will go to an index in the optimizer:

    (orders1.orderdate BETWEEN @startDate AND @endDate) OR (orders1.origtype = 0 AND orders1.docdate BETWEEN @startDate AND @endDate)

    orders2.orderdate BETWEEN @startDate AND @endDate

    But when you join the two with an OR... Yeah. The necessity of truth for either condition depends on the truth of the other, so the indexes (if we actually had any on that column) sit and collect dust. Instead, it has to perform a cross-join on the two tables first before it can filter the records. Whee. I'm thinking I'm going to need some indexed view trickery and splitting things into clever UNIONs to pull this one off. Unless anybody's got a better idea that'll work on SQL Server 2000. :)
  • 08-09-2006 7:27 PM In reply to

    • Nick
    • Not Ranked
    • Joined on 01-12-2006
    • Posts 2

    Re: Table Scan Hell

    [Obvious joke about mysql not being a real database]
    [Lame recommendation to try postgresql, oracle, sqlite, etc]


    Something clever.
  • 08-09-2006 8:24 PM In reply to

    • db2
    • Top 200 Contributor
    • Joined on 06-15-2006
    • Posts 219

    Re: Table Scan Hell

    Nick:
    [Obvious joke about mysql not being a real database]
    [Lame recommendation to try postgresql, oracle, sqlite, etc]




    [obligatory varchar2 dig]
  • 08-10-2006 6:40 AM In reply to

    • Fred
    • Not Ranked
    • Joined on 01-25-2006
    • Norway
    • Posts 23

    Re: Table Scan Hell

    db2:
    Nick:
    [Obvious joke about mysql not being a real database]
    [Lame recommendation to try postgresql, oracle, sqlite, etc]




    [obligatory varchar2 dig]


    [varchar2 retort]
    [general, unrelated stab at the uselessness of VB, in any situation, and how <programming-language> solves all my problems]
  • 08-10-2006 7:22 AM In reply to

    Re: Table Scan Hell

    Fred:
    db2:
    Nick:
    [Obvious joke about mysql not being a real database]
    [Lame recommendation to try postgresql, oracle, sqlite, etc]




    [obligatory varchar2 dig]


    [varchar2 retort]
    [general, unrelated stab at the uselessness of VB, in any situation, and how <programming-language> solves all my problems]

    <generic comment about finding salvation in xml/>
  • 08-11-2006 9:20 AM In reply to

    Re: Table Scan Hell

    nonDev:
    Fred:
    db2:
    Nick:
    [Obvious joke about mysql not being a real database]
    [Lame recommendation to try postgresql, oracle, sqlite, etc]




    [obligatory varchar2 dig]


    [varchar2 retort]
    [general, unrelated stab at the uselessness of VB, in any situation, and how <programming-language> solves all my problems]

    <generic comment about finding salvation in xml/>


    [ob. ref to http://efw.livejournal.com/ followed by meaningless reference to a famous WTF posting]
Page 1 of 1 (9 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems