• Jeff Moden (7/9/2013)


    Test Code...

    DECLARE @Bitbucket BIGINT

    SET STATISTICS TIME,IO ON

    SELECT TOP 1000000 @Bitbucket=ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    FROM sys.all_columns ac1, sys.all_columns ac2;

    SELECT TOP 1000000 @Bitbucket=ROW_NUMBER() OVER (ORDER BY (SELECT 0))

    FROM sys.all_columns ac1, sys.all_columns ac2;

    SELECT TOP 1000000 @Bitbucket=ROW_NUMBER() OVER (ORDER BY (SELECT $))

    FROM sys.all_columns ac1, sys.all_columns ac2;

    SET STATISTICS TIME,IO OFF

    They all seem to takes turns winning but all are within just a couple of ms of each other on my Dev server at work.

    As a bit of a side bar and as cool as it is, I prefer not to use the "$" sign trick... I already have people that have difficulty in understanding how the T-SQL behind even this simple thing works. I don't want to push them over the edge. 😀 Other than that, I don't have a preference but my fingers frequently type "(SELECT NULL)" due to some old muscle memory.

    I actually do the same with (SELECT NULL) ... oddly habit forming.

    Speed seems to hold pretty constant up thru 10,000,000 rows, fluctuating a little in favor of one or the other each time you run it.

    Why doesn't SQL allow the ORDER BY to be optional? In which case it should default to "no ordering."


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St