SQL Paging采用Longer(x15~x20)比获取所有数据更正常吗?

时间:2015-04-01 08:45:06

标签: sql sql-server sql-server-2008 pagination sqlperformance

我有大约16k行的View,获取所有数据大约需要5秒。

我决定在应用程序中实现“加载”,因此GUI不会冻结,用户可以在DataGridView中使用/查看提供的数据。

我注意到如果我使用SQL Paging获取所有数据需要大约90秒(1.5分钟),这样会适得其反。

现在我想知道这是否正常,为什么有人会使用它?

我尝试了3种SQL分页方式:

  

我正在使用160进行测试!

DECLARE @int_percentage AS INT = 1

WHILE @int_percentage <= 100
BEGIN
    SELECT O.*, P.Percentage
    FROM vAppointmentDetailsWithComments O
    LEFT JOIN (SELECT AppointmentID, NTILE(100) OVER(ORDER BY AppointmentID) Percentage
                FROM vAppointmentDetailsWithoutComments) P ON P.AppointmentID = O.AppointmentID
    WHERE P.Percentage = @int_percentage

    SET @int_percentage = @int_percentage + 1
END
---------------------------------------------------------------------------------------------------
DECLARE @int_percentage AS INT = 1, @int_appointmentID AS INT = 0

WHILE @int_percentage <= 100
BEGIN
    SELECT TOP 160 *
    FROM vAppointmentDetailsWithComments
    WHERE AppointmentID > @int_appointmentID

    SET @int_percentage = @int_percentage + 1
    SET @int_appointmentID = @int_appointmentID + 161
END
---------------------------------------------------------------------------------------------------
DECLARE @int_percentage AS INT = 1, @int_currentStartingRowIndex AS INT = 1

WHILE @int_percentage <= 100
BEGIN
    EXEC spGetRows @int_startingRowIndex = @int_currentStartingRowIndex, @int_maxRows = 160

    SET @int_percentage = @int_percentage + 1
    SET @int_currentStartingRowIndex = @int_currentStartingRowIndex + 160
END
---------------------------------------------------------------------------------------------------
SELECT *
FROM vAppointmentDetailsWithComments

步骤:

CREATE PROCEDURE [dbo].[spGetRows] 
(
    @int_startingRowIndex INT,
    @int_maxRows INT
)
AS

DECLARE @int_firstID INT

-- Getting 1'st ID
SET ROWCOUNT @int_startingRowIndex
SELECT @int_firstID = AppointmentID FROM vAppointmentDetailsWithoutComments ORDER BY AppointmentID

-- Setting ROWCOUNT to MAX
SET ROWCOUNT @int_maxRows

-- Getting all data >= @int_firstID
SELECT *
FROM vAppointmentDetailsWithComments
WHERE AppointmentID >= @int_firstID

SET ROWCOUNT 0

GO

结果: Results

表格和视图创建和填充数据:

  “vAppointmentDetailsWithComments”中的

FOR XML PATH是主要表现   问题

CREATE TABLE [dbo].[Appointment](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Number] [int] NOT NULL,
 CONSTRAINT [PK_Appointment] 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].[Appointment] ADD  CONSTRAINT [DF_Appointment_Number]  DEFAULT ((0)) FOR [Number]
GO
---------------------------------------------------------------------------------------------------
CREATE TABLE [dbo].[Comment](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Appointment_ID] [int] NOT NULL,
    [Text] [nvarchar](max) NOT NULL,
    [Time] [datetime] NOT NULL,
 CONSTRAINT [PK_Comment] 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].[Comment]  WITH CHECK ADD  CONSTRAINT [FK_Comment_Appointment] FOREIGN KEY([Appointment_ID])
REFERENCES [dbo].[Appointment] ([ID])
GO

ALTER TABLE [dbo].[Comment] CHECK CONSTRAINT [FK_Comment_Appointment]
GO

ALTER TABLE [dbo].[Comment] ADD  CONSTRAINT [DF_Comment_Text]  DEFAULT (N'Some random Comment for Testing purposes') FOR [Text]
GO

ALTER TABLE [dbo].[Comment] ADD  CONSTRAINT [DF_Comment_Time]  DEFAULT (getdate()) FOR [Time]
GO
---------------------------------------------------------------------------------------------------
CREATE VIEW [dbo].[vAppointmentDetailsWithComments]
AS
SELECT A.ID AppointmentID, (K.Comments + CHAR(13) + CHAR(10)) Comment
FROM Appointment A LEFT JOIN
    (SELECT A.ID,
        (SELECT STUFF
            ((SELECT REPLACE(CHAR(13) + CHAR(10) + K.Text, CHAR(7), '')
        FROM Comment K
        WHERE K.Appointment_ID = A.ID
        AND K.Text != ''
        ORDER BY K.Time FOR XML PATH, TYPE ).value('.[1]', 'NVARCHAR(MAX)'), 1, 1, '')) Comments
    FROM Appointment A) K ON K.ID = A.ID

GO
---------------------------------------------------------------------------------------------------
CREATE VIEW [dbo].[vAppointmentDetailsWithoutComments]
AS
SELECT A.ID AppointmentID
FROM Appointment A

GO
---------------------------------------------------------------------------------------------------
SET NOCOUNT ON 
BEGIN TRAN 
DECLARE @int_appointmentID AS INT = 1,
         @int_tempComment AS INT
WHILE @int_appointmentID <= 16000 
BEGIN 
    INSERT INTO Appointment VALUES (@int_appointmentID)

    SET @int_tempComment = 1

    WHILE @int_tempComment <= 5
    BEGIN
        INSERT INTO Comment (Appointment_ID) VALUES (@int_appointmentID)

        SET @int_tempComment = @int_tempComment + 1
    END

SET @int_appointmentID = @int_appointmentID + 1 
END 
COMMIT TRAN

GO

执行计划: Fast(FetchAll) Slow(Top)

1 个答案:

答案 0 :(得分:1)

部分性能问题是因为Comment表Appointment_ID列上没有索引。使用Appointment_ID上的聚簇索引并将主键索引更改为非群集,vAppointmentDetailsWithComments的select查询从我的测试框上的约5秒减少到约3.5秒。下面是一个用于创建聚簇索引并将主键重新创建为非聚集索引的脚本。

ALTER TABLE dbo.Comment DROP CONSTRAINT FK_Comment_Appointment;

ALTER TABLE Appointment DROP CONSTRAINT PK_Appointment;

ALTER TABLE Appointment ADD CONSTRAINT PK_Appointment
    PRIMARY KEY NONCLUSTERED(ID);

ALTER TABLE dbo.Comment 
    ADD CONSTRAINT FK_Comment_Appointment FOREIGN KEY(Appointment_ID)
    REFERENCES dbo.Appointment (ID);


CREATE CLUSTERED INDEX cdx_Comment_Appointment_ID ON Comment(Appointment_ID);
GO

注释的字符串连接是在T-SQL中执行的昂贵操作。我建议你在应用程序方面这样做,我希望它对于16K行来说是亚秒级的。这样就可以通过简单的连接注释来避免在SQL端跳过箍:

CREATE VIEW dbo.vAppointmentDetailsWithIndividualComments
AS
SELECT A.ID AppointmentID, K.Text, K.Time
FROM dbo.Appointment A 
LEFT JOIN dbo.Comment K
        ON K.Appointment_ID = A.ID
        AND K.Text <> '';
GO

SELECT AppointmentID, Text, Time
FROM dbo.vAppointmentDetailsWithIndividualComments
ORDER BY Time;
GO

关于您列出的分页技术,由于约会的扫描,第一个将逐步进入结果集。

第二个查询缺少ORDER BY Appointment_ID。对于确定性结果,ORDER BY需要TOP。但是,这种方法确实具有分页性能的优点,因为它将在约会表上执行索引查找,无论结果集中的位置如何,都能提供一致的性能。

SET ROWCOUNT已弃用,但底线是与第一个查询的执行方式相似(逐渐变差)。