簡単なクエリを実行したい単純なデータベースがあります。
これらは私のデータベーステーブルの列です:
- external_id
- タイムスタンプ
- 価値
- 検証
- 理由
- データム
- ウル
- アフロンデン
列は次のとおりです。
external_id
特定のメーターのIDですTimestamp
何もしませんValue
はそのメーターの値ですValidation
はいまたはいいえですReason
varchar
理由のある値を取得しましたDatum
日付ですUur
その時間ですAfronden
丸めに必要な列です
実行したいクエリは、毎日の値の合計の最高値と最低値を取得することを目標として取得しました。
ご覧のとおり、毎日が時間単位で分割され、データが同じか変更されているかを確認し、それによって合計値を取得する必要があります。
これは私のクエリです:
Declare @totaal bigInt
Declare @tussentotaal bigint
Declare @Datum varchar
Declare @datumverschil varchar
Declare @hoogste bigint
Declare @laagste bigint
Declare @teller bigint
Declare @tellettotaal bigint
set @tellettotaal = (select count(*) from cresent_opdracht_de_proost_wim.dbo.[test])
Set @teller = 1
SET @datum = (Select top(1) datum
from cresent_opdracht_de_proost_wim.dbo.[test]
order by afronden asc)
Set @datumverschil = @Datum
set @tussentotaal = 0
set @totaal = 0
set @hoogste = 1775000006856
set @laagste = 1775000006856
while @teller <= @tellettotaal
begin
if @teller = 1
Begin
set @tussentotaal = (select top(1) value
from cresent_opdracht_de_proost_wim.dbo.[test]
order by afronden asc)
if @tussentotaal != 0
begin
Set @tussentotaal = @tussentotaal/100
end
End
Else
begin
SET @tussentotaal = (Select top(1) value
from (select top (@teller) *
from cresent_opdracht_de_proost_wim.dbo.[test]) q
order by afronden desc)
Set @tussentotaal = @tussentotaal/100
end
if @tussentotaal != 0
Begin
Set @totaal = @totaal + @tussentotaal
end
SET @teller= @teller + 1
Set @datumverschil = (Select top(1) datum
from (select top (@teller) *
from cresent_opdracht_de_proost_wim.dbo.[test]) q
order by afronden desc)
if @datum != @datumverschil
Begin
if @totaal >= @hoogste
begin
set @hoogste = @totaal
end
if @totaal <= @laagste
begin
if @totaal != 0
Begin
set @laagste = @totaal
end
end
Set @datum = @datumverschil
set @totaal = 0
select @teller As teller
end
end
Select @hoogste As hoogste
Select @laagste As laagste
22 分後、44000 行のみが処理されました。
クエリを最適化する方法を知っている人はいますか?