这是我的程序,我认为它是长期未优化的。如何更好地优化它以简化查询?
这是查询:
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;
答案 0 :(得分:1)
这就是我在这里做的缩短查询的原因
ALTER function [dbo].[FuncBusReport](@startdate datetime,@enddate datetime,@top int ,@state int)
returns @BusTable table
(
[Id][int] identity(1,1) not null
,[state]nvarchar
,[Price]nvarchar
,[ReserveType]nvarchar
,[ObjectIdDepartue][int]
,[IssueDate]nvarchar
,[BankId][int]
,[Confirmed][bit]
,[TrackingCode]nvarchar
,[Transactionsuccess]nvarchar
,[Name]nvarchar
,[TiketUrl]nvarchar
,[ObjectIdReturn] [int] null
,[TelNumber]nvarchar null
)
as begin
DECLARE @StateID INT;
DECLARE @StateTable TABLE (col INT)
SELECT @StateID=@state;
IF @StateID= 1
BEGIN
INSERT INTO @StateTable(col)VALUES(1);
INSERT INTO @StateTable(col)VALUES(2);
END;
IF @StateID= 2
BEGIN
INSERT INTO @StateTable(col)VALUES(3);
INSERT INTO @StateTable(col)VALUES(5);
END;
IF @StateID= 3
BEGIN
INSERT INTO @StateTable(col)VALUES(4);
END;
IF @StateID= 4
BEGIN
INSERT INTO @StateTable(col)VALUES(6);
END;
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
,[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 (select*from @StateTable)
return
end;