How to retrieve the total row count of a query wit

2019-02-14 06:50发布

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

5条回答
手持菜刀,她持情操
2楼-- · 2019-02-14 07:32

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-14 07:33

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.

查看更多
smile是对你的礼貌
4楼-- · 2019-02-14 07:41
SELECT     TOP (2) *,
           (SELECT COUNT(*) AS Expr1 FROM T) AS C
FROM         T
查看更多
一夜七次
5楼-- · 2019-02-14 07:48

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
    ...
查看更多
叼着烟拽天下
6楼-- · 2019-02-14 07:49

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

查看更多
登录 后发表回答