「count( ) over()」は「select count( ) from table」よりもはるかに高速であることがわかりました。
例えば
count( *) オーバーを使用
with CTE as(
select col_A,col_B,totalNumber=count(*) over() from table1 where conditions..)
select totalNumber from CTE
select count( *) from を使用 (または count(1) も使用)
select count(*) from table1 where conditions..
SQL Server 2K5でのローカル テストでは、count( ) over* は、検索条件が複雑で、返される行が大きい場合、4 倍速くなります。
しかし、count(**) のオーバー パフォーマンスがこれほど高速なのはなぜでしょうか。
前もって感謝します。
ヴァンス
アップデート
私は本当にいくつかの詳細を見逃したと思います:
実際には、次のようなテストに「ステートメントの準備」SQLを使用します。
exec sp_executesql N'SELECT count(*)
FROM tableA WHERE (aaa in(@P0))
AND (bbb like @P1)',
N'@P0 nvarchar(4000),@P1 nvarchar(4000)',N'XXXXXXX-XXXX-XXX',N'%AAA%'
Execution Plan says "HashMatch" cost 61%, others is "index seek". And the execution time will be 1484ms and logical reads around 4000.
これは、
SELECT count(*)
FROM tableA WHERE (aaa in('XXXXXXX-XXXX-XXX'))
AND (bbb like '%AAA%')
Execution plan says "clustered index seek" cost 98%. And the execution time is 46ms and logical reads will be 8000.
そして、最初のSQLを次のように変更した場合:
exec sp_executesql N'with CTE as(
SELECT total=count(*) over ()
FROM tableA WHERE (aaa in(@P0))
AND (bbb like @P1)) select top 1total from cte',
N'@P0 nvarchar(4000),@P1 nvarchar(4000)',N'XXXXXXX-XXXX-XXX',N'%AAA%'
Execution plan says "clustered index seek 58%', no "hashmatch join" occurs.
And the execution time is 15ms and logical reads is: 8404.
それで、「ハッシュマッチ結合」はパフォーマンスに多くのオーバーヘッドがありますか?