niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Oracle
Retrieving a set of randomly chosen records (Oracle 9i)
Post new topic   Reply to topic Page 1 of 2 [16 Posts] View previous topic :: View next topic
Goto page:  1, 2 Next
Author Message
casey.kirkpatrick@gmail.c
*nix forums beginner


Joined: 08 Jun 2005
Posts: 20

PostPosted: Tue Feb 22, 2005 10:33 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

select * from table where DBMS_UTILITY.get_hash_value(ROWID ||
to_char(sysdate,'HH:MI:SS'),1,100) = 1

The "100" can be tweaked to give more or fewer rows.
Back to top
andreas.krisor@gmx.net
*nix forums beginner


Joined: 18 Feb 2005
Posts: 8

PostPosted: Tue Feb 22, 2005 1:52 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

Hi Steve,

thank you for your suggestion but the statement runs about half an
hour, so it doesn't really work for me.

Greetz,
Andreas
Back to top
SteveSutley@cox.net
*nix forums beginner


Joined: 21 Feb 2005
Posts: 1

PostPosted: Mon Feb 21, 2005 2:46 am    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

Andreas,

The SAMPLE is best, but if it is not good for you, you can try MOD.
This example returns 1 row from every 100 rows.

SELECT columnA, columnB
FROM your_table
GROUP BY columnA, columnB
HAVING MOD(MAX(ROWNUM), 100) = 1

The result set is automatically ordered. The same rows are picked each
time (if the table is not changed).

HTH

Steve


andreas.krisor@gmx.net wrote:
Quote:
Hi,

I need to analyze the data of very large tables (over 10 million
records). Therefore I want to retrieve a set of randomly chosen
records
(about 50,000).

Question: Exists a predefined function in Oracle 9i, which fetches a
certain part (e.g. 1%) of the rows of a table?

Hint: Because I need to get all of the data in a random order and not
in the way the rows are arranged in the table I cannot work with
SELECT
* from xxx WHERE ROWNUM < 50001.
But maybe the ROWID can be utilized to fetch a random sample of the
rows?

King regards,

Andreas
Back to top
Malcolm Dew-Jones
*nix forums Guru


Joined: 04 Mar 2005
Posts: 418

PostPosted: Sun Feb 20, 2005 3:45 am    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

andreas.krisor@gmx.net (andreas.krisor@gmx.net) wrote:
: Hi Malcolm,

: I have at the moment no possibility to try your suggestion but I think
: the ROWNUM refers to the result set of a query, so a full table scan is
: used for this statement which reduces the performance dramatically and
: therefore wouldn't be applicable.

: Am I right?

maybe, I don't know for sure

However my suggestion was less than optimal, not sure what I was thinking.
I haven't tested the code but I'm pretty sure you don't need to select
from a select, simply

select * from table where rownum modulus whatever


That could in theory be optimized at the remote end to retrieve only a
some of the rows.

As for a full table scan (which I also responded to just above) if you
want any kind of randomish sample then surely you will have to scan the
whole table - the only issue is whether the remote end can do the scan so
as to reduce the amount of data that must be sent.



--

This space not for rent.
Back to top
andreas.krisor@gmx.net
*nix forums beginner


Joined: 18 Feb 2005
Posts: 8

PostPosted: Sat Feb 19, 2005 7:53 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

Hi,

this probably works, but unfortunately I don't have the CREATE VIEW
priv in my schema.
Back to top
andreas.krisor@gmx.net
*nix forums beginner


Joined: 18 Feb 2005
Posts: 8

PostPosted: Sat Feb 19, 2005 7:50 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

Hi Malcolm,

I have at the moment no possibility to try your suggestion but I think
the ROWNUM refers to the result set of a query, so a full table scan is
used for this statement which reduces the performance dramatically and
therefore wouldn't be applicable.

Am I right?

Greetz,
Andreas
Back to top
andreas.krisor@gmx.net
*nix forums beginner


Joined: 18 Feb 2005
Posts: 8

PostPosted: Sat Feb 19, 2005 7:44 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

Hi Voker,

yes, it refers to another oracle-instance. Is there a difference
between the communication between 2 Oracle DB and an Oracle DB and for
example a DB2 DB?

Greetz,
Andreas
Back to top
Malcolm Dew-Jones
*nix forums Guru


Joined: 04 Mar 2005
Posts: 418

PostPosted: Fri Feb 18, 2005 9:25 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

andreas.krisor@gmx.net (andreas.krisor@gmx.net) wrote:
: Hi,

: I need to analyze the data of very large tables (over 10 million
: records). Therefore I want to retrieve a set of randomly chosen records
: (about 50,000).

: Question: Exists a predefined function in Oracle 9i, which fetches a
: certain part (e.g. 1%) of the rows of a table?

: Hint: Because I need to get all of the data in a random order and not
: in the way the rows are arranged in the table I cannot work with SELECT
: * from xxx WHERE ROWNUM < 50001.
: But maybe the ROWID can be utilized to fetch a random sample of the
: rows?

why not select every nth row? Not exactly random, but it will select rows
throughout the entire table, which would probably make it representative.

I can't seem to lookup the modulus function to check my syntax, but
something like the following


select * from
( select *,rownum the_rownum from the_table)
)
where modulus(the_rownum,200)=1


You could use a RAND function if it existed to do a similar selection, but
I don't see such a function in my index.

--

This space not for rent.
Back to top
IANAL_VISTA
*nix forums Guru


Joined: 23 Apr 2005
Posts: 485

PostPosted: Fri Feb 18, 2005 2:48 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

"andreas.krisor@gmx.net" <andreas.krisor@gmx.net> wrote in
news:1108737915.215801.132860@f14g2000cwb.googlegroups.com:

Quote:
Hi David,

thank you for this hint, it works partly:

my SELECT-statements refer to tables via a db-link and using SAMPLE
results in an error.

Do you know how I can use SAMPLE with db-links?


I can't say that I've tried this, but does it work to create a VIEW
in the remote DB which contains the SAMPLE clause?

SELECT * FROM REMOTE_VIEW@OTHER_DB;
Back to top
Volker Hetzer
*nix forums Guru Wannabe


Joined: 04 Apr 2005
Posts: 214

PostPosted: Fri Feb 18, 2005 2:29 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

<andreas.krisor@gmx.net> schrieb im Newsbeitrag news:1108737915.215801.132860@f14g2000cwb.googlegroups.com...
Quote:
Hi David,

thank you for this hint, it works partly:

my SELECT-statements refer to tables via a db-link and using SAMPLE
results in an error.

Do you know how I can use SAMPLE with db-links?
Is the link to another oracle db?


Greetings!
Volker
Back to top
andreas.krisor@gmx.net
*nix forums beginner


Joined: 18 Feb 2005
Posts: 8

PostPosted: Fri Feb 18, 2005 1:49 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

Hi Norm,

thank you, but still I have a problem (see below).

Greetz,
Andreas
Back to top
andreas.krisor@gmx.net
*nix forums beginner


Joined: 18 Feb 2005
Posts: 8

PostPosted: Fri Feb 18, 2005 1:45 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

Hi David,

thank you for this hint, it works partly:

my SELECT-statements refer to tables via a db-link and using SAMPLE
results in an error.

Do you know how I can use SAMPLE with db-links?

Thanks,
Andreas
Back to top
IANAL_VISTA
*nix forums Guru


Joined: 23 Apr 2005
Posts: 485

PostPosted: Fri Feb 18, 2005 1:40 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

"andreas.krisor@gmx.net" <andreas.krisor@gmx.net> wrote in
news:1108736215.318122.236000@g14g2000cwa.googlegroups.com:

Quote:
Hi,

I need to analyze the data of very large tables (over 10 million
records). Therefore I want to retrieve a set of randomly chosen records
(about 50,000).

Question: Exists a predefined function in Oracle 9i, which fetches a
certain part (e.g. 1%) of the rows of a table?

Hint: Because I need to get all of the data in a random order and not
in the way the rows are arranged in the table I cannot work with SELECT
* from xxx WHERE ROWNUM < 50001.
But maybe the ROWID can be utilized to fetch a random sample of the
rows?

King regards,

Andreas



Go lookup the keyword "SAMPLE" as part of the SELECT statement
Back to top
David Aldridge
*nix forums beginner


Joined: 03 Feb 2005
Posts: 46

PostPosted: Fri Feb 18, 2005 1:20 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

You can also try the SAMPLE clause in the select statement. SQL
Reference for details
Back to top
Norman Dunbar
*nix forums beginner


Joined: 09 Feb 2005
Posts: 24

PostPosted: Fri Feb 18, 2005 1:16 pm    Post subject: Re: Retrieving a set of randomly chosen records (Oracle 9i) Reply with quote

andreas.krisor@gmx.net wrote:
Quote:
Hi,

I need to analyze the data of very large tables (over 10 million
records). Therefore I want to retrieve a set of randomly chosen records
(about 50,000).

Question: Exists a predefined function in Oracle 9i, which fetches a
certain part (e.g. 1%) of the rows of a table?

Hi Andreas,


try this :

SELECT <stuff>
FROM <table>
SAMPLE(1);

The number in brackets (1) is the percentage of the table you want. You
will usually get a different sample on each execution. You cannot
guarantee that you will always get the same number of rows in the sample
though.


Cheers,
Norm.
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 2 [16 Posts] Goto page:  1, 2 Next
View previous topic :: View next topic
The time now is Thu Jan 08, 2009 5:13 am | All times are GMT
navigation Forum index » Databases » Oracle
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Move Oracle 10g database to another location Selt Server 0 Fri Jul 21, 2006 2:14 pm
No new posts Oracle runtime Spitfire Server 0 Fri Jul 21, 2006 1:18 pm
No new posts Oracle Text Score Computation jatinder.1975@gmail.com Server 0 Fri Jul 21, 2006 1:00 pm
No new posts hi, I am not able view all the records in the tabel Annam Srinivas MySQL 0 Fri Jul 21, 2006 7:42 am
No new posts Can a trigger insert records into another oracle server Tauqir Server 2 Fri Jul 21, 2006 12:27 am

Best Credit Cards | Proxy | Loans | Bankruptcy | Credit Card Consolidation
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.3475s ][ Queries: 16 (0.1658s) ][ GZIP on - Debug on ]