如何在SQL中有效返回记录数?

发布于 2024-09-17 19:31:44 字数 653 浏览 5 评论 0原文

我在 Windows Server 2008 Enterprise 上使用 SQL Server 2008 Enterprise。我使用以下代码返回查询的一部分数据,以在我的网络应用程序上实现分页(即向上/向下翻页以在每个页面上显示部分结果,如 Google 搜索结果分页)(我使用 pageCount 作为数字每页显示的结果数,startPos 为结果的起始编号)。例如,pageCount 10 表示每页显示 10 个结果,startPos = 0 表示第一页,startPos = 1 表示第二页,等等。

我的问题是如何在我的场景中有效地获取结果总数?我主要关心的是如何实现分页(即只触摸结果的一部分)并同时检索结果总数?

SELECT *
 FROM   (SELECT
    t.foo, t.goo, ROW_NUMBER() OVER (order by t.zoo DESC ) AS rowNum
   FROM
    dbo.mycorp  t

   WHERE
    (t.foo LIKE '%'+@search+'%'
    or t.foo  LIKE '%'+@search+'%'
    )
    ) tt
    WHERE  tt.rowNum between @startPos and  @pageCount + @startPos-1

提前致谢, 乔治

I am using SQL Server 2008 Enterprise on Windows Server 2008 Enterprise. I am using the following code to return a part of data for a query to implement paging (i.e. page up/down to show a part of result at each page, like Google search result paging) on my web application (I use pageCount as number of results showed on each page, and startPos as the start number of result). For example, pageCount 10 means show 10 results for each page, startPos = 0 means the first page, startPos = 1 means the 2nd page, etc.

My question is how to get the total number of results efficiently in my scenario? My major concern is how to implement paging (i.e. touch only a part of result) and at the same time retrieve the total number of results?

SELECT *
 FROM   (SELECT
    t.foo, t.goo, ROW_NUMBER() OVER (order by t.zoo DESC ) AS rowNum
   FROM
    dbo.mycorp  t

   WHERE
    (t.foo LIKE '%'+@search+'%'
    or t.foo  LIKE '%'+@search+'%'
    )
    ) tt
    WHERE  tt.rowNum between @startPos and  @pageCount + @startPos-1

thanks in advance,
George

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

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

发布评论

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

评论(2

白芷 2024-09-24 19:31:44

这将意味着冗余列,每行具有相同的值:

WITH cte AS (
    SELECT t.foo, 
           t.goo, 
           ROW_NUMBER() OVER (ORDER BY t.zoo DESC ) AS rowNum,
           (SELECT COUNT(*)
              FROM dbo.mycorp
             WHERE t.foo LIKE '%'+@search+'%'
                OR t.foo  LIKE '%'+@search+'%') AS total_count
      FROM dbo.mycorp  t
     WHERE t.foo LIKE '%'+@search+'%'
        OR t.foo  LIKE '%'+@search+'%')
SELECT c.foo,
       c.goo,
       c.total_count
  FROM cte c
 WHERE c.rowNum BETWEEN @startPos 
                    AND @pageCount + @startPos-1

搜索文本的更有效方法是使用 SQL Server 的全文搜索 (FTS) 功能

This will mean a redundant column, with the same value for every row:

WITH cte AS (
    SELECT t.foo, 
           t.goo, 
           ROW_NUMBER() OVER (ORDER BY t.zoo DESC ) AS rowNum,
           (SELECT COUNT(*)
              FROM dbo.mycorp
             WHERE t.foo LIKE '%'+@search+'%'
                OR t.foo  LIKE '%'+@search+'%') AS total_count
      FROM dbo.mycorp  t
     WHERE t.foo LIKE '%'+@search+'%'
        OR t.foo  LIKE '%'+@search+'%')
SELECT c.foo,
       c.goo,
       c.total_count
  FROM cte c
 WHERE c.rowNum BETWEEN @startPos 
                    AND @pageCount + @startPos-1

A more efficient means of searching text is to use SQL Server's Full Text Search (FTS) functionality.

请帮我爱他 2024-09-24 19:31:44

如果您的数据访问技术支持的话,我会考虑使用多个记录集(我知道 ADO.NET 确实支持这一点)。

在查询后包含以下内容

SELECT COUNT(*) AS TotalRecordCount
 FROM   dbo.mycorp  t 
   WHERE 
    (t.foo LIKE '%'+@search+'%' 
    or t.foo  LIKE '%'+@search+'%' 
    ) 
    ) tt

在数据访问层中,您需要在处理搜索结果后切换到下一个记录集才能检索总计数。使用 ADO.NET,这需要调用 dataReader.NextResult()。

最重要的是衡量绩效。我发现在某些情况下,使用单独的 select 语句而不是子查询可以提高性能,因为查询优化器能够更好地优化查询。无论哪种方式,您都需要在搜索列上建立适当的索引。

I would think about using the multiple recordsets if your data access technology supports it (I know ADO.NET does support this).

Include the following after your query

SELECT COUNT(*) AS TotalRecordCount
 FROM   dbo.mycorp  t 
   WHERE 
    (t.foo LIKE '%'+@search+'%' 
    or t.foo  LIKE '%'+@search+'%' 
    ) 
    ) tt

In your data access layer, you would need to switch to the next recordset after processing the search results in order to retrieve the total count. Using ADO.NET, this would require calling dataReader.NextResult().

The most important thing is to measure performance. I have found that in some cases, using a separate select statement rather than a sub-query can result in faster performance as the Query optimizer is able to better optimize the query. Either way, you will need suitable indexing on the search column.

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