私は、Id、時間、値の列を持つテーブルを持っています。SQL Serverコードを最適化する方法は?
最初のステップ:信号のID、開始時刻、終了時刻の入力パラメータを指定すると、最初に信号IDと時刻が開始時刻と終了時刻の間の行を抽出します。
第2段階:最初の手順で100行を選択したとします。 max_numという別の入力パラメータが与えられた場合、100行の中からmax_num個のサンプルをさらに一貫して選択したいと考えています。たとえば、max_numが10に設定されている場合、100行の中から1,11,21、... 91行を選択します。
以下のストアドプロシージャが最適であるかどうかわかりませんが、コードの非効率性がある場合は、私にそれを指摘し、いくつかの提案をしてください。
create procedure data_selection
@sig_id bigint,
@start_time datetime2,
@end_time datetime2,
@max_num float
AS
BEGIN
declare @tot float
declare @step int
declare @selected table (id int primary key identity not null, Date datetime2, Value real)
// first step
insert into @selected (Date, Value) select Date, Value from Table
where Id = @sig_id
and Date > = @start_time and Date < = @end_time
order by Date
// second step
select @tot = count(1) from @selected
set @step = ceiling(@tot/@max_num)
select * from @selected
where id % @step = 1
END
私はstackoverflowの新しいですので、質問がdownvoteを与えるのではなく、不適切である場合は教えてください。 – Matthew
私には合理的に見えます。なぜそれがdownvotedされたか分からない? –
私は投票しませんでしたが、@tot <@Max_Numのシナリオを検討しましたか?試してみてください – Adish