如何使用 TOP 检索查询的总行数

发布于 2024-07-14 10:45:51 字数 243 浏览 4 评论 0原文

我有一个 SQL Server 2008 查询,

SELECT TOP 10 *
FROM T
WHERE ...
ORDER BY ...

我还想获取行的总数。 显而易见的方法是进行第二次查询

SELECT COUNT(*)
FROM T
WHERE ...
ORDER BY ...

有没有有效的方法?

谢谢

I have a SQL Server 2008 query

SELECT TOP 10 *
FROM T
WHERE ...
ORDER BY ...

I'd like to get also the total number of the rows. The obious way is to make a second query

SELECT COUNT(*)
FROM T
WHERE ...
ORDER BY ...

Is there an efficient method?

Thanks

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

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

发布评论

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

评论(6

黑寡妇 2024-07-21 10:45:51

您想要第二次查询吗?

SELECT TOP 10
    *, foo.bar
FROM
    T
    CROSS JOIN
    (SELECT COUNT(*) AS bar FROM T WHERE ...) foo
WHERE
    ...
ORDER BY
    ...

OR

DECLARE @bar int
SELECT @bar = COUNT(*) AS bar FROM T WHERE ...
SELECT TOP 10
    *, @bar
FROM
    T
    CROSS JOIN
    (SELECT COUNT(*) AS bar FROM T WHERE ...) foo
WHERE
    ...
ORDER BY
    ...

或者(编辑:使用WITH)

WITH cTotal AS
(
    SELECT COUNT(*) AS bar FROM T WHERE ...)
)
SELECT TOP 10
    *, cTotal .bar
FROM
    T
WHERE
    ...
ORDER BY
    ...

Do you want a second query?

SELECT TOP 10
    *, foo.bar
FROM
    T
    CROSS JOIN
    (SELECT COUNT(*) AS bar FROM T WHERE ...) foo
WHERE
    ...
ORDER BY
    ...

OR

DECLARE @bar int
SELECT @bar = COUNT(*) AS bar FROM T WHERE ...
SELECT TOP 10
    *, @bar
FROM
    T
    CROSS JOIN
    (SELECT COUNT(*) AS bar FROM T WHERE ...) foo
WHERE
    ...
ORDER BY
    ...

Or (Edit: using WITH)

WITH cTotal AS
(
    SELECT COUNT(*) AS bar FROM T WHERE ...)
)
SELECT TOP 10
    *, cTotal .bar
FROM
    T
WHERE
    ...
ORDER BY
    ...
潜移默化 2024-07-21 10:45:51

这个答案中的内容似乎有效:

https://stackoverflow.com/a/19125458/16241

基本上你做了一个:

SELECT top 100 YourColumns, TotalCount = Count(*) Over()
From YourTable
Where SomeValue = 32

TotalCount 将显示总行数。 但它列在每一行上。

当我对此进行测试时,查询计划显示该表仅被命中一次。

What is in this answer seems to work:

https://stackoverflow.com/a/19125458/16241

Basically you do a:

SELECT top 100 YourColumns, TotalCount = Count(*) Over()
From YourTable
Where SomeValue = 32

TotalCount will have the total number of rows. It is listed on each row though.

When I tested this the query plan showed the table only being hit once.

巷子口的你 2024-07-21 10:45:51

也从第二个查询中删除 ORDER BY 子句。

Remove the ORDER BY clause from the 2nd query as well.

最冷一天 2024-07-21 10:45:51

不会。

SQL Server 不会像 MyISAM 那样将 COUNT(*) 保留在元数据中,它每次都会计算它。

更新:如果您需要估计,可以使用统计元数据:

SELECT  rows
FROM    dbo.sysindexes
WHERE   name = @primary_key,

其中@primary_key是表的主键名称。

这将返回上次统计更新的 COUNT(*)

No.

SQL Server doesn't keep COUNT(*) in metadata like MyISAM, it calculates it every time.

UPDATE: If you need an estimate, you can use statistics metadata:

SELECT  rows
FROM    dbo.sysindexes
WHERE   name = @primary_key,

where @primary_key is your table's primary key name.

This will return the COUNT(*) from last statistics update.

离线来电— 2024-07-21 10:45:51
SELECT     TOP (2) *,
           (SELECT COUNT(*) AS Expr1 FROM T) AS C
FROM         T
SELECT     TOP (2) *,
           (SELECT COUNT(*) AS Expr1 FROM T) AS C
FROM         T
薄凉少年不暖心 2024-07-21 10:45:51

只是想 -

使用以下参数实现存储过程:

  1. @TotalCount(默认 0)
  2. @PageSize
  3. 必需的过滤器参数。

第一次返回2个结果集。

  1. 总计数 SQL 语句 - 返回为 @TotalCount(仅当输入参数 TotalCount = 0 时才执行)
  2. 使用 @TotalCount 根据需要顶部 @PageSize 计数

希望这会起作用。 没有尝试过,但从逻辑上讲它会起作用。

Just thought -

Implement stored procedure with below parameters:

  1. @TotalCount (default 0)
  2. @PageSize
  3. Required filter parameters.

Return 2 result set at first time.

  1. Total Count SQL statement - Return as @TotalCount (execute only if Input parameter TotalCount = 0)
  2. Top @PageSize count as required using @TotalCount

Hope this will work. Not tried but logically it will work.

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