0

我正在主机上的 SQL 服务器上执行查询,它需要 1 分 35 秒。而且检索的行数是18000。仍然需要太多时间。查询是

select ID,
       FirstName,
       LastName,
       Branch,
       EnquiryID,
       Course,
       College,
       Mobile,
       ExamID,
       EntranceID,
       Entrance,
       Venue,
       RegNo,
       VenueID,
       Exam,
       Gender,
       row_number() over (partition by EnquiryID order by ID asc) as AttemptNO
from AGAM_View_AOPList
order by EnquiryID

表模式

    CREATE TABLE [dbo].[AGAM_AceOFPace](
[ID] [int] IDENTITY(1,1) NOT NULL,
[EnquiryID] [int] NULL,
[FirstName] [nvarchar](100) NULL,
[MiddleName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[BranchID] [int] NULL,
[Branch] [nvarchar](100) NULL,
[CourseID] [int] NULL,
[ExamID] [int] NULL,
[Exam] [nvarchar](200) NULL,
[EntranceID] [int] NULL,
[Entrance] [nvarchar](200) NULL,
[RegNo] [nvarchar](200) NULL,
[EntranceCode] [nvarchar](100) NULL,
[ExamDate] [nvarchar](50) NULL,
[UserID] [nvarchar](100) NULL,
[EntranceFees] [numeric](18, 2) NULL,
[VenueID] [int] NULL,
[Venue] [nvarchar](max) NULL,
[ChequeNumber] [nvarchar](50) NULL,
[Bank] [nvarchar](100) NULL,
[CreatedDate] [datetime] NULL,
    CONSTRAINT [PK_AGAM_AceOFPace] PRIMARY KEY CLUSTERED 
    (
[ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,    ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    ALTER TABLE [dbo].[AGAM_AceOFPace]  WITH CHECK ADD  CONSTRAINT [FK_AGAM_AceOFPace_AGAM_Inquiry] FOREIGN KEY([EnquiryID])

参考 [dbo].[AGAM_Inquiry] ([ID]) 去

    ALTER TABLE [dbo].[AGAM_AceOFPace] CHECK CONSTRAINT   [FK_AGAM_AceOFPace_AGAM_Inquiry]

第二张桌子

    CREATE TABLE [dbo].[AGAM_Inquiry](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RegNo] [nvarchar](200) NULL,
[BranchID] [int] NULL,
[Category] [nvarchar](100) NULL,
[CourseID] [int] NULL,
[EntranceFees] [numeric](18, 2) NULL,
[EntranceID] [int] NULL,
[UserID] [nvarchar](50) NULL,
[Status] [nvarchar](50) NULL,
[ReminderDate] [datetime] NULL,
[Reminder] [nvarchar](150) NULL,
[Mobile] [nvarchar](50) NULL,
[Email] [nvarchar](50) NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Landline] [nvarchar](50) NULL,
[Address] [nvarchar](100) NULL,
[DOB] [datetime] NULL,
[Gender] [nvarchar](50) NULL,
[PfBatchTime] [nvarchar](50) NULL,
[SourceOfInquiry] [nvarchar](50) NULL,
[ExStudentID] [int] NULL,
[InquiryDate] [datetime] NULL,
[ReceiptNumber] [nvarchar](50) NULL,
[RawID] [int] NULL,
[Deleted] [int] NULL,
[CreatedBy] [nvarchar](50) NULL,
[CreatedDate] [datetime] NULL,
[LastModifiedBy] [nvarchar](50) NULL,
[LastModifiedDate] [datetime] NULL,
[College] [nvarchar](150) NULL,
[Qualification] [nvarchar](150) NULL,
[RptNo] [nvarchar](100) NULL,
    CONSTRAINT [PK_AGAM_Inquiry] PRIMARY KEY CLUSTERED 
    (
[ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,   ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO

    ALTER TABLE [dbo].[AGAM_Inquiry]  WITH CHECK ADD  CONSTRAINT   [FK_AGAM_Inquiry_AGAM_Branch] FOREIGN KEY([BranchID])
    REFERENCES [dbo].[AGAM_Branch] ([ID])
    GO

    ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Branch]
    GO

    ALTER TABLE [dbo].[AGAM_Inquiry]  WITH CHECK ADD  CONSTRAINT   [FK_AGAM_Inquiry_AGAM_Course] FOREIGN KEY([CourseID])
    REFERENCES [dbo].[AGAM_Course] ([ID])
    GO

    ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Course]
    GO

     ALTER TABLE [dbo].[AGAM_Inquiry]  WITH CHECK ADD  CONSTRAINT  [FK_AGAM_Inquiry_AGAM_Users] FOREIGN KEY([UserID])
    REFERENCES [dbo].[AGAM_Users] ([UserID])
   GO

    ALTER TABLE [dbo].[AGAM_Inquiry] CHECK CONSTRAINT [FK_AGAM_Inquiry_AGAM_Users]
   GO
4

2 回答 2

0

你有关于 EnquiryId 和 CourseID 的索引吗?

看到你正在加入、排序和分区,你真的应该这样做。

CREATE INDEX IDX_AGAM_AceOFPace_EnquiryID
ON AGAM_AceOFPace (EnquiryID)

CREATE INDEX IDX_AGAM_AceOFPace_CourseID
ON AGAM_AceOFPace (CourseID)
于 2013-09-30T09:33:55.957 回答
0

您可以尝试将视图更改为此吗?

SELECT TOP (100) PERCENT 
    AP.ID, 
    AP.FirstName, 
    AP.LastName, 
    AP.Branch, 
    AP.EnquiryID, 
    AC.Name,
    AI.College, 
    AI.Mobile, 
    AP.ExamID, 
    AP.EntranceID, 
    AP.RegNo, 
    AP.VenueID, 
    AP.Exam, 
    AI.Gender, 
    AP.BranchID, 
    AP.CourseID, 
    AP.CreatedDate, 
    AI.Status, 
    AP.Entrance, 
    AP.Venue 
FROM dbo.AGAM_AceOFPace AS AP 
INNER JOIN dbo.AGAM_Inquiry AS AI ON AI.ID = AP.EnquiryID 
INNER JOIN dbo.AGAM_Course as AC on AC.ID = AP.CourseId
ORDER BY AP.EnquiryID
于 2013-09-30T08:13:09.267 回答