如果我不得不猜测,我会说这是因为查询 1 正在从两个表中提取数据。查询 2 和 3(大约同时)仅提取 TabA 的数据。
您可以检查这一点的一种方法是运行以下命令:
SET STATISTICS TIME ON
SET STATISTICS IO ON
当我跑
SELECT * FROM sys.objects
我看到了以下结果。
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 104 ms.
(242 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'sysschobjs'. Scan count 1, logical reads 10, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'syssingleobjrefs'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'syspalnames'. Scan count 1, logical reads 2, physical reads 1, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 866 ms.
您可以查看每个查询的扫描次数、逻辑读取次数和物理读取次数。物理读取当然需要更长的时间,并且代表从磁盘读取到缓存中。如果您的所有读取都是逻辑读取,那么您的表完全在缓存中。
我敢打赌,如果你看一下,在查询 1 上的 TabB 上会比在 2 和 3 上看到更多的逻辑读取。
编辑:
只是出于好奇,我做了一些测试并将结果记录在这里。