How to efficiently select random rows from a large table ?

Hello,
The following code will select 5 rows out of a random set of rows from the emp (employee) table
select *
  from (
       select ename, job
         from emp
       order by dbms_random.value()
where rownum <= 5my concern is that the inner select will cause a table scan in order to assign a random value to each row. This code when used against a large table can be a performance problem.
Is there an efficient way of selecting random rows from a table without having to do a table scan ? (I am new to Oracle, therefore it is possible that I am missing a very simple way to perform this task.)
thank you for your help,
John.
Edited by: 440bx on Jul 10, 2010 6:18 PM

Have a look at the SAMPLE clause of the select statement. The number in parenthesis is a percentage of the table.
SQL> create table t as select * from dba_objects;
Table created.
SQL> explain plan for select * from t sample (1);
Explained.
SQL> @xp
PLAN_TABLE_OUTPUT
Plan hash value: 2767392432
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT    |      |   725 | 70325 |   289   (1)| 00:00:04 |
|   1 |  TABLE ACCESS SAMPLE| T    |   725 | 70325 |   289   (1)| 00:00:04 |
8 rows selected.

Similar Messages

  • How can i select the rows from the 3 tables by particular column name?

    Vehicle Passing Summary table structure
              PsngSmry_ID(Number),Vehicle_iD(Number),PsngSmryTime(datetime)
    Vehicle table structure
              Vehicle_iD(Number),VehicleName(VarChar2),VehicleType(VarChar2)
    Here Vehicle_iD is the Primary Key
    Equipment Table Structure
              Eqpmt_id(Number),Vehicle_iD(Number),EqpmtName(VarChar2),EqpmtType(VarChar2)
    Here Eqpmt_id is the Primary Key and Vehicle_iD is the foreign Key
    Equipment Component Table Structure
              Eqpmt_Cmpnt_id(Number) ,Eqpmt_id(Number),EqpmtCmpntName(VarChar2),EqpmtCmpntName(VarChar2),Parent_Eqpmnt_ID(Number)
    Here Eqpmt_Cmpnt_id is the Primary Key and Eqpmt_id is the foreign Key
    The rows in the Vehicle Passing Summary table
         PsngSmry_ID Vehicle_ID     PsngSmryTime
         111111          80986246     2010/10/11
         111112          80986247     2010/10/12
         111113          80986248     2010/10/10
    The rows in the Vehicle Table
         Vehicle_iD     VehicleName     VehicleType
         80986246          Lorry          Four Wheeler
         80986247          Van          Four Wheeler
         80986248          Bus          Four Wheeler
    The rows in the Equipment Table:
         Eqpmt_id     Vehicle_iD     EqpmtName          EqpmtType
         109846          80986246          2 Axle Lorry          CAR
    109821          80986246          4 Axle Lorry          CAR
    109825          80986246          4 Axle Lorry          CAR
         109562          80986247          2 Axle VAn          CAR
    109555          80986247          3 Axle VAn          CAR
    109777          80986247          3 Axle VAn          CAR
         109587          80986248          2 Axle Bus          CAR
    The rows in the Equipment Component Table :
         Eqpmt_Cmpnt_id Eqpmt_id EqpmtCmpntName Parent_Eqpmnt_ID
         20904146          109846          Truck               
         20904147          109846          Truck
         20904148          109846          Axle               20904146
         20904159          109846          Axle               20904146
         20904167          109846          Wheel               20904148
         20904177          109846          Wheel               20904148
         20904185          109846          Wheel               20904159
         20904325          109846          Wheel               20904159
         20904188          109846          Axle               20904147
         20904189          109846          Axle               20904147
         20904195          109846          Wheel               20904188
         20904196          109846          Wheel               20904188
         20904197          109846          Wheel               20904189
         20904398          109846          Wheel               20904189
         10904146          109562          Truck               
         10904147          109562          Truck
         10904148          109562          Axle               10904146
         10904159          109562          Axle               10904146
         10904167          109562          Wheel               10904148
         10904177          109562          Wheel               10904148
         10904185          109562          Wheel               10904159
         10904325          109562          Wheel               10904159
         10904188          109562          Axle               10904147
         10904189          109562          Axle               10904147
         10904195          109562          Wheel               10904188
         10904196          109562          Wheel               10904188
         10904197          109562          Wheel               10904189
         10904398          109562          Wheel               10904189
    Note : In Equipment Component Table,the hierarchy will be Truck-->Axle-->Wheel.So
    1.the Parent_Eqpmnt_ID of Axle is Truck's Eqpmt_Cmpnt_id.
    2.the Parent_Eqpmnt_ID of Wheel is Axle's Eqpmt_Cmpnt_id.
    Now I want to write the store procedure which will take "PsngSmry_ID(Number)" as input and the o/p will be in the format :
         Eqpmt_Cmpnt_id Eqpmt_id EqpmtCmpntName Parent_Eqpmnt_ID
         20904146          109846      Truck     
         20904148          109846      Axle               20904146
         20904167          109846      Wheel               20904148
         20904177          109846      Wheel               20904148     
         20904159          109846      Axle               20904146
         20904185          109846      Wheel               20904159
         20904325          109846      Wheel               20904159
         20904147          109846      Truck
         20904188          109846      Axle               20904147
         20904195          109846      Wheel               20904188
         20904196          109846      Wheel               20904188
         20904189          109846      Axle               20904147
         20904197          109846      Wheel               20904189
         20904398          109846      Wheel               20904189
         10904146          109562          Truck     
         10904148          109562          Axle               10904146
         10904167          109562          Wheel               10904148
         10904177          109562          Wheel               10904148     
         10904159          109562          Axle               10904146
         10904185          109562          Wheel               10904159
         10904325          109562          Wheel               10904159
         10904147          109562      Truck
         10904188          109562      Axle               10904147
         10904195          109562      Wheel               10904188
         10904196          109562      Wheel               10904188
         10904189          109562      Axle               10904147
         10904197          109562      Wheel               10904189
         10904398          109562      Wheel               10904189
    **Please add these columns in the o/p **
    1.EqpmtName and EqpmtType from Eqpmt table
    2.VehicleName and Vehicle Type from Vehicle table
    3.PsngSmryTime from PassingSummary table **
    Can anyone tell me the solution?
    Edited by: 865216 on Jun 22, 2011 2:14 AM

    Hi,
    I am new to this technology;But, just wanted to help you.
    Please refer the code it might be atleast helpful for you.
    But am facing PL/SQL: ORA-00933: error and unable to resolve it.could anyone please correct this code.
    sorry if my code is totally wrong.
    Thank you,
    Rupa
    create or replace procedure Equipment_proc
    is
    begin
    DECLARE
    TYPE EQP_record IS RECORD (
    Vehicle_iD Vehicle.Vehicle_iD%TYPE,
    Eqpmt_Cmpnt_id Equipment_Component.Eqpmt_Cmpnt_id%type,
    EqpmtCmpntName Equipment_Component.EqpmtCmpntName%type,
    Parent_Eqpmnt_ID Equipment_Component.Parent_Eqpmnt_ID%type,
    Eqpmt_id Equipment.Eqpmt_id%type,
    vehicleId NUMBER);
    EQP_rec EQP_record;
    CURSOR EQP_cursor IS
    select a.Vehicle_iD,c.Eqpmt_Cmpnt_id,b.Eqpmt_id,c.EqpmtCmpntName,c.Parent_Eqpmnt_ID
    from Vehicle a,Equipment b,Equipment_Component c
    where a.Vehicle_iD=b.Vehicle_iD
    and b.Eqpmt_id=c.Eqpmt_id
    and a.vehicle_id=&EQP_rec.vehicleId;
    BEGIN
    OPEN EQP_cursor;
         FETCH EQP_cursor INTO EQP_rec;
         dbms_output.put_line(EQP_rec.Vehicle_iD,EQP_rec.Eqpmt_Cmpnt_id||','||EQP_rec.Eqpmt_id||','||EQP_rec.EqpmtCmpntName||','||EQP_rec.Parent_Eqpmnt_ID);
    CLOSE EQP_cursor;
    END;
    END;

  • Randomly selecting some rows from the database table

    Hi can some one help me in selecting some rows from a database table which has around 90,000 rows.
    Thanks.

    One thing you might try is the "sample" clause if you have 8i which is supposed to return a random percentage of the table. Say for example, you have a sequence number on your table as the pkey. Then you might try:
    select * from <table_name> where pkey in(select pkey from <table_name> sample(10));
    This should give you a random 10 percent of the rows in the table but I tried this once and the results seemed unpredictable. For example it returned a different number of rows each time even though the number of rows in the table didn't change.
    Hope this works for you.

  • Deleting rows from very large table

    Hello,
    I need to delete rows from a large table, but not all of them, so I can't use truncate. The delete condition is based on one column, something like this:
    delete from very_large_table where col1=100;
    There's an index (valid, B-tree) on col1, but it still goes very slow. Is there any instruction which can help delete rows faster?
    Txh in adv.
    A.

    Your manager doesn't agree to your running an EXPLAIN PLAN? What is his objection? Sounds like the prototypical 'pointy-hair boss'.
    Take a look at these:
    -- do_explain.sql
    spool explain.txt
    -- do EXPLAIN PLAN on target queries with current index definitions
    truncate table plan_table
    set echo on
    explain plan for
    <insert query here>
    set echo off
    @get_explain.sql
    -- get_explain.sql
    set linesize 120
    set pagesize 70
    column operation     format a25
    column query_plan     format a35
    column options          format a15
    column object_name     format a20
    column order           format a12
    column opt           format a6
    select     lpad(' ',level) || operation "OPERATION",
         options "OPTIONS",
         decode(to_char(id),'0','COST = ' || NVL(to_char(position),'n/a'),object_name) "OBJECT NAME",
         cardinality "rows",     
         substr(optimizer,1,6) "OPT"
    from     plan_table
    start     with id = 0
    connect by prior id = parent_id
    There are probably newer, better ways, but this should work with all living versions of Oracle and is something I've had in my back pocket for several years now. It's not actually executing the query or dml in question, just running an explain plan on it.

  • How can i select some row from multiple row in the same group of data

    I want to select some row from multiple row in the same group of data.
    ColumnA        
    Column B
    1                  OK
    1                   NG
    2                   NG
    2                          NG
    3                          OK
    3                          OK
    I want the row of group of
    ColumnA if  ColumnB contain even 'NG'
    row , select only one row which  Column B = 'NG'
    the result i want = 
    ColumnA         Column B
    1                         NG
    2                   NG
    3                          OK
    Thank you

    That's some awful explanation, but I think this is what you were driving at:
    DECLARE @forumTable TABLE (a INT, b CHAR(2))
    INSERT INTO @forumTable (a, b)
    VALUES
    (1, 'OK'),(1, 'NG'),
    (2, 'NG'),(2, 'NG'),
    (3, 'OK'),(3, 'OK')
    SELECT f.a, MIN(COALESCE(f2.b,f.b)) AS b
    FROM @forumTable f
    LEFT OUTER JOIN @forumTable f2
    ON f.a = f2.a
    AND f.b <> f2.b
    GROUP BY f.a

  • How do I select a row from the middle of a recordset?

    UserID QuestionID Answered
    10 9 N
    10 8 N
    10 7 N
    10 6 N
    10 5 Y
    10 4 Y
    10 1 Y
    From the table sorted by QuestionID DESC, how do I select the first row from the bottom going up with Answered value equal to 'N'?
    Which in the example above would be:
    10 6 N
    I need to select this row and get the QuestionID value equal to 6.
    Right now I have:
    select QuestionID
    from tblMap
    where Answered = 'N'
    and ROWNUM = 1
    order by QuestionID ASC;
    This always the top most row, which would be:
    10 9 N

    Here i used DUAL to generate a list of numbers for me.
    ME_XE?select *
      2  from
      3  (
      4     select row_number() over (order by col1 desc) as rn, count(*) over() as cnt, col1
      5     from
      6     (
      7        select level as col1 from dual connect by level <= 9
      8     )
      9  )
    10  where ceil(cnt/2) = rn
    11  /
                    RN                CNT               COL1
                     5                  9                  5
    1 row selected.
    Elapsed: 00:00:00.07
    ME_XE?Edited by: Tubby on Jul 8, 2009 1:47 PM
    Seems i misread the question :)

  • How can I select random records from one column

    How can I random select 400 records from a column contains more than 500,000 records? And how long will it take in oracle? Thanks.

    here is one option: (just change 5 to suit your needs...)
    SQL>select * from (
      2  select object_name
      3  from all_objects
      4  order by dbms_random.random
      5  ) where rownum < 5
      6  /
    OBJECT_NAME
    UTL_SYS_COMPRESS
    GV_$LOG_HISTORY
    GV_$LOGMNR_LOGS
    WWV_FLOW_THEME_7
    SQL>/
    OBJECT_NAME
    WWV_FLOW_UPGRADE_REPORT
    WRI$_ADV_SQLT_STATISTICS_PK
    V_$DATABASE
    GV_$SERVICEMETRIC
    SQL>/
    OBJECT_NAME
    WWV_FLOW_CREATE_FLOW_API
    WRH$_SERVICE_WAIT_CLASS_BL
    EXU8SNAPL
    GV$SERVICEMETRIC_HISTORY
    SQL>                well, regarding how long will it take... it depends from lots of variables...
    Cheers,
    Andrea

  • How to get the latest row from my Logical Table Source?

    Hi everyone,
    I have a SALARY_HISTORY table that holds the Salary Date and the Salary Amount for an employee, as a simple example. In my Business Model, I want to have a Display Folder called "Current Salary" and use the Where Clause restriction to derive the latest salary for each of my employees.
    In standard SQL Plus, I used a nested SQL statement, e.g. "where a.SalaryDate = (select max(b.SalaryDate) from SALARY_HISTORY b where b.employeeid = a.employeeid)".
    Is there anyway in the "Where Clause" filter of the LTS to derive this type of a query?
    I have tried things like "DB".""."SCHEMA"."AL_SALARY_HISTORY"."SalaryDate" = EVALUATE('SELECT_PHYSICAL MAX(SALARY_DATE) FROM "DB".."SCHEMA"."AL_SALARY_HISTORY" WHERE employeeid = %1)', "DB".""."SCHEMA"."AL_SALARY_HISTORY"."employeeid"). And I know I can't use any of the analytic functions in a where clause.
    So how do I go about this other than creating a View in the database or creating a function in the database to give me the maximum salary date for my employee?
    Thanks
    Paul

    Hi Paul,
    You could achieve this requirement using a sub query. Briefly, the steps are
    1. Just create one report with max(salary_date) for each employee.
    2. Create another report with SALARY_DATE included.
    3. Create a filter (Based on another analysis) on this report, as employee is in(Employee of report in step1) AND SALARY_DATE IS IN(SALARY_DATE of report in step1)(this is the max_salary_date for him)
    You could notice that BI Server would send two queries to the backend for this info.
    Hope this helps.
    Thank you,
    Dhar

  • How to soft delete a row from the target table?

    Could someone help me on this requirement?
    How to implement the below logic using only ODI? I am able to implement the below logic with the "DELETE_FLAG" as "N".
    I want to make the latest record with the flag as "N" and all the previous other records with the flag as "D".
    Thanks a lot in advance.
    I have a source table "EMP".
    EMP
    EMPID FIRST_NAME
    1 A
    2 B
    First name is changed from A to C and then, C to D etc. For each data change, I would add a target row and mark the latest row as "N" and the rest as "D". The target table would contain the following data:
    Target_EMP
    EMPID FIRST_NAME DELETE_FLAG
    1 A D
    1 C D
    1 D N

    The problem is that I can't delete the row cause it demands from me to fill the mandatory field previously. It takes place when the key field is ROWID. In other cases delete is succesful.

  • How could auto-select customized row(s) when af:table loads?

    for example,
    the first row be selected at the first start up.
    or, if i change/add a row, this updated row should be highlighted
    JDev: 11g
    any good solution for this use case?
    Thanks.
    Kevin.

    Hi Frank,
    but it doesn't work...
    Thank you.
    Kevin.

  • Select Random Rows in PL/SQL

    I would like to know if anyone has a suggestion fora select statement that would select random rows from a table based on the following:
    The table contains 1-to-many categories. From each of these categories I need to select a 'x' amount of rows based upon the category id, with a total of 25 rows being selected overall. Some categories will have only 1 row selected, some will have more than 1. The rows selected contain questions that are either multiple choice (type=1) or true/false (type=2). The total rows selected (25) cannot contain more than 20% of true/false questions. Anyone have a solution for a select statement? Thanks.

    Not having a database at hand. To be treated as a template
    with
    parameters as
    (select all_rows,
            all_categories,
            :all_questions all_questions,                               -- your 25
            :category_questions category_questions,                     -- your 'x'
            ceil(all_categories / :category_questions) pick_categories,
            :type_2_percentage type_2_percentage                        -- your 20% expressed as 0.2
       from (select count(*) all_rows,
                    count(distinct category) all_categories,
               from your_table
    categories_scrambled as
    (select category,dense_rank() over (order by categories_mix) category_choice
       from (select category,
                    ceil(dbms_random.value(0,1000)) categories_mix
               from your_table
              group by category
    rows_scrambled as
    (select category,question_type,question_text,dense_rank() over (order by rows_mix) row_choice
       from (select category,question_type,question_text,
                    ceil(dbms_random.value(0,10000)) rows_mix
               from your_table
    combination as
    (select r.category,r.question_type,r.question_text,r.row_choice,c.category_choice,
            row_number() over (partition by r.row_choice,r.question_type order by r.row_choice) row_mark
       from rows_scrambled r,categories_scrambled c
      where r.category = c.category
    type_2_questions as
    (select question_text,question_type,category
       from combination
      where category_choice <= (select pick_categories from parameters)
        and row_mark <= floor(:all_questions * :type_2_percentage / (select pick_categories from parameters))
        and question_type = 2
    type_1_questions as
    (select question_text,question_type,category,row_number() over () the_count
       from combination
      where category_choice <= (select pick_categories from parameters)
        and row_mark <= ceil((:all_questions - (select count(*) from type_2_questions)) / (select pick_categories from parameters))
        and question_type = 1
    select question_text,question_type,category
      from type_2_questions
    union all
    select question_text,question_type,category
      from type_1_questions
    where the_count <= :all_questions - (select count(*) from type_2_questions)Regards
    Etbin

  • Selecting multiple rows from List-component

    Hi
    Could someone give me an example how to programmatically select multiple rows from List-component?
    I know that this selects one row: lst_example.selectedIndex = 1;
    But how about selectin indexes 1,2 and 4 for example?

    selectedIndices
    A Vector of ints representing the indices of the currently selected item or
    items...
    var si:Vector.<int> = new Vector.<int>;
    si.push(1);
    si.push(2);
    si.push(4);
    list.selectedIndices = si;

  • Deleting a row from a UIData table

    I am trying to select a row from a jsf table then call a backing bean method to delete it using this backing bean method.
    void deleterow(){
    dataitem =(item) dataTable.getrowdata();
    List newlist = new ArrayList();
    table = model.getwrappeddata();
    for (int i=0; i<table.size(); i++){
        product = (product)table.get(i);
       if (!product.name.equals(dataitem.name)){
          newlist.add(product);
    model.setwrappeddata(newlist);
    }all the getter/setter methods are set and it does seem to work.However the results are very inconsistent,sometimes rows are deleted and then magically reappear on the the table latter! Anyone knows of a fail proof method of achieving this?
    Ta.

    A decent IDE ships with a code debugger which allows you tracking variables and running code step by step. If you don't use an IDE or you're unable to use it, then just add some sysouts which prints the desired variables at strategic locations and/or moments.

  • How to select multiple rows from List Of Values

    Hello,
    I use ADF 11g to create my list of values (LOV). I want to select multiple rows from it. but i can't.
    so how i can select many rows to set them in my adf table.
    Thank in advance

    Hi,
    LOV is map to an attribute in the viewObject so it will return only one value or more values from selected row. You can't select multiple rows from LOV.
    But you can do this by using popup which you can select multiple rows and insert the selected rows to another table.
    This blog post explain how to achieve this :
    http://husaindalal.blogspot.com/2009/11/search-from-popup-and-add-to-new-table.html#comments
    Sameh Nassar

  • How to select a row from duplicate set of records?

    I want to select a row from a duplicate set of records.
    below is the explanation of my requirement.
    select * from test_dup;
    COL_BILL     COL_SERV     COL_SYS
    b1     s1     c
    b1     s1     g
    b1     s2     c
    b1     s2     g
    b2     s2     g
    b2     s3     c
    b2     s3     g
    b3     s3     c
    Here what I want is, for a distinct col_sys if col_bill and col_serv is same then I need the row where col_sys='c'
    from the above result set I need the following:
    b1     s1     c
    b1     s2     c
    b2     s3     c
    I am using the following SQL query which is giving me the correct result set. But it will hamper the performance because there are total 45 columns in the table and total volume is around 50 million.
    select * from test_dup where col_bill||col_serv in (
    select col_bill||col_serv from (
    select col_bill,col_serv,count(*) from test_dup
    where col_sys in ('c','g')
    group by col_bill,col_serv having count(*) >1)) and
    col_sys='c';
    Can anyone please provide me the optimize SQL query for the same.

    Hi,
    Another way,
    SQL> with test_dup
    as
         select 'b1' col_bill, 's1' col_serv, 'c' col_sys from dual union all
         select 'b1', 's1', 'g' from dual union all
         select 'b1', 's2', 'c' from dual union all
         select 'b1', 's2', 'g' from dual union all
         select 'b2', 's2', 'g' from dual union all
         select 'b2', 's3', 'c' from dual union all
         select 'b2', 's3', 'g' from dual union all
         select 'b3', 's3', 'c' from dual
      select col_bill, col_serv, min(col_sys) col_sys
        from test_dup
       where col_sys in ('c', 'g')
    group by col_bill, col_serv
      having count( * ) > 1
         and count(nullif(col_sys, 'g')) > 0;
    CO CO C
    b1 s1 c
    b2 s3 c
    b1 s2 c
    3 rows selected.Regards
    Peter
    Edited by: Peter on Feb 18, 2009 1:10 AM
    - Added test data, thanks Karthick

Maybe you are looking for

  • No trace file in .../udump

    Hi, DB 10.2.0.5 I enabled a session tarce by : execute sys.dbms_system.set_sql_trace_in_session(267,21842,TRUE); (that's the J011 process) and can see it in v$session: SQL> select sid, serial#, sql_trace, sql_trace_waits, sql_trace_binds from v$sessi

  • How do i recover a folder I replaced

    I did a dumb thing and copied a file to my desktop.  It said there was already a file with that name and i said replace file.  Is there any way I can recover the folder I replaced?

  • Application Express 4.1 Grouping prohibitations use

    Hello everyone, I am a student from Holland and I have to work with Apex for my school. I've been working with apex for about a month now. I think it's a great program development environment. Here's the thing I've been going a little crazy about, I

  • Firefox is "unable to connect" to any site, while other broswers can

    I've tried clearing my cookies and history, disabling addons/extensions and the firewall, changing/disabling proxy settings, and read several of the articles, but nothing seems to work. I was out of town for 2 days and everything was working fine whe

  • How do I install adobe flash for kindle fire

    how do I install adobe flash for kindle fire