萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MySQL大數據量時提高分頁效率

MySQL大數據量時提高分頁效率

  我的這段代碼是大數據量時提高分頁的效率的測試代碼

  --提高分頁效率:實現分頁時只讀取顯示數據,需要先在數據庫創建數據庫“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

copyright © 萬盛學電腦網 all rights reserved