how to make selecting random rows in oracle faster with table with millions of rows

前端 未结 7 1156
后悔当初
后悔当初 2021-02-13 11:25

Is there a way to make selecting random rows faster in oracle with a table that has million of rows. I tried to use sample(x) and dbms_random.value and its taking a long time to

7条回答
  •  眼角桃花
    2021-02-13 12:21

    Using appropriate values of sample(x) is the fastest way you can. It's block-random and row-random within blocks, so if you only want one random row:

    select dbms_rowid.rowid_relative_fno(rowid) as fileno,
           dbms_rowid.rowid_block_number(rowid) as blockno,
           dbms_rowid.rowid_row_number(rowid) as offset
      from (select rowid from [my_big_table] sample (.01))
     where rownum = 1
    

    I'm using a subpartitioned table, and I'm getting pretty good randomness even grabbing multiple rows:

    select dbms_rowid.rowid_relative_fno(rowid) as fileno,
           dbms_rowid.rowid_block_number(rowid) as blockno,
           dbms_rowid.rowid_row_number(rowid) as offset
      from (select rowid from [my_big_table] sample (.01))
     where rownum <= 5
    
        FILENO    BLOCKNO     OFFSET
    ---------- ---------- ----------
           152    2454936         11
           152    2463140         32
           152    2335208          2
           152    2429207         23
           152    2746125         28
    

    I suspect you should probably tune your SAMPLE clause to use an appropriate sample size for what you're fetching.

提交回复
热议问题