I have a table that has more than 1 million records and I want to select random rows from this table, but not among all records - only select Random rows from results matching certain conditions.
Performance is very important, so I can NOT use ordering by NEWID and then select first item.
The table structure is some thing like this:
ID BIGINT
Title NVARCHAR(100)
Level INT
Point INT
Now, I wrote a query like:
with
tmp_one as
(
SELECT
R.Id as RID
FROM [User] as U
Inner Join
[Item] as R
On R.UserId = U.Id
WHERE ([R].[Level] BETWEEN @MinLevel AND @MaxLevel)
AND ((ABS((BINARY_CHECKSUM(NEWID(),R.Id,NEWID())))% 10000)/100 ) > @RangeOne
),
tmp_two as
(
Select tmp_one.RID as RID
From tmp_one
Where ((ABS((BINARY_CHECKSUM(NEWID(),RID,NEWID())))% 10000)/100 ) > @RangeTwo
),
tmp_three as
(
Select RID as RID
From tmp_two
Where ((ABS((BINARY_CHECKSUM(NEWID(),NEWID())))% 10000)/100 ) < @RangeThree
)
Select top 10 RID
From tmp_three
I tried to select 10 item randomly, and then select one of them, but I have an amazing problem!!!
Sometimes the output is ordered by item level! And I don't want it (it's not really random ). I really don't know how result was ordered by level.
Please suggest some solution that help me to select random record in high performance and random selected in high range of iteration is not duplicate.