June 28, 2007 at 7:29 am
Hi,
My table has around 750000 records.I need to retrieve 5 random records at each and every execution.
Anyone help me to resolve this issue.
Regards
Karthik
karthik
June 28, 2007 at 8:41 am
Here is what I use to pull random records.
begin
declare @maxcount int,@winner int
declare @winnerid int
-- get number of entries and choose a random number
set @maxcount = (select count(*) from sometable)
if @maxcount > 0
begin
set @winner = (select convert(int,(@maxcount * rand())+1))
-- pull the winner out of the hat
declare hat scroll cursor for select uid from sometable
open hat
fetch absolute @winner from hat into @winnerid
close hat
deallocate hat
select @winnerid
June 28, 2007 at 8:52 am
not very performant but you could try:
select top 5 *
from [your table] with (nolock)
order by newid()
* Noel
June 28, 2007 at 8:58 am
Neold,
is it necessary to give nolock ? or shall i give table name only ?
will NEWID() affect performance ? if the table has lot of records.
Here i have one basic question , what is the difference between NEWID() and RAND() ?
Regards
Karthik
karthik
June 28, 2007 at 9:06 am
Karthik,
The problem with what I posted is that regardless is a full table scan. Nolock will minimize the impact on others by not creating a shared lock in the entire table.
It maybe more resonable to compute something based on your "primary key" and then select those records but then again I have no clue about the definitions of your table.
The difference with rand() is that rand() is exceuted *once* per query and newid() is per record. I just gave you a quick and dirty way of doing it but it is definitely not the most performant.
By the way
did not came out right beacuse I used "<<<" instead of [[[ it is fixed now.
Cheers,
* Noel
June 28, 2007 at 9:16 am
Nelod, Thanks for your answer.
Is the NEWID() function used with ORDERBY clause only ? Can you explain me the reason for using ORDERBY in NEWID() Function ?
*Karthik
karthik
June 28, 2007 at 9:21 am
NEWID() is used to general GUID values which are supposed to be globally unique values.
It is normally used in tables that Identify records using such values.
The use of it on the ORDER BY clause is just a "hack" but it works
For more read BOL.
* Noel
June 28, 2007 at 4:31 pm
To see what's the difference between NEWID() and RAND() try this:
SELECT NEWID(), RAND(), SomeColumn
FROM SomeTable
SomeTable must have more than 1 row.
_____________
Code for TallyGenerator
June 28, 2007 at 5:59 pm
You running a 5 digit lottery?
Seriously, though, just because I'm curious everytime I see such a requirement... what is the business logic that created this requirement? In other words, why do you need to do this?
--Jeff Moden
Change is inevitable... Change for the better is not.
June 28, 2007 at 9:10 pm
Sounds almost like a homework assignment, doesn't it?
ButI use it in my company to give auditors random examples of customers accounts.
June 28, 2007 at 11:08 pm
Thanks, Chris... appreciate that...
Karthik... how about you... what are you going to use this for? The "select random rows" problem is pretty rare in SQL and I'm still curious what you're going to use this for.
As a matter of fact, if anyone has been required to produce random rows, I wouldn't mind hearing from you, either. Thanks
--Jeff Moden
Change is inevitable... Change for the better is not.
June 29, 2007 at 2:31 am
I have used it in a Call Center application. The company just wouldn't accept the possibility that an agent phoned a lead on the same street twice or more sequentially.
The lead lists were bought externally and could be in any sort order. Some times by last name, some times by phone number and some times by street address.
N 56°04'39.16"
E 12°55'05.25"
June 29, 2007 at 6:23 am
July 1, 2007 at 4:00 pm
"As a matter of fact, if anyone has been required to produce random rows, I wouldn't mind hearing from you, either."
I once wrote a query to return 20 Random telephone numbers. I then ran it 100 times to generate test scripts. At the time I was working in telecoms, and we wanted to test new functionality in a UAT environment by getting 100 people to stay an hour over and use the application to query the db.
A fairly large multi-user test was essential to ensure the app would run for its intended target of 600 users.
Dave J
July 1, 2007 at 5:07 pm
Thanks for the feedback, one and all... funny how the telcom business seems to need it the most
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply