首頁  >  文章  >  資料庫  >  MySQL如何提高資料分頁效率

MySQL如何提高資料分頁效率

巴扎黑
巴扎黑原創
2017-03-19 10:54:171187瀏覽

[導讀] 我的這段程式碼是大數據量時提高分頁的效率的測試程式碼--提高分頁效率:實作分頁時只讀取顯示資料,需要先在資料庫建立資料庫「TestForPaging」use TestForPaginggo- -建立表格SomeDatacreate table SomeData(id int

我的這段程式碼是大數據量時提高分頁的效率的測試程式碼
--提高分頁效率:實現分頁時只讀取顯示資料,需要先在資料庫建立資料庫「TestForPaging」
use TestForPaging
go
--建立表格SomeData
create table SomeData
(
id ​​int primary key,
name varchar( 30) null,
description text
)
go
--插入資料
insert into SomeData values(1,'num1','第1條')
go
insert into SomeData values(2,'num2','第2條')
go
insert into SomeData values(3,'num3','第3條')
go
insert into SomeData values(4,'num4','第4條')
go
insert into SomeData values(5,'num5','第5條')
go
--數據條目總數
select count(*) from SomeData
go
#--為每筆記錄新增一個資料等級
select name,description,ROW_NUMBER() over(order by id desc)as dataLevel from SomeData
go

--查看指定的資料層級間的資料條目
select dataLevel,name,description from
(select name,description,row_number() over(order by id desc )as dataLevel from SomeData)
 as datawithleverl where dataLevel between 2 and 4
go
--實作檢視指定的資料層級間的資料條目的預存程序
create procedure GetDataPaged##(
@startRowIndex int,
@maximumRows int,
@sort varchar
)
AS
--確保指定sort
if len(@sort)=0
set @sort='id'
--帶參數的查詢
select dataLevel,name,description from
(select name,description,row_number() over(order by @sort desc)as dataLevel from SomeData) AS datawithleverl
WHERE dataLevel > (@startRowIndex*10) AND dataLevel <= (@startRowIndex*10 + @maximumRows)
go

以上是MySQL如何提高資料分頁效率的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn