存储过程 分页【NOT IN】和【>】效率大PK 千万级别数据测试结果
use TT
go
if exists (select * from sysobjects where name='Tonge')
drop table Tonge
create table Tonge(
ID int primary key identity(1,1),
NAME varchar(100),
PWD varchar(100),
PHONE varchar(100),
BORNDATE date default getDate()
)
go
go
if exists (select * from sysobjects where name='Tonge')
drop table Tonge
create table Tonge(
ID int primary key identity(1,1),
NAME varchar(100),
PWD varchar(100),
PHONE varchar(100),
BORNDATE date default getDate()
)
go
--存储过程插入10万条数据耗时:38s//平均 2600条/s
declare @i int
set @i=0
while(@i<=100000) --插入测试数据10万条
begin
insert into Tonge values('Tonge'+Convert(varchar(2),@i),'Tonge594200'+Convert(varchar(2),@i),'16886888688',default)
set @i = @i+1
end
go
--疯狂插入法插万条插入640万条数据耗时:101s //平均 63000条/s
insert into Tonge(NAME,PWD,PHONE,BORNDATE)( select NAME,PWD,PHONE,BORNDATE from Tonge ) --疯狂插入法7次 10*2*2*2*2*2*2*2=1284万条数据
go
select MAX(ID) FROM Tonge --统计当前条数,已达到千万级别
--DBCC SHRINKFILE(TTDB_log,0) --清除事务日志
--使用 Not In 分页
if exists(select * from sysobjects where name='usp_NotIn_PagedTonge')
drop procedure usp_NotIn_PagedTonge
go
create procedure usp_NotIn_PagedTonge
@PageSize int, --每页显示的条数
@PageIndex int, --当前页数
@TotalPage int output --总共页数
AS
declare @TotalCount int --总条数
select @TotalCount=COUNT(ID) from Tonge
if(@TotalCount%@PageSize=0) --总共的页数
set @TotalPage = @TotalCount/@PageSize
else
set @TotalPage = @TotalCount/@PageSize+1
if(@PageIndex=1)
select top(@PageSize) ID,NAME,PHONE,BORNDATE from Tonge
else
select top(@PageSize) ID,NAME,PHONE,BORNDATE from Tonge
where ID not in(select top(@PageSize*(@PageIndex-1)) ID from Tonge)
order by ID
GO
if exists(select * from sysobjects where name='usp_NotIn_PagedTonge')
drop procedure usp_NotIn_PagedTonge
go
create procedure usp_NotIn_PagedTonge
@PageSize int, --每页显示的条数
@PageIndex int, --当前页数
@TotalPage int output --总共页数
AS
declare @TotalCount int --总条数
select @TotalCount=COUNT(ID) from Tonge
if(@TotalCount%@PageSize=0) --总共的页数
set @TotalPage = @TotalCount/@PageSize
else
set @TotalPage = @TotalCount/@PageSize+1
if(@PageIndex=1)
select top(@PageSize) ID,NAME,PHONE,BORNDATE from Tonge
else
select top(@PageSize) ID,NAME,PHONE,BORNDATE from Tonge
where ID not in(select top(@PageSize*(@PageIndex-1)) ID from Tonge)
order by ID
GO
--使用 大于号(>) 分页
if exists(select * from sysobjects where name='usp_Sign_PagedTonge')
drop procedure usp_Sign_PagedTonge
go
create procedure usp_Sign_PagedTonge
@PageSize int,
@PageIndex int,
@TotalPage int output
as
declare @TotalCount int
select @TotalCount=COUNT(id) from Tonge
if(@TotalPage%@PageSize=0)
set @TotalPage=@TotalCount/@PageSize
else
set @TotalPage=@TotalCount/@PageSize +1
if(@PageIndex=1)
select top(@PageSize) ID,NAME,PHONE,BORNDATE from Tonge
else
if exists(select * from sysobjects where name='usp_Sign_PagedTonge')
drop procedure usp_Sign_PagedTonge
go
create procedure usp_Sign_PagedTonge
@PageSize int,
@PageIndex int,
@TotalPage int output
as
declare @TotalCount int
select @TotalCount=COUNT(id) from Tonge
if(@TotalPage%@PageSize=0)
set @TotalPage=@TotalCount/@PageSize
else
set @TotalPage=@TotalCount/@PageSize +1
if(@PageIndex=1)
select top(@PageSize) ID,NAME,PHONE,BORNDATE from Tonge
else
--在这里查询的时候用列名代替*号,这样可以提高查询效率减少没有必要的检索
select top(@PageSize) ID,NAME,PHONE,BORNDATE from Tonge where id >
(select max(id) from
(select top (@PageSize*(@PageIndex-1)) id from Tonge order by id) emp)
go
select top(@PageSize) ID,NAME,PHONE,BORNDATE from Tonge where id >
(select max(id) from
(select top (@PageSize*(@PageIndex-1)) id from Tonge order by id) emp)
go
--NotIn分页测试
declare @PageSize int,@PageIndex int,@TotalPage int
select @PageSize=1000,@PageIndex=1000
--exec usp_NotIn_PagedTonge @PageSize,@PageIndex,@TotalPage output
print '当前页数'+convert(varchar(5),@PageIndex)+' 总页数:' + Convert(varchar(5),@TotalPage)
declare @PageSize int,@PageIndex int,@TotalPage int
select @PageSize=1000,@PageIndex=1000
--exec usp_NotIn_PagedTonge @PageSize,@PageIndex,@TotalPage output
print '当前页数'+convert(varchar(5),@PageIndex)+' 总页数:' + Convert(varchar(5),@TotalPage)
--大于号分页测试
declare @PageSize int,@PageIndex int,@TotalPage int
select @PageSize=1000,@PageIndex=1000 --每页显示1万条数据,显示第10页
--exec usp_Sign_PagedTonge @PageSize,@PageIndex,@TotalPage output
print '当前页数'+convert(varchar(5),@PageIndex)+' 总页数:' + Convert(varchar(5),@TotalPage)
declare @PageSize int,@PageIndex int,@TotalPage int
select @PageSize=1000,@PageIndex=1000 --每页显示1万条数据,显示第10页
--exec usp_Sign_PagedTonge @PageSize,@PageIndex,@TotalPage output
print '当前页数'+convert(varchar(5),@PageIndex)+' 总页数:' + Convert(varchar(5),@TotalPage)
总结:使用存储过程分页,不管是【>】还是【Not IN】查询1万条数据,且在页数相同的情况下,显示相同条数的数据。耗时都在1s左右,简直是不分上下,于是我决定再用普通分页的方式在对千万级别的数据进行测试。