翻頁在各個(gè)數(shù)據(jù)庫中一般都有各自的特性參數(shù)完成,oracle用range(rownum+alias),mssql用top,而mysql使用了limit。
limit參數(shù)在數(shù)據(jù)小時(shí)效率非常高,隨著數(shù)據(jù)量的增大,速度呈線性下降,這時(shí)候就可以顯示出range的優(yōu)勢(shì)了。
比如有張表有35w條數(shù)據(jù),不算大,一次query需要不少的內(nèi)存,如果要分批/翻頁查詢的話,如select * from car order by id limit offset, nums+offset,第一次查詢10w條,則第二次只可以查詢5w條,第三次3w條,……而且到了后面數(shù)據(jù)還會(huì)出現(xiàn)重復(fù)現(xiàn)象。
select * from car order by id limit 0, 100000;
select * from car order by id limit 100001, 150000;
select * from car order by id limit 150001, 180000;
……
集合limit和range可以很好的解決這個(gè)問題:
select * from car order by id asc limit 0, 100000;
# query {max id} from this list
……
select * from car order by id desc limit 0, 100000;
# query {min id} from this list
……
select * from car where id > {max id} and id < {min id} order by id;
總共耗時(shí)20s左右!
posted on 2007-09-01 02:14
Xu Jianxiang 閱讀(4267)
評(píng)論(1) 編輯 收藏 所屬分類:
Other Tech