How to optimize slq query? : Hi this is my procedure i think it is to long an not optimized.How can i optimize it better to short query.<br/>
This is the query:<br/>




 <pre><code>
ALTER function [dbo].[FuncBusReport](@startdate datetime,@enddate datetime,@top int ,@state int)
returns @BusTable table 
(
       [Id][int] identity(1,1) not null
      ,[state][nvarchar](50)
      ,[Price][nvarchar](50)
      ,[ReserveType][nvarchar](50)
      ,[ObjectIdDepartue][int]
      ,[IssueDate][nvarchar](50)
      ,[BankId][int]
	  ,[Confirmed][bit]
      ,[TrackingCode][nvarchar](50)
	  ,[Transactionsuccess][nvarchar](50)
      ,[Name][nvarchar](128)
	  ,[TiketUrl][nvarchar](128)
	  ,[ObjectIdReturn]  [int] null
	  ,[TelNumber][nvarchar](50) null
)
as begin

	if(@state=1)
	     insert into @BusTable
     select top (@top)
       'ناموفق' 
      ,[Price]
      ,'اتوبوس'
      ,[ObjectIdDepartue]
      ,[dbo].PersianDate(IssueDate) + ' '+ [dbo].[TimeOfDateTime](IssueDate)
      ,[BankId]
	  ,[Confirmed]
      ,[TrackingCode]
	  ,[Transactionsuccess]
      ,[dbo].[Profile].FirstName+' '+[dbo].[Profile].LastName as Name
	  ,[dbo].GetUrlDownloads(4,[ObjectIdDepartue])
	  ,[ObjectIdReturn]
	  ,[dbo].[payments].[TelNumber]
	  from [dbo].[payments]
	   inner join [dbo].[Profile] on [dbo].[Profile].[UserId]=[dbo].[payments].UserId
	   where 
	   [dbo].[Payments].[IssueDate] >=@startdate
       AND [dbo].[Payments].[IssueDate] <=@enddate 
	   and [dbo].[payments].ReserveType=4

	   and [dbo].[payments].[transactionsuccess] in(0,1)
	   and [dbo].[payments].[state] in (1,2)
	   else if(@state=3)
	    insert into @BusTable
select top (@top)
       'پرداخت موفق رزرو ناموفق' 
      ,[Price]
      ,'اتوبوس'
      ,[ObjectIdDepartue]
      ,[dbo].PersianDate(IssueDate) + ' '+ [dbo].[TimeOfDateTime](IssueDate)
      ,[BankId]
	  ,[Confirmed]
      ,[TrackingCode]
	  ,[Transactionsuccess]
      ,[dbo].[Profile].FirstName+' '+[dbo].[Profile].LastName as Name
	  ,[dbo].GetUrlDownloads(4,[ObjectIdDepartue])
	  ,[ObjectIdReturn]
	  ,[dbo].[payments].[TelNumber]
	  from [dbo].[payments]
	   inner join [dbo].[Profile] on [dbo].[Profile].[UserId]=[dbo].[payments].UserId
	   where 
	   [dbo].[Payments].[IssueDate] >=@startdate
       AND [dbo].[Payments].[IssueDate] <=@enddate 
	   and [dbo].[payments].ReserveType=4
	   and [dbo].[payments].[state] in (3,5)
	    else if(@state=6)
	    insert into @BusTable
select top (@top)
       state 
      ,[Price]
      ,'اتوبوس'
      ,[ObjectIdDepartue]
      ,[dbo].PersianDate(IssueDate) + ' '+ [dbo].[TimeOfDateTime](IssueDate)
      ,[BankId]
	  ,[Confirmed]
      ,[TrackingCode]
	  ,[Transactionsuccess]
      ,[dbo].[Profile].FirstName+' '+[dbo].[Profile].LastName as Name
	  ,[dbo].GetUrlDownloads(4,[ObjectIdDepartue])
	  ,[ObjectIdReturn]
	  ,[dbo].[payments].[TelNumber]
	  from [dbo].[payments]
	   inner join [dbo].[Profile] on [dbo].[Profile].[UserId]=[dbo].[payments].UserId
	   where 
	   [dbo].[Payments].[IssueDate] >=@startdate
       AND [dbo].[Payments].[IssueDate] <=@enddate 
	   and [dbo].[payments].ReserveType=4
	   and [dbo].[payments].[state] =6
	    else if(@state=4)
	    insert into @BusTable
select top (@top)
       'برگشت خرید'
      ,[Price]
      ,'اتوبوس'
      ,[ObjectIdDepartue]
      ,[dbo].PersianDate(IssueDate) + ' '+ [dbo].[TimeOfDateTime](IssueDate)
      ,[BankId]
	  ,[Confirmed]
      ,[TrackingCode]
	  ,[Transactionsuccess]
      ,[dbo].[Profile].FirstName+' '+[dbo].[Profile].LastName as Name
	  ,[dbo].GetUrlDownloads(4,[ObjectIdDepartue])
	  ,[ObjectIdReturn]
	  ,[dbo].[payments].[TelNumber]
	  from [dbo].[payments]
	   inner join [dbo].[Profile] on [dbo].[Profile].[UserId]=[dbo].[payments].UserId
	   where 
	   [dbo].[Payments].[IssueDate] >=@startdate
       AND [dbo].[Payments].[IssueDate] <=@enddate 
	   and [dbo].[payments].ReserveType=4
	   and [dbo].[payments].[state] =4
	    else if(@state=0)
	    insert into @BusTable
select top (@top)
       case
	    when [state] in(1,2) then 'ناموفق'
		when [state] in(3,5) then 'پرداخت موفق و رزرو ناموفق'
		when [state] = 4 then 'برگشت خرید'
	    when [state] = 6 then 'پرداخت موفق رزرو موفق'
	   end 

      ,[Price]
      ,'اتوبوس'
      ,[ObjectIdDepartue]
      ,[dbo].PersianDate(IssueDate) + ' '+ [dbo].[TimeOfDateTime](IssueDate)
      ,[BankId]
	  ,[Confirmed]
      ,[TrackingCode]
	  ,[Transactionsuccess]
      ,[dbo].[Profile].FirstName+' '+[dbo].[Profile].LastName as Name
	  ,[dbo].GetUrlDownloads(4,[ObjectIdDepartue])
	  ,[ObjectIdReturn]
	  ,[dbo].[payments].[TelNumber]
	  from [dbo].[payments]
	   inner join [dbo].[Profile] on [dbo].[Profile].[UserId]=[dbo].[payments].UserId
	   where 
	   [dbo].[Payments].[IssueDate] >=@startdate
       AND [dbo].[Payments].[IssueDate] <=@enddate 
	   and [dbo].[payments].ReserveType=4
	   
return
end;
</code></pre>