你能禁用ServiceStack AutoQuery的count(Total)吗?

时间:2016-06-21 19:24:53

标签: c# servicestack autoquery-servicestack

我对两个表的简单连接进行了AutoQuery设置,大约有130万行。使用内置的迷你分析器来测量SQL时序,返回前100行(无过滤)的查询需要3ms,计数需要额外的341ms。

是否可以在不获取计数的情况下使用AutoQuery?我实际上并不需要了解完整的计数。

修改

所以我在想,找出剩余的行数是否与完整计数相比可能会更快。我使用SSMS测试了我们的MSSQL数据库的时间。

--Generated by ServiceStack
set statistics time on
SELECT COUNT(*) "COUNT(*)" 
FROM "table1" INNER JOIN "table2" ON
("table1"."PrimaryKey" = "table2"."ForeignKey")
set statistics time off

--Skipping 100
set statistics time on
SELECT CASE WHEN EXISTS(
  SELECT "table1"."PrimaryKey"
  FROM "table1" INNER JOIN "table2" ON
  ("table1"."PrimaryKey" = "table2"."ForeignKey")
  ORDER BY "table1"."PrimaryKey" OFFSET 100 ROWS FETCH NEXT 1 ROWS ONLY
) 
THEN CAST(1 AS BIT) 
ELSE CAST(0 AS BIT) END
set statistics time off

--Skipping 100000
set statistics time on
SELECT CASE WHEN EXISTS(
  SELECT "table1"."PrimaryKey"
  FROM "table1" INNER JOIN "table2" ON
  ("table1"."PrimaryKey" = "table2"."ForeignKey")
  ORDER BY "table1"."PrimaryKey" OFFSET 100000 ROWS FETCH NEXT 1 ROWS ONLY
) 
THEN CAST(1 AS BIT) 
ELSE CAST(0 AS BIT) END
set statistics time off

--Skipping 1000000
set statistics time on
SELECT CASE WHEN EXISTS(
  SELECT "table1"."PrimaryKey"
  FROM "table1" INNER JOIN "table2" ON
  ("table1"."PrimaryKey" = "table2"."ForeignKey")
  ORDER BY "table1"."PrimaryKey" OFFSET 1000000 ROWS FETCH NEXT 1 ROWS ONLY
) 
THEN CAST(1 AS BIT) 
ELSE CAST(0 AS BIT) END
set statistics time off

输出:

(1 row(s) affected)

 SQL Server Execution Times:
   CPU time = 203 ms,  elapsed time = 200 ms.

(1 row(s) affected)

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

(1 row(s) affected)

 SQL Server Execution Times:
   CPU time = 16 ms,  elapsed time = 19 ms.

(1 row(s) affected)

 SQL Server Execution Times:
   CPU time = 203 ms,  elapsed time = 193 ms.

1 个答案:

答案 0 :(得分:3)

这不仅仅是您是否需要知道完整计数,而且ServiceClient API's like GetLazy()还需要总计,因此它能够透明地在多个分页查询后传输AutoQuery结果。

之前这不是一个明确的选项,但您可以通过添加一个预先填充它的ResponseFilter来避免AutoQuery查询Total,例如:

Plugins.Add(new AutoQueryFeature {
    MaxLimit = 100,
    ResponseFilters = {
        ctx => { ctx.Response.Meta["COUNT(*)"] = "0"; }
    }
});

我还在this commit中添加了对此选项的支持,因此在以后的版本中,您可以删除总计:

Plugins.Add(new AutoQueryFeature {
    MaxLimit = 100,
    IncludeTotal = false,
});

此更改可从v4.0.61获得,现在为available on MyGet