Home Forums SQL Server 2005 T-SQL (SS2K5) Using ORDER BY in a query returns more rows than if ORDER BY is not used RE: Using ORDER BY in a query returns more rows than if ORDER BY is not used

  • Luis Cazares (10/2/2014)


    There's no way that your ORDER BY will change the results, it must be something else.

    + 1. I don't see how an ORDER BY clause can possibly change the rows being returned. Are rows being written to the table between the times you run it? That's the only thing I can think of that would cause the problem you describe.

    Luis Cazares (10/2/2014)


    Your query is full of UDFs that will affect performance in a severe way, specially with the one in the WHERE clause and the functions on your JOIN clause.

    Oh my, this is so very true. It looks like part of an ETL process where you're importing and massaging the data before writing it to a permanent table. If this is true, you've already imported the data at this point. You'd do well to fire UPDATE statements against the data to trim off the unwanted spaces.

    I think Luis was being very nice is saying "affect performance in a severe way". I would say that they're going to kill it. Give some thought to breaking down all the steps you're doing here into more manageable pieces. The adage "divide and conquer" comes to mind. If you can convert some of the scalar UDFs into ITVFs, that will help tremendously. You can also add some columns to your intermediate tables and calculate some of this stuff in-place ahead of time. This will also help because you'll be able to verify your data at each step along the way and moving on to the next one until you know they're all good. In the end, your heavy lifting should already be done before you write to your production table.