我从一些表中随机抽取样本,并注意到根据我编写查询的方式,它不起作用。显然,我没有使用 all_tab_columns,我只是提供了一个可以在普通实例 (9.2.0.8) 上运行的示例。
为什么会这样?
select * from (
select
floor(dbms_random.value(0,1000)) as randomval
from
all_tab_columns where rownum < 10000
) where randomval > 200 and randomval < 300;
但这不会返回任何结果。
select * from (
select
floor(dbms_random.value(0,1000)) as randomval
from
all_tab_columns
) where randomval > 200 and randomval < 300;
查询中的 rownum < 10000 有何作用?
编辑:澄清什么不起作用。
编辑:添加版本9.2.0.8
最佳答案
我没有明确的答案,但我有一个理论......
我的猜测是您的第二个查询正在对此进行优化:
select *
from all_tab_columns
where floor(dbms_random.value(0,1000))> 200
and floor(dbms_random.value(0,1000)) < 300;
并且在内联 View 中包含 rownum
标准会以某种方式阻止这种优化。
这也可以解释为什么我们中的一些人(包括我)无法看到您所描述的问题 - 因为我们使用不同版本的 Oracle,并且查询针对我们的优化方式不同。
编辑
经过一番谷歌搜索后,我发现了 this AskTom question ,这似乎相关。 Tom Kyte 的回答以这一行结束:
When calling functions from SQL, you had better not rely on how often the functions get called, in what order, or whatever. In short, assume nothing. And remember - SQL rewrites kick in and we rewrite your SQL all of the time. Don't rely on side effects
关于sql - 为什么在使用 dbms_random.value 函数时需要在此 Oracle SQL 查询中包含 rownum 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15099477/