1

可以在第一个但不能在第二个中使用 OPTION (HASH GROUP)。如何在第二个中包含 OPTION (HASH GROUP)?

  SELECT 
  count(*) as 'match'  
  FROM [docSVenum1] with (nolock)   
  INNER LOOP JOIN  [FTSindexWordOnce] as w1 with (NOLOCK, FORCESEEK) 
    ON [docSVenum1].sID = w1.[sID] and [docSVenum1].[enumID] = '142'
  INNER HASH JOIN [FTSindexWordOnce] as w2 with (NOLOCK)
    ON  w1.wordID = w2.wordID and w2.[sID] = '2'      
  GROUP BY W1.[sID]
  OPTION (HASH GROUP)


select max(list.match) as 'max'
  from
  (
      SELECT 
       count(*) as 'match'  
      FROM [docSVenum1] with (nolock)   
      INNER LOOP JOIN  [FTSindexWordOnce] as w1 with (NOLOCK, FORCESEEK) 
        ON [docSVenum1].sID = w1.[sID] and [docSVenum1].[enumID] = '142'
      INNER HASH JOIN [FTSindexWordOnce] as w2 with (NOLOCK)
        ON  w1.wordID = w2.wordID and w2.[sID] = '2'      
      GROUP BY W1.[sID]
      -- OPTION (HASH GROUP)
  ) as list;

在现实生活中,完整的查询将更像这样

    select max(list.match) as 'max'
  from
  (
      SELECT 200*count(*)/([d1].[textSize] + [d2].[textSize]) as 'match'  
      FROM [docSVenum1] with (nolock)   
      INNER LOOP JOIN  [FTSindexWordOnce] as w1 with (NOLOCK, FORCESEEK) 
        ON [docSVenum1].sID = w1.[sID] and [docSVenum1].[enumID] = '142'
      INNER HASH JOIN [FTSindexWordOnce] as w2 with (NOLOCK)
        ON  w1.wordID = w2.wordID and w2.[sID] = '2'
      JOIN docSVsys as d1 with (nolock)
        on d1.sID = w1.sID
      JOIN docSVsys as d2 with (nolock)
        on d2.sID = w1.sID    
      GROUP BY W1.[sID],  [d1].[textSize], [d2].[textSize]
      -- OPTION (HASH GROUP) 
  ) as list;
4

1 回答 1

2

我可以as list在 SQL Server 2005(没有 forceseek 提示,版本 = 9.0.5266)、2008(10.0.5785)和 2012(11.0.2316)上的子查询(之后)之外使用该选项。但在 2008 R2 (10.50.2811) 上,我收到的错误消息是:

消息 8622,级别 16,状态 1,第 1 行
查询处理器无法生成查询计划,因为此查询中定义了提示。在不指定任何提示且不使用 SET FORCEPLAN 的情况下重新提交查询。

SELECT MAX(x.match) FROM (
SELECT 
  count(*) as match  
  FROM sys.objects AS o with (nolock, forceseek)   
  INNER LOOP JOIN sys.columns as c with (NOLOCK) 
    ON o.object_id = c.object_id
  INNER HASH JOIN sys.columns as c2 with (NOLOCK)
    ON o.object_id = c2.object_id   
  GROUP BY o.object_id
) AS x
  OPTION (HASH GROUP);

如果我不使用聚合,它可以工作:

SELECT x.match FROM (
SELECT 
  count(*) as match  
  FROM sys.objects AS o with (nolock, forceseek)   
  INNER LOOP JOIN sys.columns as c with (NOLOCK) 
    ON o.object_id = c.object_id
  INNER HASH JOIN sys.columns as c2 with (NOLOCK)
    ON o.object_id = c2.object_id   
  GROUP BY o.object_id
) AS x
  OPTION (HASH GROUP);

如果我使用这种使用 TOP 而不是 MAX 的变体,它也可以:

SELECT TOP (1) x.match FROM (
SELECT 
  count(*) as match  
  FROM sys.objects AS o with (nolock, forceseek)   
  INNER LOOP JOIN sys.columns as c with (NOLOCK) 
    ON o.object_id = c.object_id
  INNER HASH JOIN sys.columns as c2 with (NOLOCK)
    ON o.object_id = c2.object_id   
  GROUP BY o.object_id
) AS x
  ORDER BY x.match DESC
  OPTION (HASH GROUP);

甚至可以尝试消除子查询(假设仅用于隔离最大值 - 如果是这样,则没有必要):

SELECT TOP (1) 
  count(*) as [max]  
  FROM sys.objects AS o with (nolock, forceseek)   
  INNER LOOP JOIN sys.columns as c with (NOLOCK) 
    ON o.object_id = c.object_id
  INNER HASH JOIN sys.columns as c2 with (NOLOCK)
    ON o.object_id = c2.object_id   
  GROUP BY o.object_id
  ORDER BY [max] DESC
  OPTION (HASH GROUP);

我有一个问题,想看看这是否是我针对 SQL Server 2012 报告的类似问题。当我了解更多信息时,我会在这里更新。

于 2012-07-25T16:23:34.420 回答