如何确定最佳查询,以确保它在最短时间内执行

时间:2015-04-27 06:07:51

标签: sql-server sql-server-2012

我正在使用SQL Server 2012。

查询

 create table domestic
 (
     salesid int, saleamt money
 )

 create table international
 (
     salesid int, saleamt money
 )

 insert into domestic values(1,200),(2,400)
 insert into international values(4,500),(6,800)

 --to get the difference
--1
 select count(salesid),sum(saleamt) from domestic
 union all
  select count(salesid),sum(saleamt) from international
--2
  select count(salesid),sum(saleamt) from
  (
  select salesid,saleamt from domestic
  union all
   select salesid,saleamt from international
   )x

我已经包含了这些的执行计划。并检查了statistics

enter image description here

enter image description here

set statistics io on;

的结果
(2 row(s) affected)
Table 'international'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'domestic'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row(s) affected)
Table 'international'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'domestic'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

有人可以帮我决定哪些会在最短的时间内执行吗?

2 个答案:

答案 0 :(得分:3)

查询不等同(产生不同的结果),因此对它们进行比较并不是很有用。

无论如何,我认为你需要在表格中有更多的数据来进行有意义的测试。

答案 1 :(得分:1)

请使用SET STATISTICS TIME ON查找执行时间并进行比较。