在下面的分页查询中,仅用2秒即可从10万条记录中提取25条记录。但是,当我在查询中添加until
列以返回记录的总数(100k)时,它花费了超过1分钟的时间。有没有什么方法可以优化查找总记录数?
下面的一个正在快速运行,而没有在外部选择查询中包含TOTROWS
列。
TOTROWS
在外部选择查询中添加DECLARE @PRODUCTNAME NVARCHAR(200),
@PAGE VARCHAR(100)
SET NOCOUNT ON;
DECLARE @ROWNUM INT = 25
DECLARE @ROWCOUNT
DECLARE @TOTROWS INT
DECLARE @XY INT
SET @PAGE = 1
SELECT TOP 25
ID, NAME
FROM
(SELECT
*,
TOTROWS = COUNT(ID) OVER()
FROM
(SELECT DISTINCT
TP.ID AS ID, TP.NAME AS [NAME],
ROW_NUMBER() OVER (ORDER BY TP.ID ASC) AS Row
FROM
PDF TP
<WHERE CONDITIONS>
UNION ALL
SELECT DISTINCT
TP.ID AS ID, TP.NAME AS [NAME],
ROW_NUMBER() OVER (ORDER BY TP.ID ASC) AS Row
FROM
HTML TP
WHERE <conditions>) a
WHERE
ROW > (@PAGE - 1) * 25) XY
列后,下面的命令运行缓慢。
TOTROWS