Oracle中取前十条数据的sql语句!!!

jeff1114 2010-08-02 09:55:56
select * from (select * from 表a where sex='男' order by age) where rownum<=10
上面这条语句是可以查找出来的,但是语句执行时间有点长,比select * from 表a where sex='男' and rownum<=10慢多了
但如果加上order,那条语句就会报错,有没有高手能给出既能排序执行速度又快的sql语句啊!!!
其实我是想修改前十数据的值,想用的方法是
update 表a set note='aaa' where id in(select id from (select id from 表a where sex='男' order by age) where rownum<=10)这条语句,这个方法对吗?有好点的sql语句吗???
...全文
3590 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
taotie1225 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 minitoy 的回复:]
select * from (select * from 表 where sex = '男' order by age)
where rownum <=10
[/Quote]

正解
勇敢的心515 2010-08-05
  • 打赏
  • 举报
回复
学习 。。。。
siriusraider 2010-08-02
  • 打赏
  • 举报
回复
对AGE加索引不行么?加完索引之后再SELECT,我是初学者,不知道这样行不~
心中的彩虹 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 jeff1114 的回复:]
select * from (select * from 表a where sex='男' order by age) where rownum<=10
上面这条语句是可以查找出来的,但是语句执行时间有点长,比select * from 表a where sex='男' and rownum<=10慢多了
但如果加上order,那条语句就会报错,有没有高手能给出既能排序执行速度又快的sql语……
[/Quote]

select * from 表a where sex='男' and rownum<=10慢多了
--这个肯定是有问题了 你的直接是给每个记录加个序号

update 表a set note='aaa' where id in(select id from (select id from 表a where sex='男' order by age) where rownum<=10)
--这个是可以的

--也可以用下面的
update 表a set note='aaa' where id in
(select id from (select id,row_number() over(order by age,rowid) rn from 表a where sex='男') t where rn<=10)



minitoy 2010-08-02
  • 打赏
  • 举报
回复
select * from (select * from 表 where sex = '男' order by age)
where rownum <=10
orbcle 2010-08-02
  • 打赏
  • 举报
回复
补充
in子查询只执行了1次
exists子查询执行了
select count(1) from a;
orbcle 2010-08-02
  • 打赏
  • 举报
回复
语句可以这么写

update
(
select * from (select * from 表a where sex='男' order by age) where rownum<=10
)tmp
set tmp.note='aaa' ;

至于exists和in谁快来说,要看表的数据条数,这里我想应该是in更快,
in需要做的事
1.找出男性同胞,
2.然后在临时表空间排序,
3.取出前十条
4.排序10条数据,和父查询匹配
exists需要做的事
1.找出男性同胞,
2.然后在临时表空间排序,
3.取出前十条
4.不排序但是表a中的每条数据都要和子查询中id匹配,如果有1千万条就要匹配1千万次,效率远低于排序10条数据....
所以exists不是总比in快



jeff1114 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 phoenix_99 的回复:]
select * from 表 where sex = '男' and rownum <=10 order by age
没有发现错误
[/Quote]
是没有报错,但是查询的结果有问题啊!!!!没有按顺序来...中间漏了好多数据!!!
Phoenix_99 2010-08-02
  • 打赏
  • 举报
回复
select * from 表 where sex = '男' and rownum <=10 order by age
没有发现错误
jeff1114 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 czfxwpy 的回复:]
和4楼的有点不一样 但我想我的会快一点。
[/Quote]
我在pl/sql中运行你和4楼写的语句,都直接死在那不动了,只能强制关才能关!!怎么回事啊?
我的sql语句是:

update yuqingdongtai a set isextract='tjf' where exists (select id from (select id ,row_number() over (order by generatetime,id)rn from yuqingdongtai where ishulue=0 and isignore=0 and isextract='0' )b where rn <=20 and a.id=b.id )
huangyunzeng2008 2010-08-02
  • 打赏
  • 举报
回复
这样写会快点嘛?我不敢苟同啊!
zhuhuadeaa 2010-08-02
  • 打赏
  • 举报
回复
exists 肯定要比 in快很多。。。
czfxwpy 2010-08-02
  • 打赏
  • 举报
回复
和4楼的有点不一样 但我想我的会快一点。
czfxwpy 2010-08-02
  • 打赏
  • 举报
回复
上来发现被人写了,噢。
UPDATE table1 a SET note='aaa' 
WHERE exists (SELECT 1
FROM (SELECT id,age,row_number() over(order by age,id) rk FROM table1
WHERE sex='男') b
WHERE rk<=10 and a.id=b.id);

create table table1 (
id VARCHAR2(2),
sex VARCHAR2(2),
age number
);
alter table table1 add note VARCHAR2(3) default 'bbb'; --增加一列
SELECT * FROM table1 ;
INSERT INTO table1 VALUES('01','男',10);
INSERT INTO table1 VALUES('02','男',14);
INSERT INTO table1 VALUES('03','男',11);
INSERT INTO table1 VALUES('04','男',14);
INSERT INTO table1 VALUES('05','男',10);
INSERT INTO table1 VALUES('06','男',13);
INSERT INTO table1 VALUES('07','女',25);
INSERT INTO table1 VALUES('08','男',23);
INSERT INTO table1 VALUES('09','男',36);
INSERT INTO table1 VALUES('10','男',35);
INSERT INTO table1 VALUES('11','男',24);
INSERT INTO table1 VALUES('12','男',25);
INSERT INTO table1 VALUES('13','男',24);
INSERT INTO table1 VALUES('14','男',15);
INSERT INTO table1 VALUES('15','男',13);

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧