Home Forums SQL Server 2012 SQL Server 2012 - T-SQL What's the best way to get a sample set of a big table without primary key? RE: What's the best way to get a sample set of a big table without primary key?

  • Most typically in SQL Server I see this technique used:

    SELECT TOP ...
    FROM dbo.table_name
    ORDER BY NEWID()

    I presume that gives a roughly random sample.  Of course it likely does require sorting the entire record set.

    If you have an existing index that contains a unique set of columns, you could select just those columns in the "ORDER BY" query, allowing an index scan and sort rather than a full table scan and sort, then join back to the main table after that to get other column(s) from the table.

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