SQL查询需要1分35秒

时间:2013-09-30 07:21:04

标签: sql sql-server

我正在主机上执行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

TABLE SCHEMAS

    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]

GO

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

参考[dbo]。[AGAM_Inquiry]([ID]) GO

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

GO

第二表

    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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您对EnquiryId和CourseID有索引吗?

看到你正在加入,通过它进行排序和分区你真的应该。

CREATE INDEX IDX_AGAM_AceOFPace_EnquiryID
ON AGAM_AceOFPace (EnquiryID)

CREATE INDEX IDX_AGAM_AceOFPace_CourseID
ON AGAM_AceOFPace (CourseID)