Home  >  Article  >  Database  >  MySQL提高分页效率_MySQL

MySQL提高分页效率_MySQL

WBOY
WBOYOriginal
2016-05-30 17:10:29962browse

下面就是大数据量时提高分页的效率的测试代码,分享给大家。

--提高分页效率:实现分页时只读取显示数据,需要先在数据库创建数据库“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

以上就是本文的的全部内容,希望对大家的学习有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn