在 ASP.NET 2.0 中的改进的 DeleteExpiredSessions 存储过程


简介


如果两个会话的数量和单个会话的大小增加 Microsoft ASP.NET 2.0 中,删除已过期的会话可能会影响 ASP.NET Web 站点的整体性能。 执行删除作业时,在运行 Microsoft SQL Server 的服务器上发生的任何拦截将对执行 ASP.NET 请求使用 SQL Server 会话模式的产生负面影响。

DeleteExpiredSessions存储过程的最新改进提高了性能,并减少阻塞。这一改进最小化对 ASP.NET 请求的执行造成不利影响。 这种改善也将在下一版本的 ASP.NET 中实现。

详细信息


若要使用新的DeleteExpiredSessions存储过程,请按照下列步骤操作︰


  1. 打开 SQL Server 管理 Studio 控制台。
  2. 单击新建查询
  3. 如果您使用非持久性的 SQL 会话状态,粘贴以下脚本到右窗格。
    USE [ASPState]GO

    DECLARE @ver int
    EXEC dbo.GetMajorVersion @@ver=@ver OUTPUT
    DECLARE @cmd nchar(4000)
    IF (@ver >= 8)
    SET @cmd = N'
    ALTER PROCEDURE dbo.DeleteExpiredSessions
    AS
    SET NOCOUNT ON
    SET DEADLOCK_PRIORITY LOW

    DECLARE @now datetime
    SET @now = GETUTCDATE()

    CREATE TABLE #tblExpiredSessions
    (
    SessionId nvarchar(88) NOT NULL PRIMARY KEY
    )

    INSERT #tblExpiredSessions (SessionId)
    SELECT SessionId
    FROM [tempdb].dbo.ASPStateTempSessions WITH (READUNCOMMITTED)
    WHERE Expires < @now

    IF @@ROWCOUNT <> 0
    BEGIN
    DECLARE ExpiredSessionCursor CURSOR LOCAL FORWARD_ONLY READ_ONLY
    FOR SELECT SessionId FROM #tblExpiredSessions

    DECLARE @SessionId nvarchar(88)

    OPEN ExpiredSessionCursor

    FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId

    WHILE @@FETCH_STATUS = 0
    BEGIN
    DELETE FROM [tempdb].dbo.ASPStateTempSessions WHERE SessionId = @SessionId
    FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId
    END

    CLOSE ExpiredSessionCursor

    DEALLOCATE ExpiredSessionCursor

    END

    DROP TABLE #tblExpiredSessions

    RETURN 0'
    ELSE
    SET @cmd = N'
    ALTER PROCEDURE dbo.DeleteExpiredSessions
    AS
    SET NOCOUNT ON
    SET DEADLOCK_PRIORITY LOW

    DECLARE @now datetime
    SET @now = GETDATE()

    CREATE TABLE #tblExpiredSessions
    (
    SessionId nvarchar(88) NOT NULL PRIMARY KEY
    )

    INSERT #tblExpiredSessions (SessionId)
    SELECT SessionId
    FROM [tempdb].dbo.ASPStateTempSessions WITH (READUNCOMMITTED)
    WHERE Expires < @now

    IF @@ROWCOUNT <> 0
    BEGIN
    DECLARE ExpiredSessionCursor CURSOR LOCAL FORWARD_ONLY READ_ONLY
    FOR SELECT SessionId FROM #tblExpiredSessions

    DECLARE @SessionId nvarchar(88)

    OPEN ExpiredSessionCursor

    FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId

    WHILE @@FETCH_STATUS = 0
    BEGIN
    DELETE FROM [tempdb].dbo.ASPStateTempSessions WHERE SessionId = @SessionId
    FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId
    END

    CLOSE ExpiredSessionCursor

    DEALLOCATE ExpiredSessionCursor

    END

    DROP TABLE #tblExpiredSessions

    RETURN 0'
    EXEC (@cmd)
    GO
    如果您使用持久的 SQL 会话状态,粘贴以下脚本到右窗格。
    USE [ASPState]GO

    DECLARE @ver int
    EXEC dbo.GetMajorVersion @@ver=@ver OUTPUT
    DECLARE @cmd nchar(4000)
    IF (@ver >= 8)
    SET @cmd = N'
    ALTER PROCEDURE dbo.DeleteExpiredSessions
    AS
    SET NOCOUNT ON
    SET DEADLOCK_PRIORITY LOW

    DECLARE @now datetime
    SET @now = GETUTCDATE()

    CREATE TABLE #tblExpiredSessions
    (
    SessionId nvarchar(88) NOT NULL PRIMARY KEY
    )

    INSERT #tblExpiredSessions (SessionId)
    SELECT SessionId
    FROM [ASPState].dbo.ASPStateTempSessions WITH (READUNCOMMITTED)
    WHERE Expires < @now

    IF @@ROWCOUNT <> 0
    BEGIN
    DECLARE ExpiredSessionCursor CURSOR LOCAL FORWARD_ONLY READ_ONLY
    FOR SELECT SessionId FROM #tblExpiredSessions

    DECLARE @SessionId nvarchar(88)

    OPEN ExpiredSessionCursor

    FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId

    WHILE @@FETCH_STATUS = 0
    BEGIN
    DELETE FROM [ASPState].dbo.ASPStateTempSessions WHERE SessionId = @SessionId
    FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId
    END

    CLOSE ExpiredSessionCursor

    DEALLOCATE ExpiredSessionCursor

    END

    DROP TABLE #tblExpiredSessions

    RETURN 0'
    ELSE
    SET @cmd = N'
    ALTER PROCEDURE dbo.DeleteExpiredSessions
    AS
    SET NOCOUNT ON
    SET DEADLOCK_PRIORITY LOW

    DECLARE @now datetime
    SET @now = GETDATE()

    CREATE TABLE #tblExpiredSessions
    (
    SessionId nvarchar(88) NOT NULL PRIMARY KEY
    )

    INSERT #tblExpiredSessions (SessionId)
    SELECT SessionId
    FROM [ASPState].dbo.ASPStateTempSessions WITH (READUNCOMMITTED)
    WHERE Expires < @now

    IF @@ROWCOUNT <> 0
    BEGIN
    DECLARE ExpiredSessionCursor CURSOR LOCAL FORWARD_ONLY READ_ONLY
    FOR SELECT SessionId FROM #tblExpiredSessions

    DECLARE @SessionId nvarchar(88)

    OPEN ExpiredSessionCursor

    FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId

    WHILE @@FETCH_STATUS = 0
    BEGIN
    DELETE FROM [ASPState].dbo.ASPStateTempSessions WHERE SessionId = @SessionId
    FETCH NEXT FROM ExpiredSessionCursor INTO @SessionId
    END

    CLOSE ExpiredSessionCursor

    DEALLOCATE ExpiredSessionCursor

    END

    DROP TABLE #tblExpiredSessions

    RETURN 0'
    EXEC (@cmd)
    GO
  4. 单击执行
要确定是否会话状态数据库被配置为永久或非永久 SQL 会话状态,请使用下列规范︰
  • 非持久性 SQL 会话状态
    在 tempdb 数据库来存储会话数据创建 ASPStateTempApplications 和 ASPStateTempSessions 的表。
  • 持久的 SQL 会话状态
    会话状态数据库来存储会话数据所在的数据库中创建表 ASPStateTempApplications 和 ASPStateTempSessions。