我有存储过程并且我执行了执行计划我已经获得了有关缺失索引的以下信息
/*
Missing Index Details from SQLQuery2.sql - bravo2.SFA_CAI_WP8 (PRIMASL\npremadasa (67))
The Query Processor estimates that implementing the following index could improve the query cost by 56.7627%.
*/
/*
USE [SFA_CAI_WP8]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[InvoiceHeader] ([Deleted])
INCLUDE ([InvoiceNo],[NetAmount])
GO
*/
因此我创建了非群集索引如下:
CREATE NONCLUSTERED INDEX NotDeletedInvoices
ON [dbo].[InvoiceHeader] ([Deleted])
INCLUDE ([InvoiceNo],[NetAmount])
GO
然而,我得到了缺少索引的结果....!
我该怎么做..?
我的存储过程
ALTER Proc [dbo].[srpInvoiceBucket]
@DistributionCenterId int
As
Set NoCount on
Begin
Begin Try
Declare @ID int,
@Division int,
@NumberofInvs int,
@Numberoflooping int,
@Maxamount money,
@InvAmount money,
@MinimumAmount money,
@MaxmimumAmount money;
Declare @InvCursor Cursor
Declare @InvoiceBucket Table (ID int identity primary key, [Range] nvarchar(100), Maximum money, Minimum money, NumberofInvoice int Default(0), Total money Default(0), Average money Default(0));
-- Use Ceiling Instead of Round Due to the Issue of Rounding Value could be lower than actual Value and extra group
Select @Maxamount = Max(NetAmount), @NumberofInvs = Count(*), @Division = Ceiling(Max(NetAmount)/10) from InvoiceHeader where DistributionCenterId = @DistributionCenterId;
IF @NumberofInvs <= 0 RAISERROR (N'No Invoice Available',16,1);
--Arranging Bucket groups
Set @Numberoflooping = 0;
While @Numberoflooping < 10
begin
If @Numberoflooping = 0
begin Set @MinimumAmount = @Numberoflooping * @Division; end
else begin Set @MinimumAmount = (@Numberoflooping * @Division) + 1; end
Set @MaxmimumAmount = (@Numberoflooping + 1) * @Division;
Insert into @InvoiceBucket ([Range],Maximum,Minimum ) Values (Cast(@MinimumAmount as nvarchar(100)) + ' - ' + Cast(@MaxmimumAmount as nvarchar(100)), @MaxmimumAmount,@MinimumAmount )
Set @Numberoflooping = @Numberoflooping + 1;
end
Set @InvCursor = Cursor For
Select NetAmount from InvoiceHeader where Deleted = 'false'
Open @InvCursor
Fetch Next From @InvCursor INTO @InvAmount
WHILE @@Fetch_Status = 0
Begin
--Amount Bucket Opearion
Set @ID = (Select ID from @InvoiceBucket Where @InvAmount Between Minimum and Maximum);
Update @InvoiceBucket Set NumberofInvoice = NumberofInvoice + 1, Total = Total + @InvAmount Where ID = @ID;
Fetch Next From @InvCursor INTO @InvAmount
End
Close @InvCursor
Deallocate @InvCursor
Update @InvoiceBucket Set Average = Round(Total / NumberofInvoice,2) Where NumberofInvoice > 0;
select * from @InvoiceBucket;
End Try
Begin Catch
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState );
End Catch
End
答案 0 :(得分:0)
我调查了您的存储过程。我认为您只需要为存储过程提供以下索引。
CREATE NONCLUSTERED INDEX Index1 ON [dbo].[InvoiceHeader] ([Deleted]) INCLUDE ([NetAmount])
CREATE NONCLUSTERED INDEX Index2 ON [dbo].[InvoiceHeader] (DistributionCenterId)
答案 1 :(得分:0)
看起来像Microsoft无意修复旧版SQL Server中的错误(可能会添加为注释,但代码不够)。看到这个链接: