加快这个程序

时间:2013-03-13 08:57:41

标签: sql sql-server tsql sql-server-2012

我的表有超过100K的记录,并且已有适当的索引。这是我的存储过程,耗时差不多1.2秒,目前是不可接受的:

ALTER PROCEDURE [dbo].[GetNextAndPreviousInsturmentID]
(
    @AccountID      int,
    @JurisdictionID int,
    @InstrumentID   varchar(14),
    @PreviousInstrumentID   varchar(14) OUT,
    @NextInstrumentID   varchar(14) OUT,
    @PreviousDocumentID int OUT,
    @NextDocumentID int OUT,
    @ShowPublicPageForPreviousInstrumentID  bit OUT,
    @ShowPublicPageForNextInstrumentID  bit OUT
)
AS
BEGIN
    Declare @RowNum int = 0
    Declare @PreviousCreatedByAccountID         int = 0
    Declare @PreviousBelongsToJurisdictionID    int = 0
    Declare @NextCreatedByAccountID             int = 0
    Declare @NextBelongsToJurisdictionID        int = 0

    Select @RowNum = RowNum
    From
    (
        Select ROW_NUMBER() Over (Order by RecordingDateTime) as RowNum, InstrumentID
                From Documents Where RecordingDateTime IS NOT NULL
    ) v where v.InstrumentID = @InstrumentID


    ;With normal As
    (   
        Select  ROW_NUMBER() Over (Order by RecordingDateTime) as RowNum, 
                DocumentID, 
                CreatedByAccountID, 
                JurisdictionID,
                InstrumentID            
            From Documents  Where RecordingDateTime IS NOT NULL
    )
    SELECT
        @PreviousInstrumentID            = MAX(CASE RowNum WHEN @RowNum - 1 THEN InstrumentID       END),
        @PreviousDocumentID              = MAX(CASE RowNum WHEN @RowNum - 1 THEN DocumentID         END),
        @PreviousCreatedByAccountID      = MAX(CASE RowNum WHEN @RowNum - 1 THEN CreatedByAccountID END),
        @PreviousBelongsToJurisdictionID = MAX(CASE RowNum WHEN @RowNum - 1 THEN JurisdictionID     END),
        @NextInstrumentID                = MAX(CASE RowNum WHEN @RowNum + 1 THEN InstrumentID       END),
        @NextDocumentID                  = MAX(CASE RowNum WHEN @RowNum + 1 THEN DocumentID         END),
        @NextCreatedByAccountID          = MAX(CASE RowNum WHEN @RowNum + 1 THEN CreatedByAccountID END),
        @NextBelongsToJurisdictionID     = MAX(CASE RowNum WHEN @RowNum + 1 THEN JurisdictionID     END)
    FROM normal
    WHERE RowNum IN (@RowNum - 1, @RowNum + 1)


    if @JurisdictionID > 0 
    Begin
        If @NextBelongsToJurisdictionID = @JurisdictionID
        Begin
            Set @ShowPublicPageForNextInstrumentID = 0
        End
        Else
        Begin
            Set @ShowPublicPageForNextInstrumentID = 1
        End

        If @PreviousBelongsToJurisdictionID = @JurisdictionID
        Begin
            Set @ShowPublicPageForPreviousInstrumentID = 0
        End
        Else
        Begin
            Set @ShowPublicPageForPreviousInstrumentID = 1
        End

    End
    Else If @AccountID > 0
    Begin

        If @NextCreatedByAccountID = @AccountID
        Begin
            Set @ShowPublicPageForNextInstrumentID = 0
        End
        Else
        Begin
            Set @ShowPublicPageForNextInstrumentID = 1
        End

        If @PreviousCreatedByAccountID = @AccountID
        Begin
            Set @ShowPublicPageForPreviousInstrumentID = 0
        End
        Else
        Begin
            Set @ShowPublicPageForPreviousInstrumentID = 1
        End

    End
    Else
    Begin
        Set @ShowPublicPageForNextInstrumentID = 1
        Set @ShowPublicPageForPreviousInstrumentID = 1
    End

    Set @PreviousInstrumentID = IsNull(@PreviousInstrumentID,'')
    Set @NextInstrumentID = IsNull(@NextInstrumentID,'')
    Set @PreviousDocumentID = IsNull(@PreviousDocumentID,0)
    Set @NextDocumentID = IsNull(@NextDocumentID,0)
    Set @ShowPublicPageForPreviousInstrumentID = ISNULL(@ShowPublicPageForPreviousInstrumentID,0)
    Set @ShowPublicPageForNextInstrumentID = IsNull(@ShowPublicPageForNextInstrumentID,0)
END

这是我的表定义以及索引:

CREATE TABLE [dbo].[Documents](
    [DocumentID] [int] IDENTITY(1000001,1) NOT NULL,
    [IGroupID] [int] NOT NULL,
    [ITypeID] [int] NOT NULL,
    [IDate] [smalldatetime] NOT NULL,
    [JudgementTypeID] [smallint] NULL,
    [JurisdictionID] [int] NOT NULL,
    [DocumentDate] [smalldatetime] NULL,
    [Remarks] [varchar](256) NULL,
    [UserID] [int] NOT NULL,
    [CreatedByAccountID] [int] NULL,
    [CreatedByJurisdictionID] [int] NULL,
    [InternalNotes] [varchar](250) NULL,
    [ParentDocumentID] [int] NULL,
    [DocumentStatusID] [tinyint] NULL,
    [FilingNumber] [int] NULL,
    [EfileDate] [datetime] NULL,
    [EfiledByUserID] [int] NULL,
    [ACEfileCreditCardID] [int] NULL,
    [ACEfileBankAccountID] [int] NULL,
    [StampData] [varchar](1024) NULL,
    [InstrumentID] [varchar](14) NULL,
    [ApprovedBy] [int] NULL,
    [RecordingDateTime] [datetime] NULL,
    [Receipt] [varchar](8000) NULL,
    [ReceiptNo] [varchar](50) NULL,
    [IsReEfiled] [bit] NOT NULL,
    [OldInstrumentID] [varchar](14) NULL,
    [LastStatusChangedDateTime] [datetime] NULL,
    [ImportedFromInstrumentID] [varchar](14) NULL,
    [IsChanged] [bit] NOT NULL,
    [IsUpdatedAfterChanged] [bit] NOT NULL,
 CONSTRAINT [PK_Documents] PRIMARY KEY CLUSTERED 
(
    [DocumentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY],
 CONSTRAINT [UQ_Documents] UNIQUE NONCLUSTERED 
(
    [DocumentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_ACEfileBankAccountID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_ACEfileBankAccountID] ON [dbo].[Documents]
(
    [ACEfileBankAccountID] ASC
)
INCLUDE (   [ApprovedBy],
    [JurisdictionID],
    [DocumentStatusID],
    [DocumentID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [ACEfileCreditCardID],
    [StampData],
    [InstrumentID],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_ACEfileCreditCardID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_ACEfileCreditCardID] ON [dbo].[Documents]
(
    [ACEfileCreditCardID] ASC
)
INCLUDE (   [ACEfileBankAccountID],
    [ApprovedBy],
    [JurisdictionID],
    [DocumentStatusID],
    [DocumentID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [StampData],
    [InstrumentID],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_ApprovedBy]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_ApprovedBy] ON [dbo].[Documents]
(
    [ApprovedBy] ASC
)
INCLUDE (   [JurisdictionID],
    [DocumentStatusID],
    [DocumentID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [StampData],
    [InstrumentID],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_CreatedByAccountID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_CreatedByAccountID] ON [dbo].[Documents]
(
    [CreatedByAccountID] ASC
)
INCLUDE (   [InstrumentID],
    [DocumentStatusID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [StampData],
    [ApprovedBy],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_CreatedByJurisdictionID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_CreatedByJurisdictionID] ON [dbo].[Documents]
(
    [CreatedByJurisdictionID] ASC
)
INCLUDE (   [CreatedByAccountID],
    [InstrumentID],
    [DocumentStatusID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [StampData],
    [ApprovedBy],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_DocumentDate]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_DocumentDate] ON [dbo].[Documents]
(
    [DocumentDate] ASC
)
INCLUDE (   [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [ApprovedBy],
    [JurisdictionID],
    [DocumentStatusID],
    [DocumentID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [Remarks],
    [UserID],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [StampData],
    [InstrumentID],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_DocumentStatusID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_DocumentStatusID] ON [dbo].[Documents]
(
    [DocumentStatusID] ASC
)
INCLUDE (   [DocumentID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [StampData],
    [InstrumentID],
    [ApprovedBy],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_EfileDate]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_EfileDate] ON [dbo].[Documents]
(
    [EfileDate] ASC
)
INCLUDE (   [UserID],
    [RecordingDateTime],
    [ApprovedBy],
    [ACEfileBankAccountID],
    [LastStatusChangedDateTime],
    [ACEfileCreditCardID],
    [EfiledByUserID],
    [ITypeID],
    [IGroupID],
    [InstrumentID],
    [OldInstrumentID],
    [CreatedByJurisdictionID],
    [CreatedByAccountID],
    [DocumentStatusID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [StampData],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_EfiledByUserID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_EfiledByUserID] ON [dbo].[Documents]
(
    [EfiledByUserID] ASC
)
INCLUDE (   [ITypeID],
    [IGroupID],
    [InstrumentID],
    [OldInstrumentID],
    [CreatedByJurisdictionID],
    [CreatedByAccountID],
    [DocumentStatusID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [StampData],
    [ApprovedBy],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

/****** Object:  Index [IX_FilingNumber]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_FilingNumber] ON [dbo].[Documents]
(
    [FilingNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_IDate]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_IDate] ON [dbo].[Documents]
(
    [IDate] ASC
)
INCLUDE (   [EfileDate],
    [UserID],
    [RecordingDateTime],
    [ApprovedBy],
    [ACEfileBankAccountID],
    [LastStatusChangedDateTime],
    [ACEfileCreditCardID],
    [EfiledByUserID],
    [ITypeID],
    [IGroupID],
    [InstrumentID],
    [OldInstrumentID],
    [CreatedByJurisdictionID],
    [CreatedByAccountID],
    [DocumentStatusID],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [StampData],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_IGroupID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_IGroupID] ON [dbo].[Documents]
(
    [IGroupID] ASC
)
INCLUDE (   [UserID],
    [DocumentDate],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [ApprovedBy],
    [JurisdictionID],
    [DocumentStatusID],
    [DocumentID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [Remarks],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [StampData],
    [InstrumentID],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_ImportedFromInstrumentID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_ImportedFromInstrumentID] ON [dbo].[Documents]
(
    [ImportedFromInstrumentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_InstrumentID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_InstrumentID] ON [dbo].[Documents]
(
    [InstrumentID] ASC
)
INCLUDE (   [OldInstrumentID],
    [CreatedByJurisdictionID],
    [CreatedByAccountID],
    [DocumentStatusID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [StampData],
    [ApprovedBy],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_ITypeID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_ITypeID] ON [dbo].[Documents]
(
    [ITypeID] ASC
)
INCLUDE (   [IGroupID],
    [UserID],
    [DocumentDate],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [ApprovedBy],
    [JurisdictionID],
    [DocumentStatusID],
    [DocumentID],
    [IDate],
    [JudgementTypeID],
    [Remarks],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [StampData],
    [InstrumentID],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_JurisdictionID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_JurisdictionID] ON [dbo].[Documents]
(
    [JurisdictionID] ASC
)
INCLUDE (   [DocumentStatusID],
    [DocumentID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [StampData],
    [InstrumentID],
    [ApprovedBy],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_LastStatusChangedDateTime]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_LastStatusChangedDateTime] ON [dbo].[Documents]
(
    [LastStatusChangedDateTime] ASC
)
INCLUDE (   [ACEfileBankAccountID],
    [ACEfileCreditCardID],
    [EfiledByUserID],
    [ITypeID],
    [IGroupID],
    [InstrumentID],
    [OldInstrumentID],
    [CreatedByJurisdictionID],
    [CreatedByAccountID],
    [DocumentStatusID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [StampData],
    [ApprovedBy],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_OldInstrumentID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_OldInstrumentID] ON [dbo].[Documents]
(
    [OldInstrumentID] ASC
)
INCLUDE (   [CreatedByJurisdictionID],
    [CreatedByAccountID],
    [InstrumentID],
    [DocumentStatusID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [StampData],
    [ApprovedBy],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

/****** Object:  Index [IX_ParentDocumentID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_ParentDocumentID] ON [dbo].[Documents]
(
    [ParentDocumentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_RecordingDateTime]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_RecordingDateTime] ON [dbo].[Documents]
(
    [RecordingDateTime] ASC
)
INCLUDE (   [ApprovedBy],
    [ACEfileBankAccountID],
    [LastStatusChangedDateTime],
    [ACEfileCreditCardID],
    [EfiledByUserID],
    [ITypeID],
    [IGroupID],
    [InstrumentID],
    [OldInstrumentID],
    [CreatedByJurisdictionID],
    [CreatedByAccountID],
    [DocumentStatusID],
    [IDate],
    [JudgementTypeID],
    [JurisdictionID],
    [DocumentDate],
    [Remarks],
    [UserID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [StampData],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

SET ANSI_PADDING ON

GO

/****** Object:  Index [IX_UserID]    Script Date: 3/13/2013 5:17:53 AM ******/
CREATE NONCLUSTERED INDEX [IX_UserID] ON [dbo].[Documents]
(
    [UserID] ASC
)
INCLUDE (   [DocumentDate],
    [ACEfileCreditCardID],
    [ACEfileBankAccountID],
    [ApprovedBy],
    [JurisdictionID],
    [DocumentStatusID],
    [DocumentID],
    [IGroupID],
    [ITypeID],
    [IDate],
    [JudgementTypeID],
    [Remarks],
    [CreatedByAccountID],
    [CreatedByJurisdictionID],
    [InternalNotes],
    [ParentDocumentID],
    [FilingNumber],
    [EfileDate],
    [EfiledByUserID],
    [StampData],
    [InstrumentID],
    [RecordingDateTime],
    [Receipt],
    [ReceiptNo],
    [IsReEfiled],
    [OldInstrumentID],
    [LastStatusChangedDateTime],
    [ImportedFromInstrumentID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO

有什么方法可以加快这个查询的速度吗?基本上我想尝试下一个和前一个。因此,如果我传入一些乐器ID,我会尝试找到它的上一个和下一个记录。

这是我的滞后/主导查询(新功能 - 慢速表现:()

;With normal As
    (   
        Select  Top 100 Percent
                InstrumentID, 
                Lead(d.InstrumentID) Over (Order By RecordingDateTime) as LeadInstrumentID, 
                Lag(d.InstrumentID) Over (Order By RecordingDateTime) as LagInstrumentID,
                DocumentID,
                Lead(d.DocumentID) Over (Order By RecordingDateTime) as LeadDocumentID, 
                Lag(d.DocumentID) Over (Order By RecordingDateTime) as LagDocumentID, 
                CreatedByAccountID,
                Lead(d.CreatedByAccountID) Over (Order By RecordingDateTime) as LeadCreatedByAccountID, 
                Lag(d.CreatedByAccountID) Over (Order By RecordingDateTime) as LagCreatedByAccountID, 
                JurisdictionID,
                Lead(d.JurisdictionID) Over (Order By RecordingDateTime) as LeadJurisdictionID, 
                Lag(d.JurisdictionID) Over (Order By RecordingDateTime) as LagJurisdictionID


        From Documents d Where RecordingDateTime IS NOT NULL
            Order by RecordingDateTime
    )
    SELECT
        @PreviousInstrumentID            = LagInstrumentID,
        @PreviousDocumentID              = LagDocumentID,
        @PreviousCreatedByAccountID      = LagCreatedByAccountID,
        @PreviousBelongsToJurisdictionID = LagJurisdictionID,
        @NextInstrumentID                = LeadInstrumentID,
        @NextDocumentID                  = LeadDocumentID,
        @NextCreatedByAccountID          = LeadCreatedByAccountID,
        @NextBelongsToJurisdictionID     = LeadJurisdictionID
    FROM normal n
        Where n.InstrumentID = @InstrumentID

现在我已经有了一个关于InstrumentID的索引,但是这个查询比我之前在SSMS中花了2秒的查询更糟糕。

这就是我最终得到的--3个单独的查询:

Declare @RecordingDateTime  datetime

    Select @RecordingDateTime = RecordingDateTime
        From Documents d Where d.InstrumentID = @InstrumentID

    Select  Top 1   @PreviousInstrumentID          =  InstrumentID ,
                    @PreviousDocumentID            = DocumentID  ,
                    @PreviousCreatedByAccountID    =  CreatedByAccountID,
                    @PreviousBelongsToJurisdictionID = JurisdictionID 
        From Documents d 
            Where d.RecordingDateTime < @RecordingDateTime
                Order By d.RecordingDateTime Desc

    Select  Top 1   @NextInstrumentID          =  InstrumentID ,
                    @NextDocumentID            = DocumentID  ,
                    @NextCreatedByAccountID    =  CreatedByAccountID,
                    @NextBelongsToJurisdictionID = JurisdictionID 
        From Documents d 
            Where d.RecordingDateTime > @RecordingDateTime
                Order By d.RecordingDateTime 

此查询的持续时间仅为25,如SQL分析器

中所述

0 个答案:

没有答案