0

次のコードのように、サブクエリが適切であり、その場所がある場合があります。

select x1.test, (select r from table) from ......

ただし、サブクエリを追加すると、パフォーマンスの問題や再利用性の問題など、クエリのハンディキャップが増えます。

以下のSQLコードをリファクタリングして、以前よりも高速に実行するにはどうすればよいですか...



Select f.c1
      ,f.c2
         ,f.c3 As 'c3Id'
         ,p.Value As 'c3'
         ,m.c4 As 'r1'


         ,(Select SUM(t1.table1x)
             From [ap].table1 t1
             Where t1.table1x = 1 
               And f.c1 = t1.c1 
               And f.c2 = t1.c2
               And f.c3 = t1.c3) As 'r2'

         ,(Select SUM(t2.x)
             From [ap].table2 t2
             Where t2.c1 = f.c1
               And t2.c1 = f.c2
               And t2.c3 = f.c3) As 'r3'

         ,(Select SUM(t1.table1x)
             From [ap].table3 t3
             Where t3.table3Turu = 2
               And f.c1 = t3.c1 
               And f.c2 = t3.c2
               And f.c3 = t3.c3) As 'r4'

         ,(Select SUM(t4.x)
             From [ap].table4 t4
             Where t4.c1 = f.c1
               And t4.c1 = f.c2
               And t4.c3 = f.c3) As 'r5'

From [ap].table1 f 
Inner Join [dbo].table5 m On f.c1 = m.col11
Inner Join [dbo].table6 p On f.c3 = p.col22

Where p.xxxx = 'test'
Group By f.c1, f.c2, f.c3, m.c4, p.Value

4

1 に答える 1

0

You can use UDF (User Defined Functions) to simplify the code. Include your subquery into a function and send required parameters to this function from your main query. For Example, you can send 3 parameters f.c1, f.c2 and f.c3 to a function say GetSumT1() and use that in the query in place of your sub query.

Select f.c1       
,f.c2          
,f.c3 As 'c3Id'          
,p.Value As 'c3'          
,m.c4 As 'r1'            
, GetSumT1(f.c1, f.c2, f.c3) As 'r2'
...
...
于 2012-10-09T07:10:27.487 に答える