sqlserverアドバンス

3740 ワード

1.インデックスがsqlserverを使ってインデックスを集めているのは、本当にクエリ速度を上げるものであり、そしてインデックスがクエリの中で最適化されるかどうかは、あなたが作成したインデックスがあなたが照会した条件またはgroup by文の中にあるかどうかであり、あなたのselectが検索した結果フィールドではない。
2.union all/union
select sum(a.AddPnt1) as num,a.ChrId1 FROM(
    select ChrId1,AddPnt1 from W_GldMatchBalanceLog where LogTime BETWEEN '2017-08-14' and '2017-08-15'
UNION ALL
    select ChrId2,AddPnt2 from W_GldMatchBalanceLog where LogTime BETWEEN '2017-08-14' and '2017-08-15'
UNION ALL
    select ChrId3,AddPnt3 from W_GldMatchBalanceLog where LogTime BETWEEN '2017-08-14' and '2017-08-15'
UNION ALL
    select ChrId4,AddPnt4 from W_GldMatchBalanceLog where LogTime BETWEEN '2017-08-14' and '2017-08-15') as a where a.ChrId1 <> 0 GROUP BY ChrId1 ORDER BY num DESC
同じアンケート結果の複数の値を取る必要がある場合、どれぐらいの検索が必要ですか?その後union、sql 2005以降は対象言語の変数に相当する概念のキーワードwith asが現れます。検索の結果を保存することができます。
select sum(a.AddPnt1) as num,a.ChrId1 FROM(
with cr AS
(SELECT ChrId1,AddPnt1,ChrId2,AddPnt2,ChrId3,AddPnt3,ChrId4,AddPnt4 from W_GldMatchBalanceLog where  LogTime BETWEEN '2017-08-14' and '2017-08-15')
SELECT ChrId1,AddPnt1 FROM cr
UNION ALL
SELECT ChrId2,AddPnt2 FROM cr
UNION ALL
SELECT ChrId3,AddPnt3 FROM cr
UNION ALL
SELECT ChrId4,AddPnt4 FROM cr)
下のコードの効率は上の倍以上で、しかもデータ量が大きいほど明らかです。