Oracle SQL实现分页查询

Oracle SQL分页查询

1、表结构


  
  
  1. SQL> desc test;
  2. 名称 是否为空? 类型
  3. --------------------- -------- ---------------
  4. ID NOT NULL NUMBER(8)
  5. USERNAME VARCHAR2(32)
  6. PASSWORD VARCHAR2(32)
  7. AGE NUMBER(3)
  8. BIRTHDAY DATE
  9. ADDRESS VARCHAR2(40)

2、表记录条数


  
  
  1. SQL> select count (*) from test;
  2. COUNT(*)
  3. ----------
  4. 5000001

3、表中部分数据


  
  
  1. SQL> select * from test where rownum < 10;
  2. ID USERNAME PASSWORD AGE BIRTHDAY ADDRESS
  3. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4. 325610 user325610 password325610 10 15 - 10 - 15 The_Earth
  5. 325611 user325611 password325611 11 15 - 10 - 15 The_Earth
  6. 325612 user325612 password325612 12 15 - 10 - 15 The_Earth
  7. 325613 user325613 password325613 13 15 - 10 - 15 The_Earth
  8. 325614 user325614 password325614 14 15 - 10 - 15 The_Earth
  9. 325615 user325615 password325615 15 15 - 10 - 15 The_Earth
  10. 325616 user325616 password325616 16 15 - 10 - 15 The_Earth
  11. 325617 user325617 password325617 17 15 - 10 - 15 The_Earth
  12. 325618 user325618 password325618 18 15 - 10 - 15 The_Earth

4、进行分页

1、常用方式

这种方式比较好理解,而且内层查询效率高,整体查询效率较稳定。


  
  
  1. SQL> select *
  2. 2 from ( select row_.*, rownum rownum_
  3. 3 from ( select *
  4. 4 from test
  5. 5 order by id asc) row_
  6. 6 where rownum <= 20)
  7. 7 where rownum_ >= 10;

或者


  
  
  1. SQL> select *
  2. 2 from ( select test.*, rownum rownum_ from test where rownum <= 5000000 order by id asc)
  3. 3 where rownum_ >= 4999990;

查询结果


  
  
  1. ID USERNAME PASSWORD AGE BIRTHDAY ADDRESS
  2. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3. 4999989 user4999989 password4999989 69 15 - 10月 - 15 The_Earth
  4. 4999990 user4999990 password4999990 70 15 - 10月 - 15 The_Earth
  5. 4999991 user4999991 password4999991 71 15 - 10月 - 15 The_Earth
  6. 4999992 user4999992 password4999992 72 15 - 10月 - 15 The_Earth
  7. 4999993 user4999993 password4999993 73 15 - 10月 - 15 The_Earth
  8. 4999994 user4999994 password4999994 74 15 - 10月 - 15 The_Earth
  9. 4999995 user4999995 password4999995 75 15 - 10月 - 15 The_Earth
  10. 4999996 user4999996 password4999996 76 15 - 10月 - 15 The_Earth
  11. 4999997 user4999997 password4999997 77 15 - 10月 - 15 The_Earth
  12. 4999998 user4999998 password4999998 78 15 - 10月 - 15 The_Earth
  13. 4999999 user4999999 password4999999 79 15 - 10月 - 15 The_Earth
  14. 已选择11行。
  • 1

2、第二种方式

采用这种方式,查询越靠后,效率越低,整体需要查询两次,而且逐条比较,不推荐使用。


  
  
  1. SQL> select *
  2. 2 from test
  3. 3 where id not in
  4. 4 ( select id from test
  5. 5 where rownum <= 20)
  6. 6 and rownum <= 10 order by id asc;

3、第三种方式

这种是采用minus函数取差集的方式获取,同样查询越靠后,效率越低


  
  
  1. SQL> select *
  2. 2 from test
  3. 3 where rownum <= 20
  4. 4 minus
  5. 5 select * from test where rownum <= 10;

5、写成函数方便调用

待续……

总结:

1、内查询的高效利用

2、rownum的使用

猜你喜欢

转载自blog.csdn.net/qq_36167988/article/details/84107154