It depends. What indexes are on the table? What volume of rows are within the table?
I've just created a sample which performed well but it may be very different to your scenario. As a rule, if the optimizer has trouble then the query needs to be simplified. What you have done maybe what is required. It depends. Here's the SQL I knocked up to see if I could find anything obvious with the show execution plan on.
set nocount on
GO
if object_id('tempdb..#MaxMinExample')is not null drop table #MaxMinExample
GO
create table #MaxMinExample([key] int identity(1,1) primary key clustered,iField1 int,iField2 int,dtDate datetime,iSomeField1 int,iSomeField2 int,iSomeField3 int,iSomeField4 int)
GO
--initial data set which we'll cartesian
insert into #MaxMinExample(iField1 ,iField2 ,dtDate ,iSomeField1 ,iSomeField2 ,iSomeField3 ,iSomeField4 )
values (1,2,getdate(),1,2,3,4)
insert into #MaxMinExample(iField1 ,iField2 ,dtDate ,iSomeField1 ,iSomeField2 ,iSomeField3 ,iSomeField4 )
values (2,3,getdate()+1,4,5,6,7)
insert into #MaxMinExample(iField1 ,iField2 ,dtDate ,iSomeField1 ,iSomeField2 ,iSomeField3 ,iSomeField4 )
values (3,4,getdate()+2,5,6,7,8)
insert into #MaxMinExample(iField1 ,iField2 ,dtDate ,iSomeField1 ,iSomeField2 ,iSomeField3 ,iSomeField4 )
values (5,6,getdate()+3,6,7,8,9)
insert into #MaxMinExample(iField1 ,iField2 ,dtDate ,iSomeField1 ,iSomeField2 ,iSomeField3 ,iSomeField4 )
values (6,7,getdate()+4,7,8,9,10)
GO
--create loads of data
declare @count int set @count=1
while (select count(*) from #MaxMinExample)<865830
begin
insert into #MaxMinExample(iField1 ,iField2 ,dtDate ,iSomeField1 ,iSomeField2 ,iSomeField3 ,iSomeField4 )
select a.iField1+@count ,a.iField2+@count ,a.dtDate+@count ,a.iSomeField1+@count ,a.iSomeField2+@count ,a.iSomeField3+@count ,a.iSomeField4+@count from #MaxMinExample a cross join #MaxMinExample b
set @count=@count+1
end
GO
--create the indexes
create index MaxMinExample_iSomeField1 on #MaxMinExample(iSomeField1)
create index MaxMinExample_iSomeField2 on #MaxMinExample(iSomeField2)
create index MaxMinExample_iSomeField3 on #MaxMinExample(iSomeField3)
create index MaxMinExample_iSomeField4 on #MaxMinExample(iSomeField4)
create index MaxMinExample_dtDate on #MaxMinExample(dtDate)
GO
declare @maxval1 int,@maxval2 int,@dtDateMin datetime,@dtDateMax datetime,@param1 int,@param2 int,@param3 int,@param4 int
select @param1=4,@param2=5,@param3=6,@param4=7
select @maxval1 = max(iField1), @maxval2 = max(iField2), @dtDateMin = Min(dtDate),@dtDateMax = Max(dtDate) from #MaxMinExample
where iSomeField1=@param1 and iSomeField2=@param2 and iSomeField3=@param3 and iSomeField4=@param4
select top 1 @maxval1 = iField1 from #MaxMinExample where iSomeField1=@param1 and iSomeField2=@param2 and iSomeField3=@param3 and iSomeField4=@param4 order by iField1 DESC
select top 1 @maxval2 = iField2 from #MaxMinExample where iSomeField1=@param1 and iSomeField2=@param2 and iSomeField3=@param3 and iSomeField4=@param4 order by iField2 DESC
select top 1 @dtDateMin = dtDate from #MaxMinExample where iSomeField1=@param1 and iSomeField2=@param2 and iSomeField3=@param3 and iSomeField4=@param4 order by dtDate ASC
select top 1 @dtDateMax = dtDate from #MaxMinExample where iSomeField1=@param1 and iSomeField2=@param2 and iSomeField3=@param3 and iSomeField4=@param4 order by dtDate DESC
Mark Baekdal - www.dbghost.com - SQL Developer