Sunday, September 6, 2009

70-433 SQL Server TABLESAMPLE operator

The following operator can be included a where clause: 


TABLESAMPLE (20 PERCENT)


If you can use a pseudo-random sampling and you're on SQL Server 2005/2008, then take a look at TABLESAMPLE. For instance, an example from SQL Server 2008 / AdventureWorks 2008 which works based on rows:

USE AdventureWorks2008;
GO


SELECT
FirstName, LastName
FROM
Person.Person
TABLESAMPLE
(100 ROWS)
WHERE
EmailPromotion = 2;

The catch is that TABLESAMPLE isn't exactly random as it generates a given number of rows from each physical page. You may not get back exactly 5000 rows unless you limit with TOP as well. If you're on SQL Server 2000, you're going to have to either generate a temporary table which match the primary key or you're going to have to do it using a method using NEWID().

http://stackoverflow.com/questions/652064/select-random-sampling-from-sqlserver-quickly

No comments: