0

この SQL クエリをより効率的に記述する方法はありますか?

約 45 秒で 100,000 行のセットから約 800 行を返します。

Sql Server 2008 R2 を使用しています

Select a.Div as Division
      ,a.Room as RoomLocation
      ,a.Form as Forms
      ,a.Nums as TotalNumberLocations
From AView a
Where a.Id = '1'
And a.Div = 'A'
Group By a.Div, a.Nums, a.Room, a.Form

union

Select b.Div as Division
      ,b.Room as RoomLocation
      ,b.Form as Forms
      ,b.Nums as TotalNumberLocations
From AView b
Where b.Id = '1'
And b.Div = 'G'
Group By b.Div, b.Nums, b.Room, b.Form

union

Select c.Div as Division
      ,c.Room as RoomLocation
      ,c.Form as Forms
      ,c.Nums as TotalNumberLocations
From AView c
Where c.Id = '1'
And c.Div = 'R'
Group By c.Div, c.Nums, c.Room, c.Form
Order By Forms asc, TotalNumberLocations asc
4

3 に答える 3

7

IN 句に値を指定できるのに、なぜ UNION を使用するのでしょうか。テーブルを 3 回スキャンしていました。

Select Div as Division
      ,Room as RoomLocation
      ,Form as Forms
      ,Nums as TotalNumberLocations
From AView 
Where Id = '1'
And Div IN ('A','G','R')
Group By Div, Nums, Room, Form
Order By Forms asc, TotalNumberLocations asc
于 2013-09-06T19:08:04.273 に答える
4

他の回答と同様ですが、GROUP BYをに置き換えることもできますdistinct

Select distinct Div as Division
      ,Room as RoomLocation
      ,Form as Forms
      ,Nums as TotalNumberLocations
From AView a
Where Id = '1'
And Div in ('A', 'G', 'R')
Order By Forms asc, TotalNumberLocations asc
于 2013-09-06T19:10:08.500 に答える
2

Id列とDivテーブルのインデックスはAView、このクエリのパフォーマンスにとって非常に有益であり、おそらくクエリを書き直すよりもはるかに有益であると思います。

于 2013-09-06T19:16:51.363 に答える