开发者

What has better performance on SQL Server

开发者 https://www.devze.com 2023-02-23 19:50 出处:网络
I want to compare a number of values (up to ten) with a function that will return the smallest value of them.

I want to compare a number of values (up to ten) with a function that will return the smallest value of them.

My colleague wrote the function like:

set @smallest = null

if @smallest is null or @date0 < @smallest 
begin
   开发者_如何学C set @smallest = @date0
end

if @smallest is null or @date1 < @smallest 
begin
    set @smallest = @date1
end
... (repeating 10 times)

Beside of that the if statement could be written smarter (the null check can fall away after the first comparison) I was wondering if creating an in-memory indexed table and let the function return me the first value would be more efficient? Is there any documentation that I could read for this?


creating an in-memory indexed table

There is no point having an index on 10 records. Create a derived table (will sit in memory) as shown below, then run MIN across the table:

select @smallest = MIN(Adate)
from (
    select @date0 Adate union all
    select @date1 union all
    select @date2 union all
    -- ....
    select @date9) X
0

精彩评论

暂无评论...
验证码 换一张
取 消