Home » SQL & PL/SQL » SQL & PL/SQL » Generating Random numbers query (Oracle 11.2.0.3, Linux)
Generating Random numbers query [message #609634] Mon, 10 March 2014 10:46 Go to next message
sspn2010
Messages: 167
Registered: October 2008
Senior Member
Hi,

I've below table with the numbers from 10,000,000 to 99,999,999. Now i need to generate random numbers from this table based on the input.

create table TMP_SERIAL_NBR
(
  serial_nbr NUMBER
);

INsert /*+ append parallel(a,16) */ into tmp_serial_nbr a
 SELECT column_value as serial_nbr FROM TABLE(rowgen(10000000,99999999));


create or replace type numtab_type as table of number;

CREATE OR REPLACE FUNCTION rowgen(p_start_num in number, p_end_num IN NUMBER)
RETURN numtab_type DETERMINISTIC PIPELINED PARALLEL_ENABLE
AS
BEGIN
  -- create or replace type numtab_type as table of number;
  FOR i IN p_start_num..p_end_num
  LOOP
    PIPE ROW(i);
  END LOOP;
  
  RETURN;
END rowgen;



I need 40MM random numbers from tmp_serial_nbr and using below code to get random numbers. But this query is running for nearly 25 - 30 minues. Can someone help on how to tune this query to make it efficient?


create table final_random_nbr nologging as
SELECT ROWNUM AS SEQ_NBR, SERIAL_NBR
     FROM (  SELECT SERIAL_NBR
               FROM (SELECT /*+ parallel (tsn 16) */
                           SERIAL_NBR,
                            TRUNC (DBMS_RANDOM.VALUE (1, 999999999))
                               AS SORT_RNK
                       FROM TMP_SERIAL_NBR TSN)
           ORDER BY SORT_RNK)
    WHERE ROWNUM <= 40000000



Appreciate your help!

Thanks
SS
Re: Generating Random numbers query [message #609636 is a reply to message #609634] Mon, 10 March 2014 10:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

dbms_random has to be called for each row of your table TMP_SERIAL_NBR and as you say at least 40000000, so I think Oracle is fairly good to return you in less than half an hour (without speaking about the sort it has to make in the end).

Re: Generating Random numbers query [message #609638 is a reply to message #609636] Mon, 10 March 2014 11:07 Go to previous messageGo to next message
sspn2010
Messages: 167
Registered: October 2008
Senior Member
Thank you Michael!

If i understood it correctly the query is fine and removing "order by sort_rnk" will improve the performance?

Thanks
SS
Re: Generating Random numbers query [message #609642 is a reply to message #609638] Mon, 10 March 2014 13:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Yes.

Re: Generating Random numbers query [message #609645 is a reply to message #609642] Mon, 10 March 2014 15:15 Go to previous message
sspn2010
Messages: 167
Registered: October 2008
Senior Member
Thank you!
Previous Topic: clarification about selectivity
Next Topic: 01843. 00000 - "not a valid month"
Goto Forum:
  


Current Time: Thu Apr 25 04:39:43 CDT 2024