原来有一个表,都是在页面上分页处理的,现在表中的数据达到150-160万,页面显示老是有问题。
现在需要修改在存储过程中分页,请大家帮忙考虑怎么分页??我有想过把符合条件的数据全放到临时表中(在临时表加一个自增标识字段),但测试过,光这样一条语句:Select dbo.fn_IDList(CorpNumber,Mobile,@datefrom,@dateto) as ID, Min(ID) minID ,CorpNumber,Mobile,Max(NewDate) as NewDate,Sum(Times) Times ,Max(City) City  FROM TestTable   都要执行很久。相关代码:
http://www.363366.cn/temp/test.html (有代码着色显示,还有测试脚本下载)
麻烦帮忙看看....

解决方案 »

  1.   

    利用系统内部存储过程的分页  CREATE PROC sp_PageView   
    @sql         ntext,     --要执行的sql语句
    @PageCurrent int=1,     --要显示的页码
    @PageSize    int=10,    --每页的大小
    @PageCount   int OUTPUT --总页数
    AS
    SET NOCOUNT ON
    DECLARE @p1 int
    --初始化分页游标
    EXEC sp_cursoropen 
    @cursor=@p1 OUTPUT,
    @stmt=@sql,
    @scrollopt=1,
    @ccopt=1,
    @rowcount=@PageCount OUTPUT--计算总页数
    IF ISNULL(@PageSize,0)<1 
    SET @PageSize=10
    SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
    IF ISNULL(@PageCurrent,0)<1 OR ISNULL(@PageCurrent,0)>@PageCount
    SET @PageCurrent=1
    ELSE
    SET @PageCurrent=(@PageCurrent-1)*@PageSize+1--显示指定页的数据
    EXEC sp_cursorfetch @p1,16,@PageCurrent,@PageSize--关闭分页游标
    EXEC sp_cursorclose @p1
          
      

  2.   

    to kk19840210 
    老兄,你可以仔细看一下我的情况,你这个对我的情况可能没用,不过我会试试。谢谢。
      

  3.   

    like '%太多了,索引都不起作用,不慢才奇怪,建议用全文索引
      

  4.   

    to gingerkang :
    like字段的索引是没有用,但我认为更重要的是时间及性能都花在dbo.fn_IDList和分组上了。不知道有没有更好的办法
      

  5.   

    索引没用肯定慢,感觉你应该在NewDate列上键聚集,排序太费时间了
      

  6.   

    还有,我这个问题不知道怎么分页????
    Select dbo.fn_IDList(CorpNumber,Mobile,@datefrom,@dateto) as ID,  Min(ID) minID,CorpNumber,Mobile,Max(NewDate) as NewDate,Sum(Times) Times,Max(City) City FROM TestTable where ......
    如果这样选择数据到一个临时表,然后再按每页条数,当前页进行分页的话,这一步就已经很耗时了
      

  7.   

    利用 颠倒法 的高效分页存储过程exec sp_fenye '表名','需要返回的列','排序的字段名','排序方式','查询条件','页尺寸=10','页码=4'
    --注意,要修改存储过程里面主键的字段名SET QUOTED_IDENTIFIER ON 
    GO
    SET ANSI_NULLS ON 
    GO
    create     PROCEDURE sp_fenye
        @tblName varchar(255), -- 表名 
        @strGetFields varchar(1000) = '*', -- 需要返回的列 
        @fldName varchar(255)='', -- 排序的字段名 
        @OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 
        @strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)     @PageSize int = 60, -- 页尺寸 
        @PageIndex int = 1 -- 页码 AS 
        --declare @PageSize int
        SET NOCOUNT ON
        declare @strSQL varchar(5000) -- 主语句 
        declare @strOrder1 varchar(400) -- 排序类型 
        declare @strOrder2 varchar(400) -- 排序类型 
        declare @tmp_filed varchar(400) -- 排序类型 
        declare @mainid varchar(20) -- 排序类型     set @mainid='id' --主键的字段名    if @fldName<>@mainid
            set @tmp_filed=' '+@mainid+','+@fldName
        else
            set @tmp_filed=' '+@fldName
        
        if @OrderType <> 0 --如果@OrderType不是0,就执行降序,这句很重要! 
            begin
                if @fldName<>@mainid
                    begin
                        set @strOrder1 = ' order by ' + @fldName +' desc,'+@mainid+' desc' 
                        set @strOrder2 = ' order by ' + @fldName +' ,'+@mainid+' desc' 
                    end
                else
                    begin
                        set @strOrder1 = ' order by ' + @fldName +' desc' 
                        set @strOrder2 = ' order by ' + @fldName +' asc' 
                    end
            end    else 
            begin
                if @fldName<>@mainid
                    begin
                        set @strOrder1 = ' order by ' + @fldName +' asc,'+@mainid+' desc' 
                        set @strOrder2 = ' order by ' + @fldName +' desc,'+@mainid+' desc' 
                    end
                else
                    begin
                        set @strOrder1 = ' order by ' + @fldName +' asc' 
                        set @strOrder2 = ' order by ' + @fldName +' desc' 
                    end
            end
        if @PageIndex = 1 
            begin 
                if @strWhere <> '' 
                    set @strSQL = 'select top ' + ltrim(str(@PageSize)) +' '+@strGetFields+ ' from ' + @tblName + ' with(nolock) where ' + @strWhere + @strOrder1 
                else 
                    set @strSQL = 'select top ' + ltrim(str(@PageSize)) +' '+@strGetFields+ ' from '+ @tblName + ' with(nolock)  '+ @strOrder1 
                    --如果是第一页就执行以上代码,这样会加快执行速度 
            end 
        else 
            begin 
                --以下代码赋予了@strSQL以真正执行的SQL代码 
                if @strWhere = '' 
                    set @strsql='select '+ @strGetFields + ' from ' + @tblName +' with(nolock) where '+@mainid+' in'
                        +'(select top '+ ltrim(str(@PageSize)) + ' '+@mainid+' from '
                        +'(select top '+ ltrim(str(@PageIndex*@PageSize))+@tmp_filed+' from '
                        +@tblName+' with(nolock) '+@strOrder1+') as aaa '+@strOrder2+')'
                        +@strOrder1            else
                    set @strsql='select '+ @strGetFields + ' from ' + @tblName +' with(nolock) where '+@mainid+' in'
                        +'(select top '+ ltrim(str(@PageSize)) + ' '+@mainid+' from '
                        +'(select top '+ ltrim(str(@PageIndex*@PageSize))+@tmp_filed+' from '
                        +@tblName+' with(nolock) where '+@strWhere+@strOrder1+') as aaa '+@strOrder2+')'
                        +@strOrder1        end 
        exec (@strSQL)
        --select @strSQL as aaa
    GO
    SET QUOTED_IDENTIFIER OFF 
    GO
    SET ANSI_NULLS ON 
      

  8.   

    楼主去掉函数,下面写一种算法:条件楼主自己定义:
    /*if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[TestTable]
    GO*/CREATE TABLE [dbo].[TestTable] (
        [ID] [int] IDENTITY (1, 1) NOT NULL ,    --自增标识
        [CorpNumber] [varchar] (10) NOT NULL ,   --公司编号
        [Mobile] [nvarchar] (20) NOT NULL ,      --手机
        [NewDate] [smalldatetime] NOT NULL ,     --最新访问时间
        [Times] [smallint] NOT NULL ,            --访问次数
        [City] [nvarchar] (200) NULL             --手机所在城市
    ) ON [PRIMARY]
    GOinsert  TestTable select '公司1','中国移动',getdate(),10,'X1'
    insert  TestTable select '公司1','中国移动',getdate(),20,'X1'
    insert  TestTable select '公司1','中国移动',getdate(),30,'X1'
    insert  TestTable select '公司1','中国移动',getdate(),40,'X1'
    insert  TestTable select '公司5','中国联通',getdate(),50,'X1'goif not object_id('Tempdb..#1') is null
    drop table #1
    select * into #1 from TestTable where CorpNumber='公司1' and Mobile='中国移动'if not object_id('Tempdb..#2') is null
    drop table #2
    select ID=cast(null as nvarchar(200)),minID=min(ID),CorpNumber,Mobile,NewDate=Max(NewDate),City=Max(City)
    into #2
    from #1
    group by CorpNumber,Mobilewhile exists(select 1 from #1)
    begin
    update a
    set ID=isnull(a.ID+',','')+rtrim(b.ID)
    from #2 a ,#1 b 
    where
    not exists(select 1 from #1 where ID>b.ID)

    delete b
    from 
    #1 b
    where
    not exists(select 1 from #1 where ID>b.ID)
    endselect * from #2
    /*
    4,3,2,1 1 公司1 中国移动 2007-12-15 09:55:00 X1
    */
      

  9.   

    to arrow_gx :
      我以前也写过这样的分页,跟网的的差不多,这样通用的分页方法对我这个没有用to roy_88 :
      我试过,对了加上CorpNumber和Mobile参数,如果同一个CorpNumber同一个Mobile的记录条数超过400-500条时,那个while块运行很慢,至少达半分钟。如果查询没带上条件,那更是没法运行下去了。
      你这个比我的好一点点,但还是没达到要求。
    对你们的帮助表示感谢!
      

  10.   

    我也有一个
    CREATE PROC sp_PageView   
    @sql         ntext,     --要执行的sql语句
    @PageCurrent int=1,     --要显示的页码
    @PageSize    int=10,    --每页的大小
    @PageCount   int OUTPUT --总页数
    AS
    SET NOCOUNT ON
    DECLARE @p1 int
    --初始化分页游标
    EXEC sp_cursoropen 
        @cursor=@p1 OUTPUT,
        @stmt=@sql,
        @scrollopt=1,
        @ccopt=1,
        @rowcount=@PageCount OUTPUT--计算总页数
    IF ISNULL(@PageSize,0)<1 
        SET @PageSize=10
    SET @PageCount=(@PageCount+@PageSize-1)/@PageSize
    IF ISNULL(@PageCurrent,0)<1 OR ISNULL(@PageCurrent,0)>@PageCount
        SET @PageCurrent=1
    ELSE
        SET @PageCurrent=(@PageCurrent-1)*@PageSize+1--显示指定页的数据
    EXEC sp_cursorfetch @p1,16,@PageCurrent,@PageSize--关闭分页游标
    EXEC sp_cursorclose @p1