Hive 提供了類似 Oracle 的 rownum 機制,類似這樣(效率比較差):
select * from (select row_number() over (order by create_time desc) as rownum,u.* from user u) mm where mm.rownum between 10 and 15;
between 和 and 分頁可以按照下面這個公式 between :( currentPage- 1)pageSize+1 ,and : (currentPagepageSize)
還有一種辦法,如果表里有唯一標(biāo)識字段也可以借助這個字段和 limit 實現(xiàn)。比如:
獲取第一頁數(shù)據(jù):
注:同時需要記錄這 10 條中最大的 id 為 preId,作為下一頁的條件。
select * from table order by id asc limit 10;
獲取第二頁數(shù)據(jù):
注:同時保存數(shù)據(jù)中最大的 id 替換 preId。
select * from table where id >preId order by id asc limit 10;
舉例:
獲取第一頁數(shù)據(jù):
select * from table where type=2 order by id asc limit 10;
獲取第二頁數(shù)據(jù):
需要獲取第一頁10條中最大的id為preId,作為下一頁的條件。
int preId=select max(id) from table where type=2 order by id asc limit 10;
select * from table where type=2 and id >preId order by id asc limit 10;
獲取第三頁數(shù)據(jù):
需要獲取2頁20條中最大的id為preId,作為下一頁的條件。
int preId=select max(id) from table where type=2 order by id asc limit 20;
select * from table where type=2 and id >preId order by id asc limit 10;