12
返回列表 发新帖
楼主: fan_zhen_hua

[讨论] order by dbms_random.random问题

[复制链接]
论坛徽章:
47
蒙奇·D·路飞
日期:2017-03-27 08:04:23马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11一汽
日期:2013-09-01 20:46:27复活蛋
日期:2013-03-13 07:55:232013年新春福章
日期:2013-02-25 14:51:24ITPUB 11周年纪念徽章
日期:2012-10-09 18:03:322012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:202012新春纪念徽章
日期:2012-02-13 15:13:20
11#
发表于 2009-11-4 02:45 | 只看该作者
原帖由 szusunny 于 2009-11-3 03:11 发表
其实order by dbms_random.random 并不是随机用一个列排序, 而是增加一个随机列, 然后按这随机列进行排序!
等价于select i.*,dbms_random.random from all_objects i order by dbms_random.random;


I agree with you. You can run this SQL multiple times to see the result:

SQL> select table_name, dbms_random.value from user_tables order by dbms_random.value;

TABLE_NAME        VALUE
---------- ------------
T1          .0677720002
T4         .29022524836
T2         .29651656741
JUNK2      .32677403399
TESTNULL   .49501084415
T3         .55977963534
TEST       .57477114003
TESTNLS    .63702901646
EMPTY_TS   .65707118667
TESTCHAR   .77008281445
TABLE1      .8539139886
T          .95402244366

12 rows selected.

Somebody asked Tom Kyte the same question at
http://asktom.oracle.com/pls/ask ... ON_ID:6075151195522

See "Still not clear...   October 22, 2002 - 12pm Central time zone". Unfortunately, Tom didn't really answer the question.

I looked up SQL Reference. It's not clear either:

"Use the ORDER BY clause to order rows returned by the statement."
"expr [i.e. the expression following 'order by'] orders rows based on their value for expr. The expression is based on columns in the select list or columns in the tables, views, or materialized views in the FROM clause."

It may be documented somewhere, but I didn't find it. We can think of "dbms_random.value" in the "order by" clause as a psedocolumn.

We definitely should not consider it as column position. Consider this:

SQL> create or replace function f return number as begin return 1; end;
  2  /

Function created.

SQL> select table_name from user_tables order by f;

TABLE_NAME
----------
TESTCHAR
TESTNULL
TESTNLS
EMPTY_TS
TEST
T
T2
T3
T4
TABLE1
JUNK2
T1

12 rows selected.

SQL> select table_name, f from user_tables order by f;

TABLE_NAME            F
---------- ------------
TESTCHAR              1
TESTNULL              1
TESTNLS               1
EMPTY_TS              1
TEST                  1
T                     1
T2                    1
T3                    1
T4                    1
TABLE1                1
JUNK2                 1
T1                    1

12 rows selected.

You see the function f which always returns 1 does not make the table_name sort. It's NOT the same as "select table_name from user_tables order by 1".

Yong Huang

使用道具 举报

回复
招聘 : Java研发
论坛徽章:
1
2010新春纪念徽章
日期:2010-03-01 11:19:06
12#
 楼主| 发表于 2009-11-4 10:11 | 只看该作者
yong huang讲的很精辟,我从来没想过这么多。。受益非浅~~
"expr [i.e. the expression following 'order by'] orders rows based on their value for expr. The expression is based on columns in the select list or columns in the tables, views, or materialized views in the FROM clause."

order by 表达式是按表达式值来排序,此时如果算第几列没什么意义,也理解不通。所以最原始的问题,应该是以不可预测的顺序来产生一个行集。。
谢谢~

使用道具 举报

回复
论坛徽章:
8
2009新春纪念徽章
日期:2009-01-04 14:52:28祖国60周年纪念徽章
日期:2009-10-09 08:28:002010新春纪念徽章
日期:2010-03-01 11:07:24ITPUB9周年纪念徽章
日期:2010-10-08 09:32:25ITPUB十周年纪念徽章
日期:2011-11-01 16:23:262013年新春福章
日期:2013-02-25 14:51:24沸羊羊
日期:2015-03-04 14:51:522015年新春福章
日期:2015-03-06 11:57:31
13#
发表于 2009-11-6 13:25 | 只看该作者
明白了,学习。

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表