求高效分页语句

MattHgh 2010-06-16 03:08:32

现在写的分页语句如下
查询 第1页 1-15 个记录


select * from
( select A.*,rownum r
from (select * from table_data where active='Y' order by s_time desc)
A where rownum <= 15
) where r >0


感觉效率很低下,帮我修改一下
提高效率
谢谢
...全文
127 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingjin520 2010-06-18
  • 打赏
  • 举报
回复
select a.*, row_number() over (order by id) rn from a a where rn <= 15;

没有测试过
feiyu8607 2010-06-18
  • 打赏
  • 举报
回复
实际在oracle中执行了一下,效率都差不多。。。
zhangwonderful 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tangren 的回复:]

引用楼主 matthgh 的回复:
现在写的分页语句如下
查询 第1页 1-15 个记录

SQL code

select * from
( select A.*,rownum r
from (select * from table_data where active='Y' order by s_time desc)
A where rownum ……
[/Quote]
支持
tangren 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 matthgh 的回复:]
现在写的分页语句如下
查询 第1页 1-15 个记录

SQL code

select * from
( select A.*,rownum r
from (select * from table_data where active='Y' order by s_time desc)
A where rownum <= 15
) wher……
[/Quote]

这个应该是常用的正确的分页写法了,不过你可以试试分析函数
SELECT *
FROM (SELECT t.*, row_number(ORDER BY s_time DESC) r FROM table_data t WHERE active = 'Y') A
WHERE r BETWEEN 1 AND 15;
dong308 2010-06-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 matthgh 的回复:]
现在写的分页语句如下
查询 第1页 1-15 个记录


SQL code

select * from
( select A.*,rownum r
from (select * from table_data where active='Y' order by s_time desc)
A where rownum <= 15
……
[/Quote]


再加索引,就OK了
心中的彩虹 2010-06-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 matthgh 的回复:]
现在写的分页语句如下
查询 第1页 1-15 个记录


SQL code

select * from
( select A.*,rownum r
from (select * from table_data where active='Y' order by s_time desc)
A where rownum <= 15
……
[/Quote]

--嵌套太多了
select a.* ,rownum rn
from table_data a
where active='Y' and rownum <= 15
order by s_time desc
--上面就可以了 就是1到15的 默认是1开始的

select * from
(select a.* ,rownum rn
from table_data a
where active='Y' and rownum <= 15
order by s_time desc) t
where rn>=n and rn<=m
--随你哪条到哪条










lzbbob1985 2010-06-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 matthgh 的回复:]
引用 1 楼 luoyoumou 的回复:
-- 为什么外层还要嵌套呢?

-- 这样不就得了?


SQL code

select A.*, rownum r
from (select * from table_data where active='Y' order by s_time desc) A
where rownum <= 15;


这样的确提高了一些,但……
[/Quote]

呵呵 在表上增加个字段 用序列给弄上 然后给加个索引 可能很快
MattHgh 2010-06-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luoyoumou 的回复:]
-- 为什么外层还要嵌套呢?

-- 这样不就得了?


SQL code

select A.*, rownum r
from (select * from table_data where active='Y' order by s_time desc) A
where rownum <= 15;
[/Quote]

这样的确提高了一些,但是还是不够快
luoyoumou 2010-06-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luoyoumou 的回复:]
-- 在 s_time、active 字段加个索引

CREATE INDEX on table_date(active,s_time);
[/Quote]

CREATE INDEX tb_ix1 on table_date(active,s_time);
luoyoumou 2010-06-16
  • 打赏
  • 举报
回复
-- 在 s_time、active 字段加个索引

CREATE INDEX on table_date(active,s_time);
luoyoumou 2010-06-16
  • 打赏
  • 举报
回复
-- 为什么外层还要嵌套呢?

-- 这样不就得了?


select A.*, rownum r
from (select * from table_data where active='Y' order by s_time desc) A
where rownum <= 15;

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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