Delete session from database after it expired?

后端 未结 3 680
野趣味
野趣味 2021-01-15 11:54

This might be a silly question but i was wondering if it would be a good idea to delete all expired \"sessions\" from my database, every 15 minutes?

Or just leave it

相关标签:
3条回答
  • 2021-01-15 12:29

    When my team deploys SQL Server session state in a .NET application, we simply create a job with SQL Server Agent to clean up the expired sessions nightly (or at some appropriate interval).

    Unless you have massive amounts of traffic, you can do this whenever it is convenient without worrying about instantly deleting unused sessions, but it's definitely a good idea to clean up.

    0 讨论(0)
  • 2021-01-15 12:32

    If you're using the standard SQL Server Session State, old sessions are automatically deleted for you. Until the old entries are deleted, sessions do not expire, since the provider does not actively monitor the Expires column in the session table.

    The install / configuration utility creates a SQL Agent job that calls the DeleteExpiredSessions stored procedure every 60 seconds. This means that SQL Agent needs to be running for session expiration to work and for the table to get cleaned up.

    0 讨论(0)
  • 2021-01-15 12:37

    If your table looks like that

    SessionId datatype,
    SessionLastTouch datetime
    

    then when registering the new session do some cleanup after writing(session issue or touch) to the table, like this:

    delete from YourSessionsRegister WHERE SessionLastTouch < DATEADD(min, -15, GETDATE())
    

    BUT

    May be better way is to cleanup the session register based on some schedule - to reduce DB load in peak hours - simply create the job, which cleans up the register every night or smth. like this

    0 讨论(0)
提交回复
热议问题