分頁查詢十萬條以上資料的sql語句

2021-08-15 10:16:25 字數 1383 閱讀 4409

普通的select語句當查詢超過10萬條語句列表時速度會嚴重影響,下面是公司dba寫出的優化版的sql語句

select t.poi_id,

t.poi_name,

t.x_axis,

t.y_axis,

t.poi_state,

t.poi_desc,

t.poi_address,

t.thumbnail,

t.area,

t.city,

t.poi_type,

t.sales_volume,

t.ascription

from t_poi_info t

inner join ( select s.poi_id from t_poi_info s where s.delflag = '0' and s.poi_type = # and s.poi_state = # order by poi_id desc limit #,#) f using (poi_id)

where t.delflag='0'

and t.poi_type =#

and t.poi_state =#

and t.poi_name like concat('%',#,'%')

and t.area like concat('%',#,'%')

and t.city like concat('%',#,'%')

and t.ascription =#

order by t.poi_id desc

但是進行分頁是,需要查詢下總條數:

select count(t.poi_id)

from t_poi_info t

where t.delflag='0'

and t.poi_type =#

and t.poi_state =#

and t.poi_name like concat('%',#,'%')

and t.area like concat('%',#,'%')

and t.city like concat('%',#,'%')

and t.ascription =#

如果直接執行這條sql也會出現超過一分鐘的查詢時間。

目前想到的處理辦法供大家借鑑,可以說不是很準確。

從業務邏輯上講,人們翻頁時一般及其有耐心的人也不會翻到100頁,因此,我可以先查詢第100頁的資料

如果有資料,說明資料量特別大,我們就不再調取查詢總計量的sql語句,預設給乙個上限頁數100頁。就節省了查總條數的查詢時間。

如果沒有資料,說明資料量不大,那我們就正常執行查詢總條數的sql語句。正常分頁處理

SQL高效分頁(百萬條資料)

select top 頁大小 from select row number over order by id as rownumber,from table1 as a where rownumber 頁大小 頁數 1 註解 首先利用row number 為table1表的每一行新增乙個行號,給行號...

百萬條資料如何進行分頁查詢

今天面試被問到一張表 有500w條資料,如何進行分頁查詢,瞬間不知道怎麼回答,平時工作中沒接觸到這麼大的資料量。所以回家自己去實驗一下 建立一張user表 create table user id bigint 20 not null auto increment,username varchar ...

SQL基礎 查詢資料 分頁查詢

使用select查詢時,如果結果集資料量很大,可以使用分頁顯示,每次顯示指定條數 語法limit offset 首先要確定每頁需要顯示的結果數量pagesize,然後根據當前頁的索引pageindex 從1開始 確定limit和offset應該設定的值。limit總是設定為pagesize offs...