当前位置: 代码迷 >> SQL >> 解决几百万条以下数据分页让人蛋疼的 SQL2005, SQL2008最后一页卡死,最后一页查询超时的源码
  详细解决方案

解决几百万条以下数据分页让人蛋疼的 SQL2005, SQL2008最后一页卡死,最后一页查询超时的源码

热度:121   发布时间:2016-05-05 13:42:29.0
解决几百万条以上数据分页让人蛋疼的 SQL2005, SQL2008最后一页卡死,最后一页查询超时的源码

应该很多人也遇到过这个问题,大概在2年前我也遇到过标题中的问题,当时研究了几天很是纠结没能彻底解决问题,后来也找了很多方法没能解决问题。最近又遇到这个问题,实在是不解决也不行了,冷静的想了想,完善了一下分页查询的方法,现在把代码贴上,给大家参考,若有什么漏洞,请及时联系吉日嘎拉,有错我会积极修正。希望不要重复浪费生命,直接拿过去用就可以了,在通用权限管理系统组件里也用了这个方法在进行分页。

???最近维护一个每天有10万多IP访问的网站,也是用了这个分页存储过程,分页效率还可以,最后一页没在出现卡死状态,若有问题及时联系作者QQ:252056973,欢迎大家交流分享。当系统有少数几个用户实用时问题也不严重,但是系统每时每刻都有很多人访问时那就闹心了,很容易产生网站效率极低的,访问量严重下降的趋势。

?? 最后一页分页一卡死,整个网站的性能都会非常明显的下降,不知道为啥,微软有这个BUG一直没处理好。希望SQL2012里不要有这个问题就好了。

?? 参考代码如下:

??

--?=============================================
--?Author:????????吉日嘎拉
--?Create?date:?2012年02月23日
--?Description:????2012年02月23日编码规范化
--?=============================================
ALTER?PROCEDURE?[dbo].[GetRecordByPage]?
[email protected](4000),???????????--?表名
[email protected](4000),???????????--?要显示的字段名(不要加select)
[email protected](4000),???????????--?查询条件(注意:?不要加?where)
[email protected](255),????????????--?排序索引字段名
[email protected]=?20,????????????????--?页大小
[email protected]=?1,?????????????????--?页码
[email protected],??????????????--?返回记录总数
[email protected](5)?=?'DESC'??????--?设置排序类型,?非?0?值则降序
AS
BEGIN

[email protected](8000)??????--?主语句
[email protected]??????--?获取前几条记录
[email protected]?????--?总共会是几页
[email protected]????--?获取多少条记录
[email protected](4000)?????--?用于查询记录总数的语句
[email protected](400)??????????--?排序类型
[email protected](4000)??????????--?临时变量

[email protected]=?LTRIM(RTRIM(@SortExpression))
[email protected]=?UPPER(LTRIM(RTRIM(@SortDire)))
????
????--?这里是计算整体记录行数
[email protected]
????BEGIN
[email protected]!=?''
????????BEGIN
[email protected]=?'[email protected]=COUNT(1)?FROM?'[email protected]+?'?WHERE?'[email protected]
????????END
????????ELSE
????????BEGIN
[email protected]=?'[email protected]=COUNT(1)?FROM?'[email protected]
????????END
????END

[email protected]=@@ROWCOUNT
[email protected],?N'@RecordCount?INT?OUT',[email protected]out

[email protected]
????BEGIN
[email protected]=?0
????END
????
????--?这里是控制页数最多少
[email protected][email protected][email protected]+?1
????
????--?这里检查当前页的有效性
????IF?(@PageIndex?<?1)
????BEGIN
[email protected]=?1
????END
????
????--?这里限制最后一页的有效性
????IF?(@PageIndex?>[email protected])
????BEGIN
[email protected][email protected]
????END

[email protected]!=?'ASC'
????BEGIN
[email protected]=?'<(SELECT?MIN'
[email protected]=?'?ORDER?BY?'[email protected]+?'?DESC'
????END
????ELSE
????BEGIN
????????set[email protected]=?'>(SELECT?MAX'
????????set[email protected]=?'?ORDER?BY?'[email protected]+?'?ASC'
????END
????
????--?这里是调试信息
[email protected]

????--?获取几条数据??吉日嘎拉?2010-11-02?更新
[email protected][email protected]@PageSize?*?(@PageIndex?-?1)
[email protected]>[email protected]
????BEGIN
[email protected][email protected]
????END

[email protected][email protected]*?(@PageIndex?-?1)
[email protected]>[email protected]
????BEGIN
[email protected][email protected]
????END

[email protected]=?'SELECT?TOP?'?+?STR(@TopN)?+?'?'[email protected]+?'?FROM?'
[email protected]+?'?WHERE?'[email protected][email protected]+?'('
????????+?RIGHT(@SortExpression,?LEN(@SortExpression)?-?CHARINDEX('.',[email protected]))?+?')?FROM?(SELECT?TOP?'?+?STR(@TopLimit)
????????+?'?'[email protected]+?'?FROM?'[email protected][email protected]+?')?AS?TableTemp)'
[email protected]

[email protected]!=?''
[email protected]=?'SELECT?TOP?'?+?STR(@TopN)?+?'?'[email protected]+?'?FROM?'
[email protected]+?'?WHERE?'[email protected][email protected]+?'('
????????????+?RIGHT(@SortExpression,?LEN(@SortExpression)?-?CHARINDEX('.',@SortExpression))?+?')?FROM?(SELECT?TOP?'?+?STR(@TopLimit)
????????????+?'?'[email protected]+?'?FROM?'[email protected]+?'?WHERE?'[email protected]+?'?'
[email protected]+?')?AS?TableTemp)?AND?'[email protected]+?'?'[email protected]

[email protected]=?1
????BEGIN
????????--?第一页的显示效率提高
[email protected]=?''
[email protected]!=?''
[email protected]=?'?WHERE?'[email protected]

[email protected]=?'SELECT?TOP?'?+?STR(@TopN)?+?'?'[email protected]
??????????????????????????+?'?FROM?'[email protected][email protected]+?'?'[email protected]
????END
????ELSE
????BEGIN????
????????--?解决大数据最有一页卡死的问题
[email protected][email protected]
????????BEGIN
[email protected]=?'ASC'
????????????BEGIN
[email protected]=?'?ORDER?BY?'[email protected]+?'?DESC'
????????????END
????????????ELSE
????????????BEGIN
[email protected]=?'?ORDER?BY?'[email protected]+?'?ASC'
????????????END
????????
[email protected]=?''
[email protected]!=?''
[email protected]=?'?WHERE?'?+?@WhereConditional
????????????????
????????????SET?@commandText?=?'SELECT?TOP?'?+?STR(@TopN)?+?'?'?+?@SelectField?
??????????????????????????????+?'?FROM?'?+?@TableName?+?@SQLTemp?+?'?'?+?@SQLOrder
????????????
????????????SET?@commandText?=?'SELECT?'?+?@SelectField
??????????????????????????????+?'?FROM?('?+?@commandText?+?')?AS?TableTemp?ORDER?BY?'?+?@SortExpression?+?'?'?+?@SortDire
????????END
????END
????
????EXEC?(@commandText)
????
????--?这个是调试程序用的
????--?SELECT?@commandText
????
END

?

将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。
  相关解决方案