合并:SQL 2005+使用 SET ROWCOUNT 或 RowNumber() 进行动态分页?

发布于 2024-08-10 01:07:16 字数 1856 浏览 7 评论 0原文

我有一个复杂的 SP,它应用多个 JOIN 和查找,并带有复杂的过滤器,例如逗号分隔值等......最重要的是,我必须部署两个复杂但高效的功能:

1。动态排序但我看到它的局限性 - 你知道长/笨拙的 CASE 层次结构,奇怪的是专家们也同意这是我们拥有的唯一“最佳”解决方案:

SQL 存储过程中的动态排序 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID= 5942

不管怎样,我暂时对这个不抱太大期望。

2.动态分页 - 也就是说,我希望 SP 能够返回从 Y(Y = 页码)开始的 X 条记录(X = 页面大小)。我希望你已经有了大概的想法。

为了更清楚地说明,我想使用 MySQL 中可用的东西PostgreSQL:

[LIMIT { number | ALL }] [OFFSET number]

数据库:SQL 分页? http://www.sql.org/sql-database/ postgresql/manual/queries-limit.html

这么简单的 & 很奇怪。基本功能在 SQL 2005+ 中不可用..还是我错了(我很高兴听到它:-))


我已经知道两种适合我的性能\复杂性权衡的方法 -

[ 2.1]使用SQL 2005的“RowNumber()”功能,然后应用过滤器:(我过去使用过它)

WHERE (Row BETWEEN (@PageIndex-1) * @PageSize +1 AND @PageIndex* @PageSize)

但是,这需要创建一个临时表或使用WITH子句。这也解释在: SQL Server 中的行偏移

[2.2] 我发现了一些新方法. 其中之一是使用

SET ROWCOUNT

https://web.archive.org/web/20211020131201/https://www.4guysfromrolla.com/webtech/042606-1.shtml

他们说总体 2.2 是2.1 后生效。是吗?另外,我想知道如果两个用户请求同时触发同一个 SP 两次会发生什么。我希望“SET ROWCOUNT”不会在同时的 SP 调用中“共享”或“覆盖”。请确认。 比较 2.1 和 2.1 的任何其他点2.2?

I've a complex SP which applies multiple JOINs and lookup and come complex filters like comma-separated values, etc... On top of it, I've to deploy two complex yet performance-effective features:

1. Dynamic sorting but I see its limited - you kno the long/clumsy CASE hierarchy, its strange that experts also agree that this is the only 'best' solution we've got:

Dynamic Sorting within SQL Stored Procedures
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=5942

Anyway, I don't expect much on this one for now.

2. Dynamic pagination - that is I want the SP to be able to return only X number of records (X = page size) starting from Y (Y = page number). I hope you've got the general idea.

To make it more clear I want to use something available in MySQL & PostgreSQL:

[LIMIT { number | ALL }] [OFFSET number]

Database: SQL Pagination?
http://www.sql.org/sql-database/postgresql/manual/queries-limit.html

Its strange such a simple & basic functionality is NOT available in SQL 2005+ .. or am I wrong (I'd be glad to hear it :-))


I've known two approaches which suite my performance\complexity tradeoff -

[2.1] Using the 'RowNumber()' feature of SQL 2005 and then applying filter: (I've used it in past)

WHERE (Row BETWEEN (@PageIndex-1) * @PageSize +1 AND @PageIndex* @PageSize)

But again, this needs creating a temp table or using a WITH clause. This is also explained in:
Row Offset in SQL Server

[2.2] I found some new ways. One of them is using the

SET ROWCOUNT

https://web.archive.org/web/20211020131201/https://www.4guysfromrolla.com/webtech/042606-1.shtml

And they say that overall 2.2 is effective then 2.1. Is it? Also, I wanted to know what happens if two user-requests trigger the same SP twice simultaneously .. I hope the 'SET ROWCOUNT' won't be 'shared' or 'over-written' among simultaneous SP-calls. Pls confirm.
Any other points comparing 2.1 & 2.2 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2024-08-17 01:07:16

我相信这两种方法都很好,我们可以选择最适合我们要求的方法


我已经知道两种适合我的性能\复杂性权衡的方法 -

[2.1] 使用 SQL 2005 的“RowNumber()”功能< /strong> 然后应用过滤器:(我过去使用过它)

WHERE (Row BETWEEN (@PageIndex-1) * @PageSize +1 AND @PageIndex* @PageSize)

但是,这需要创建一个临时表或使用WITH子句。这也解释在:
SQL Server 中的行偏移

[2.2] 我发现了一些新方法. 其中之一是使用

SET ROWCOUNT

https://web.archive.org/web/20211020131201/https://www.4guysfromrolla.com/webtech/042606-1.shtml

他们说总体 2.2 是2.1 后生效。是吗?另外,我想知道如果两个用户请求同时触发同一个 SP 两次会发生什么。我希望“SET ROWCOUNT”不会在同时的 SP 调用中“共享”或“覆盖”。请确认。
比较 2.1 和 2.1 的任何其他点2.2?

I believe both approaches are good and we can choose to go with what best suits our requirements


I've known two approaches which suite my performance\complexity tradeoff -

[2.1] Using the 'RowNumber()' feature of SQL 2005 and then applying filter: (I've used it in past)

WHERE (Row BETWEEN (@PageIndex-1) * @PageSize +1 AND @PageIndex* @PageSize)

But again, this needs creating a temp table or using a WITH clause. This is also explained in:
Row Offset in SQL Server

[2.2] I found some new ways. One of them is using the

SET ROWCOUNT

https://web.archive.org/web/20211020131201/https://www.4guysfromrolla.com/webtech/042606-1.shtml

And they say that overall 2.2 is effective then 2.1. Is it? Also, I wanted to know what happens if two user-requests trigger the same SP twice simultaneously .. I hope the 'SET ROWCOUNT' won't be 'shared' or 'over-written' among simultaneous SP-calls. Pls confirm.
Any other points comparing 2.1 & 2.2 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文