Select a random sample of results from a query result

后端 未结 8 769
-上瘾入骨i
-上瘾入骨i 2020-11-30 20:14

This question asks about getting a random(ish) sample of records on SQL Server and the answer was to use TABLESAMPLE. Is there an equivalent in Oracle 10?

8条回答
  •  [愿得一人]
    2020-11-30 20:23

    This in not a perfect answer but will get much better performance.

    SELECT  *
    FROM    (
        SELECT  *
        FROM    mytable sample (0.01)
        ORDER BY
                dbms_random.value
        )
    WHERE rownum <= 1000
    

    Sample will give you a percent of your actual table, if you really wanted a 1000 rows you would need to adjust that number. More often I just need an arbitrary number of rows anyway so I don't limit my results. On my database with 2 million rows I get 2 seconds vs 60 seconds.

    select * from mytable sample (0.01)
    

提交回复
热议问题