Home Forums SQL Server 2008 T-SQL (SS2K8) Curious thought on how to approach making a query like this "better' RE: Curious thought on how to approach making a query like this "better'

  • Row selection is controlled by "only" 4 tables:

    FROM

    THISDATABASE.dbo.ccard_primary AS b WITH (READUNCOMMITTED)

    INNER JOIN THISDATABASE.dbo.ccard_secondary AS a WITH (READUNCOMMITTED)

    ON a.tranid = b.tranid

    INNER JOIN @RD_Fleet_inv_list AS z

    ON z.tranid = b.tranid

    LEFT JOIN THISDATABASE.dbo.CCardLineItems AS c WITH (READUNCOMMITTED)

    ON c.UniqueId = a.TranId

    So that's actually not as bad as it could be. The other JOINs can temporarily be dropped for all preliminary testing. All those LEFT JOINs will kill your performance, so it'll be a huge advantage to test w/o them.

    The insanely bloated "GROUP BY" is another killer: it must be eliminated. I don't think I saw any aggregating function in the query, so it's just a result of "lazy" joins producing duplicate rows. Until you can work out the other issues, DISTINCT will do the same thing but likely with a lot less overhead.

    The lists in the WHERE clause should likely be turned into indexed tables, tested using NOT EXISTS.

    You should take it logically, one step at a time, and you can get through it. [Btw, unless this was a deliberate attempt to cause an issue, be-atching about it just slows you down, and may upset others; be-atch after it's cleaned up, if you feel the need to.]

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.